summaryrefslogtreecommitdiffhomepage
path: root/animated.lua
diff options
context:
space:
mode:
Diffstat (limited to 'animated.lua')
-rw-r--r--animated.lua43
1 files changed, 30 insertions, 13 deletions
diff --git a/animated.lua b/animated.lua
index c698872..236b68d 100644
--- a/animated.lua
+++ b/animated.lua
@@ -1,42 +1,59 @@
-- `Animated`
--- Abstract class for animated entities.
+-- Abstract class for drawable animated entities.
-- Metatable
Animated = {
- animations = require "animations",
- current--[[animations.idle]],
+ animations--[[table with animations]],
+ current--[[animations.default]],
+ sprite--[[love.graphics.newImage()]],
frame = 1,
- delay = .1
+ delay = .1,
}
Animated.__index = Animated
-Animated.current = Animated.animations.idle
--- setAnimation(self, animation)
+-- 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 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
-
--- getAnimation(self)
+-- Returns current animation table.
function Animated:getAnimation()
return self.current
end
--- animate(self, dt)
-function Animated:animate(dt)
+-- Get frame quad for drawing.
+function Animated:getQuad()
+ return self.current[self.frame]
+end
+
+-- Drawing self to LOVE2D buffer.
+function Animated:draw(...)
+ love.graphics.draw(self:getSprite(), self:getQuad(), ...)
+end
+
+-- Animation updating.
+function Animated:update(dt)
self.delay = self.delay - dt
if self.delay < 0 then
self.delay = self.delay + Animated.delay -- INITIAL from metatable
self:nextFrame()
end
end
-
--- nextFrame(self)
+-- 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("idle")
+ self:setAnimation("default")
end
end \ No newline at end of file