summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-02-08 19:18:25 +0100
committerAki <nthirtyone@gmail.com>2017-02-08 19:18:25 +0100
commita7b1bcc6872579b0ed8dbce399352d3afca1a80f (patch)
treecad837f35cc495d42385c3238de06804e8d68d07
parent939ef3d25ec3dadba9e86b04ae1e294e789175d0 (diff)
parentf44ce392bc511fdfd24c54a5ee24b09f8ff6836f (diff)
downloadroflnauts-a7b1bcc6872579b0ed8dbce399352d3afca1a80f.zip
roflnauts-a7b1bcc6872579b0ed8dbce399352d3afca1a80f.tar.gz
roflnauts-a7b1bcc6872579b0ed8dbce399352d3afca1a80f.tar.bz2
Merge branch 'animations'
-rw-r--r--animated.lua81
-rw-r--r--animations.lua4
-rw-r--r--assets/platforms/alpha-big-1.pngbin702 -> 0 bytes
-rw-r--r--assets/platforms/alpha-big.pngbin0 -> 3836 bytes
-rw-r--r--assets/platforms/alpha-small-1.pngbin351 -> 0 bytes
-rw-r--r--assets/platforms/alpha-small.pngbin0 -> 821 bytes
-rw-r--r--decoration.lua6
-rw-r--r--ground.lua20
-rw-r--r--maps/alpha abyss.lua34
-rw-r--r--player.lua75
-rw-r--r--world.lua14
11 files changed, 164 insertions, 70 deletions
diff --git a/animated.lua b/animated.lua
new file mode 100644
index 0000000..160ee44
--- /dev/null
+++ b/animated.lua
@@ -0,0 +1,81 @@
+-- `Animated`
+-- Abstract class for drawable animated entities.
+
+-- Metatable
+Animated = {
+ animations--[[table with animations]],
+ current--[[animations.default]],
+ sprite--[[love.graphics.newImage()]],
+ frame = 1,
+ delay = .1,
+}
+Animated.__index = Animated
+
+-- Cleans up reference to sprite on deletion.
+function Animated:delete()
+ self.sprite = nil
+end
+
+-- Sets an Image as a sprite.
+function Animated:setSprite(image)
+ self.sprite = image
+end
+-- Returns current sprite Image.
+function Animated:getSprite()
+ return self.sprite
+end
+
+-- Sets new animations list.
+function Animated:setAnimationsList(t)
+ if t then
+ self.animations = t
+ self:setAnimation("default")
+ end
+end
+
+-- Sets current animation by table key.
+function Animated:setAnimation(animation)
+ self.frame = 1
+ self.delay = Animated.delay -- INITIAL from metatable
+ self.current = self.animations[animation]
+end
+-- Returns current animation table.
+function Animated:getAnimation()
+ return self.current
+end
+
+-- Get frame quad for drawing.
+function Animated:getQuad()
+ if self.animations and self.current then
+ return self.current[self.frame]
+ end
+end
+
+-- Drawing self to LOVE2D buffer.
+-- If there is no Quad, it will draw entire sprite.
+function Animated:draw(...)
+ local s, q = self:getSprite(), self:getQuad()
+ if s then
+ love.graphics.setColor(255,255,255,255)
+ if q then love.graphics.draw(s, q, ...)
+ else love.graphics.draw(s, ...) end
+ end
+end
+-- Animation updating.
+function Animated:update(dt)
+ if self.animations and self.current then
+ self.delay = self.delay - dt
+ if self.delay < 0 then
+ self.delay = self.delay + Animated.delay -- INITIAL from metatable
+ self:nextFrame()
+ end
+ end
+end
+-- Moving to the next frame.
+function Animated:nextFrame()
+ if self.current.repeated or not (self.frame == self.current.frames) then
+ self.frame = (self.frame % self.current.frames) + 1
+ else
+ self:setAnimation("default")
+ end
+end \ No newline at end of file
diff --git a/animations.lua b/animations.lua
index 58c4b88..881da49 100644
--- a/animations.lua
+++ b/animations.lua
@@ -1,8 +1,8 @@
-- Animations spritesheet array for `Player`
-- Basic spritesheet size is 376x26. Each frame is 24x24 and has 1px border around it.
--- From the left: idle (walk0), walk1, walk2, walk3, attack0, attack1, attack3, attack_up0, attack_up1, attack_up2, attack_down0, attack_down1, attack_down2, damage0, damage1
+-- From the left: default (walk0), walk1, walk2, walk3, attack0, attack1, attack3, attack_up0, attack_up1, attack_up2, attack_down0, attack_down1, attack_down2, damage0, damage1
local animations = {
- idle = {
+ default = {
[1] = love.graphics.newQuad( 1, 1, 24,24, 376,26),
frames = 1,
repeated = true
diff --git a/assets/platforms/alpha-big-1.png b/assets/platforms/alpha-big-1.png
deleted file mode 100644
index 6500f73..0000000
--- a/assets/platforms/alpha-big-1.png
+++ /dev/null
Binary files differ
diff --git a/assets/platforms/alpha-big.png b/assets/platforms/alpha-big.png
new file mode 100644
index 0000000..3397022
--- /dev/null
+++ b/assets/platforms/alpha-big.png
Binary files differ
diff --git a/assets/platforms/alpha-small-1.png b/assets/platforms/alpha-small-1.png
deleted file mode 100644
index b3f1a01..0000000
--- a/assets/platforms/alpha-small-1.png
+++ /dev/null
Binary files differ
diff --git a/assets/platforms/alpha-small.png b/assets/platforms/alpha-small.png
new file mode 100644
index 0000000..cce324f
--- /dev/null
+++ b/assets/platforms/alpha-small.png
Binary files differ
diff --git a/decoration.lua b/decoration.lua
index 9552b09..3ca6f76 100644
--- a/decoration.lua
+++ b/decoration.lua
@@ -4,10 +4,11 @@ Decoration = {
x = 0,
y = 0
}
+Decoration.__index = Decoration
+setmetatable(Decoration, Animated)
function Decoration:new(x, y, sprite)
local o = {}
setmetatable(o, self)
- self.__index = self
o.sprite = love.graphics.newImage(sprite)
o:setPosition(x,y)
return o
@@ -28,6 +29,5 @@ function Decoration:draw(offset_x, offset_y, scale)
local draw_x = (math.floor(x) + offset_x) * scale
local draw_y = (math.floor(y) + offset_y) * scale
-- draw
- love.graphics.setColor(255,255,255,255)
- love.graphics.draw(self.sprite, draw_x, draw_y, 0, scale, scale)
+ Animated.draw(self, draw_x, draw_y, 0, scale, scale)
end \ No newline at end of file
diff --git a/ground.lua b/ground.lua
index cd2b607..1318c11 100644
--- a/ground.lua
+++ b/ground.lua
@@ -3,6 +3,7 @@
-- Collision category: [1]
-- WHOLE CODE HAS FLAG OF "need a cleanup"
+require "animated"
-- Metatable of `Ground`
-- nils initialized in constructor
@@ -11,13 +12,14 @@ Ground = {
shape = nil,
fixture = nil,
world = nil,
- sprite = nil
}
+Ground.__index = Ground
+setmetatable(Ground, Animated)
+
-- Constructor of `Ground`
-function Ground:new (game, world, x, y, shape, sprite)
+function Ground:new (game, world, x, y, shape, sprite, animations)
local o = {}
setmetatable(o, self)
- self.__index = self
o.body = love.physics.newBody(world, x, y)
-- MULTIPLE SHAPES NEED TO BE REWRITED!
o.shape = {}
@@ -37,17 +39,12 @@ function Ground:new (game, world, x, y, shape, sprite)
end
end
-- END HERE
- o.sprite = love.graphics.newImage(sprite)
+ o:setSprite(love.graphics.newImage(sprite))
+ o:setAnimationsList(animations)
o.world = game
return o
end
--- Destructor of `Ground`
-function Ground:delete ()
- -- body deletion is handled by world deletion
- self.sprite = nil
-end
-
-- Position
function Ground:getPosition()
return self.body:getPosition()
@@ -65,8 +62,7 @@ function Ground:draw (offset_x, offset_y, scale, debug)
local draw_x = (math.floor(x) + offset_x) * scale
local draw_y = (math.floor(y) + offset_y) * scale
-- sprite draw
- love.graphics.setColor(255,255,255,255)
- love.graphics.draw(self.sprite, draw_x, draw_y, 0, scale, scale)
+ Animated.draw(self, draw_x, draw_y, 0, scale, scale)
-- debug draw
if debug then
love.graphics.setColor(255, 69, 0, 140)
diff --git a/maps/alpha abyss.lua b/maps/alpha abyss.lua
index a3b6153..0dd2c61 100644
--- a/maps/alpha abyss.lua
+++ b/maps/alpha abyss.lua
@@ -1,4 +1,26 @@
-- The abyss of the alpha.
+-- Animations
+local animations_small = {
+ default = {
+ frames = 20,
+ repeated = true
+ }
+}
+local animations_big = {
+ default = {
+ frames = 20,
+ repeated = true
+ }
+}
+for i=1,10 do
+ local a = love.graphics.newQuad(i*118-118, 0, 118,51, 1180,51)
+ animations_big.default[i*2-1] = a
+ animations_big.default[i*2] = a
+ local a = love.graphics.newQuad(i*60-60, 0, 60,20, 600,20)
+ animations_small.default[i*2-1] = a
+ animations_small.default[i*2] = a
+end
+-- Map data
return {
-- GENERAL
name = "alpha abyss",
@@ -24,25 +46,29 @@ return {
x = -60,
y = 0,
shape = {0,0, 117,0, 101,50, 16,50},
- sprite = "assets/platforms/alpha-big-1.png"
+ sprite = "assets/platforms/alpha-big.png",
+ animations = animations_big
},
{
x = -145,
y = -50,
shape = {0,0, 59,0, 59,19, 0,19},
- sprite = "assets/platforms/alpha-small-1.png"
+ sprite = "assets/platforms/alpha-small.png",
+ animations = animations_small
},
{
x = 85,
y = -50,
shape = {0,0, 59,0, 59,19, 0,19},
- sprite = "assets/platforms/alpha-small-1.png"
+ sprite = "assets/platforms/alpha-small.png",
+ animations = animations_small
},
{
x = -30,
y = -80,
shape = {0,0, 59,0, 59,19, 0,19},
- sprite = "assets/platforms/alpha-small-1.png"
+ sprite = "assets/platforms/alpha-small.png",
+ animations = animations_small
}
},
decorations = {}
diff --git a/player.lua b/player.lua
index d2bc6d1..b6d4e3f 100644
--- a/player.lua
+++ b/player.lua
@@ -3,6 +3,7 @@
-- Collision category: [2]
-- WHOLE CODE HAS FLAG OF "need a cleanup"
+require "animated"
-- Metatable of `Player`
-- nils initialized in constructor
@@ -24,11 +25,6 @@ Player = {
alive = true,
punchcd = 0.25,
punchdir = 0, -- a really bad thing
- -- Animation
- animations = require "animations",
- current = nil,
- frame = 1,
- delay = 0.10,
-- Movement
inAir = true,
salto = false,
@@ -44,15 +40,18 @@ Player = {
portrait_sheet = require "nautsicons",
portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130),
-- Sounds
- sfx = require "sounds"
+ sfx = require "sounds",
+ -- Animations table
+ animations = require "animations"
}
+Player.__index = Player
+setmetatable(Player, Animated)
-- Constructor of `Player`
function Player:new (game, world, x, y, name)
-- Meta
local o = {}
setmetatable(o, self)
- self.__index = self
-- Physics
local group = -1-#game.Nauts
o.body = love.physics.newBody(world, x, y, "dynamic")
@@ -65,11 +64,11 @@ function Player:new (game, world, x, y, name)
o.body:setFixedRotation(true)
-- Misc
o.name = name or "empty"
- o.sprite = newImage("assets/nauts/"..o.name..".png")
+ o:setSprite(newImage("assets/nauts/"..o.name..".png"))
o.world = game
o.punchcd = 0
-- Animation
- o.current = o.animations.idle
+ o.current = o.animations.default
o:createEffect("respawn")
-- Portrait load for first object created
if self.portrait_sprite == nil then
@@ -79,12 +78,6 @@ function Player:new (game, world, x, y, name)
return o
end
--- Destructor of `Player`
-function Player:delete()
- -- body deletion is handled by world deletion
- self.sprite = nil
-end
-
-- Control set managment
function Player:assignControlSet(set)
self.controlset = set
@@ -110,7 +103,7 @@ function Player:update(dt)
end
-- Salto
- if self.salto and (self.current == self.animations.walk or self.current == self.animations.idle) then
+ if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then
self.rotate = (self.rotate + 17 * dt * self.facing) % 360
elseif self.rotate ~= 0 then
self.rotate = 0
@@ -153,21 +146,7 @@ function Player:update(dt)
end
end
- -- # ANIMATIONS
- -- Animation
- self.delay = self.delay - dt
- if self.delay < 0 then
- self.delay = self.delay + Player.delay -- INITIAL from metatable
- -- Thank you De Morgan!
- if self.current.repeated or not (self.frame == self.current.frames) then
- self.frame = (self.frame % self.current.frames) + 1
- elseif isDown(controlset, "right") or isDown(controlset, "left") then
- -- If nonrepeatable animation is finished and player is walking
- self:changeAnimation("walk")
- elseif self.current == self.animations.damage then
- self:changeAnimation("idle")
- end
- end
+ Animated.update(self, dt)
-- # DEATH
-- We all die in the end.
@@ -240,7 +219,7 @@ function Player:controlpressed(set, action, key)
if (self.current == self.animations.attack) or
(self.current == self.animations.attack_up) or
(self.current == self.animations.attack_down) then
- self:changeAnimation("idle")
+ self:setAnimation("default")
end
-- Remove jump
self.jumpnumber = self.jumpnumber - 1
@@ -252,7 +231,7 @@ function Player:controlpressed(set, action, key)
(self.current ~= self.animations.attack) and
(self.current ~= self.animations.attack_up) and
(self.current ~= self.animations.attack_down) then
- self:changeAnimation("walk")
+ self:setAnimation("walk")
end
-- Punching
@@ -262,19 +241,19 @@ function Player:controlpressed(set, action, key)
if isDown(controlset, "up") then
-- Punch up
if self.current ~= self.animations.damage then
- self:changeAnimation("attack_up")
+ self:setAnimation("attack_up")
end
self:hit("up")
elseif isDown(controlset, "down") then
-- Punch down
if self.current ~= self.animations.damage then
- self:changeAnimation("attack_down")
+ self:setAnimation("attack_down")
end
self:hit("down")
else
-- Punch horizontal
if self.current ~= self.animations.damage then
- self:changeAnimation("attack")
+ self:setAnimation("attack")
end
if f == 1 then
self:hit("right")
@@ -299,7 +278,7 @@ function Player:controlreleased(set, action, key)
(isDown(controlset, "left") or isDown(controlset, "right")) and
self.current == self.animations.walk
then
- self:changeAnimation("idle")
+ self:setAnimation("default")
end
end
@@ -317,8 +296,7 @@ function Player:draw(offset_x, offset_y, scale, debug)
local draw_x = (math.floor(x) + offset_x) * scale
local draw_y = (math.floor(y) + offset_y) * scale
-- sprite draw
- love.graphics.setColor(255,255,255,255)
- love.graphics.draw(self.sprite, self.current[self.frame], draw_x, draw_y, self.rotate, self.facing*scale, 1*scale, 12, 15)
+ Animated.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, scale, 12, 15)
-- debug draw
if debug then
for _,fixture in pairs(self.body:getFixtureList()) do
@@ -358,11 +336,18 @@ function Player:drawHUD(x,y,scale,elevation)
end
-- Change animation of `Player`
--- idle, walk, attack, attack_up, attack_down, damage
-function Player:changeAnimation(animation)
- self.frame = 1
- self.delay = Player.delay -- INITIAL from metatable
- self.current = self.animations[animation]
+-- default, walk, attack, attack_up, attack_down, damage
+function Player:nextFrame()
+ local isDown = Controller.isDown
+ local controlset = self:getControlSet()
+ if self.current.repeated or not (self.frame == self.current.frames) then
+ self.frame = (self.frame % self.current.frames) + 1
+ elseif isDown(controlset, "right") or isDown(controlset, "left") then
+ -- If nonrepeatable animation is finished and player is walking
+ self:setAnimation("walk")
+ elseif self.current == self.animations.damage then
+ self:setAnimation("default")
+ end
end
-- Spawn `Effect` relative to `Player`
@@ -425,7 +410,7 @@ function Player:damage(direction)
local x,y = self.body:getLinearVelocity()
self.body:setLinearVelocity(x,0)
self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15)
- self:changeAnimation("damage")
+ self:setAnimation("damage")
self.combo = math.min(27, self.combo + 1)
self.punchcd = 0.08 + self.combo*0.006
self:playSound(2)
diff --git a/world.lua b/world.lua
index b01948e..30359fd 100644
--- a/world.lua
+++ b/world.lua
@@ -93,7 +93,7 @@ function World:loadMap(name)
self.map = map()
-- Platforms
for _,platform in pairs(self.map.platforms) do
- self:createPlatform(platform.x, platform.y, platform.shape, platform.sprite)
+ self:createPlatform(platform.x, platform.y, platform.shape, platform.sprite, platform.animations)
end
-- Decorations
for _,decoration in pairs(self.map.decorations) do
@@ -125,8 +125,8 @@ function World:getSpawnPosition()
end
-- Add new platform to the world
-function World:createPlatform(x, y, polygon, sprite)
- table.insert(self.Platforms, Ground:new(self, self.world, x, y, polygon, sprite))
+function World:createPlatform(x, y, polygon, sprite, animations)
+ table.insert(self.Platforms, Ground:new(self, self.world, x, y, polygon, sprite, animations))
end
-- Add new naut to the world
@@ -231,10 +231,16 @@ function World:update(dt)
self.world:update(dt)
-- Camera
self.camera:update(dt)
- -- Nauts
+ -- Engine world: Nauts, Grounds (kek) and Decorations - all Animateds (top kek)
for _,naut in pairs(self.Nauts) do
naut:update(dt)
end
+ for _,platform in pairs(self.Platforms) do
+ platform:update(dt)
+ end
+ for _,decoration in pairs(self.Decorations) do
+ decoration:update(dt)
+ end
-- Clouds
if self.map.clouds then
-- generator