summaryrefslogtreecommitdiffhomepage
path: root/not/Sprite.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-03-16 19:05:50 +0100
committerAki <nthirtyone@gmail.com>2017-03-16 19:05:50 +0100
commit84278ed41f61c586dbb38dd99c45ee33e2f58c77 (patch)
treed7661f79901f40464b0b39a9bc5de6f4d5191b3b /not/Sprite.lua
parentbd22a09dc862ac5480426bfd5e0311a97b6cfa77 (diff)
downloadroflnauts-84278ed41f61c586dbb38dd99c45ee33e2f58c77.zip
roflnauts-84278ed41f61c586dbb38dd99c45ee33e2f58c77.tar.gz
roflnauts-84278ed41f61c586dbb38dd99c45ee33e2f58c77.tar.bz2
Moved ? -> not.?; Renamed Player -> Hero
Diffstat (limited to 'not/Sprite.lua')
-rw-r--r--not/Sprite.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/not/Sprite.lua b/not/Sprite.lua
new file mode 100644
index 0000000..1cc46f7
--- /dev/null
+++ b/not/Sprite.lua
@@ -0,0 +1,81 @@
+-- `Sprite`
+-- Abstract class for drawable animated entities.
+
+-- Metatable
+Sprite = {
+ animations--[[table with animations]],
+ current--[[animations.default]],
+ image--[[love.graphics.newImage()]],
+ frame = 1,
+ delay = .1,
+}
+Sprite.__index = Sprite
+
+-- Cleans up reference to image on deletion.
+function Sprite:delete()
+ self.image = nil
+end
+
+-- Sets an Image as a image.
+function Sprite:setImage(image)
+ self.image = image
+end
+-- Returns current image Image.
+function Sprite:getImage()
+ return self.image
+end
+
+-- Sets new animations list.
+function Sprite:setAnimationsList(t)
+ if t then
+ self.animations = t
+ self:setAnimation("default")
+ end
+end
+
+-- Sets current animation by table key.
+function Sprite:setAnimation(animation)
+ self.frame = 1
+ self.delay = Sprite.delay -- INITIAL from metatable
+ self.current = self.animations[animation]
+end
+-- Returns current animation table.
+function Sprite:getAnimation()
+ return self.current
+end
+
+-- Get frame quad for drawing.
+function Sprite: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 image.
+function Sprite:draw(...)
+ local s, q = self:getImage(), 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 Sprite:update(dt)
+ if self.animations and self.current then
+ self.delay = self.delay - dt
+ if self.delay < 0 then
+ self.delay = self.delay + Sprite.delay -- INITIAL from metatable
+ self:nextFrame()
+ end
+ end
+end
+-- Moving to the next frame.
+function Sprite: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