summaryrefslogtreecommitdiffhomepage
path: root/not
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-04-03 19:16:42 +0200
committerAki <nthirtyone@gmail.com>2017-04-03 19:16:42 +0200
commit33865425828c92bb1cf97dee8d0b0f51f9b01042 (patch)
tree6e16c1085a2cc77bf667ed037de75ec695a4333a /not
parente2e4164cda3b4536fcba96f5c5283eb125d2b6b1 (diff)
downloadroflnauts-33865425828c92bb1cf97dee8d0b0f51f9b01042.zip
roflnauts-33865425828c92bb1cf97dee8d0b0f51f9b01042.tar.gz
roflnauts-33865425828c92bb1cf97dee8d0b0f51f9b01042.tar.bz2
Effect is now child of Decoration. Uses Sprite methods and Decoration positioning
Diffstat (limited to 'not')
-rw-r--r--not/Effect.lua92
1 files changed, 34 insertions, 58 deletions
diff --git a/not/Effect.lua b/not/Effect.lua
index 7010946..1bf9ed7 100644
--- a/not/Effect.lua
+++ b/not/Effect.lua
@@ -1,70 +1,46 @@
--- `Effect`
+--- `Effect`
-- Short animation with graphics that plays in various situation.
-
--- Metatable of `Effect`
--- nils initialized in constructor
--- TODO: inherit from `not.Sprite`.
--- TODO: clean-up and reformat code, see newer code for reference.
+-- TODO: animation is currently slower than it used to be, check if it is ok; if not then make it possible to change it to 0.06 delay.
Effect = {
- x = 0,
- y = 0,
- delay = 0.06,
- initial = nil,
- frame = 1,
- animation = nil,
- sprite = nil,
- quads = require "effects"
+ finished = false,
}
--- Construct of `Effect`
-function Effect:new(name, x, y)
- -- Meta
- local o = {}
- setmetatable(o, self)
- self.__index = self
- -- Load spritesheet to metatable if not yet loaded
- if self.sprite == nil then
- self.sprite = love.graphics.newImage("assets/effects.png")
+-- `Effect` is a child of `Decoration`.
+require "not.Decoration"
+Effect.__index = Effect
+setmetatable(Effect, Decoration)
+
+-- Constructor of `Effect`.
+function Effect:new (name, x, y)
+ local o = setmetatable({}, self)
+ o:init(name, x, y)
+ -- Load spritesheet statically.
+ if self:getImage() == nil then
+ self:setImage(Sprite.newImage("assets/effects.png"))
end
- -- Init
- o.initial = o.delay
- o.animation = name
- o.x = x or self.x
- o.y = y or self.y
return o
end
--- Position
-function Effect:getPosition()
- return self.x, self.y
+-- Initializator of `Effect`.
+function Effect:init (name, x, y)
+ Decoration.init(self, x, y, nil)
+ self:setAnimationsList(require("effects"))
+ self:setAnimation(name)
end
--- Animation and return flag for deletion after completion
--- returns true if completed and ready to delete
-function Effect:update(dt)
- self.delay = self.delay - dt
- if self.delay < 0 then
- if self.frame < self.quads[self.animation].frames then
- self.frame = self.frame + 1
- self.delay = self.delay + self.initial
- else
- return true -- delete
- end
- end
- return false
+-- Update of `Effect`.
+-- Returns true if animation is finished and effect is ready to be deleted.
+function Effect:update (dt)
+ Decoration.update(self, dt)
+ return self.finished
end
--- Draw me with scale and offsets, senpai
-function Effect:draw(offset_x, offset_y, scale)
- -- locals
- local offset_x = offset_x or 0
- local offset_y = offset_y or 0
- local scale = scale or 1
- local x, y = self:getPosition()
- -- pixel grid
- 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, self.quads[self.animation][self.frame], draw_x, draw_y, 0, scale, scale)
-end \ No newline at end of file
+-- Overridden from `not.Sprite`.
+-- Sets finished flag if reached last frame of played animation.
+function Effect:goToNextFrame ()
+ if not (self.frame == self.current.frames) then
+ self.frame = (self.frame % self.current.frames) + 1
+ else
+ self.finished = true
+ end
+end