Animation = {} function Animation:New(anim_data) local o = {} o.path = anim_data.path o.frames = anim_data.frames o.speed = anim_data.speed o.imgs = anim_data.imgs o.subframe = 0 o.frame = 1 setmetatable(o, self) self.__index = self return o end function Animation:ChangeTo(anim_data) if anim_data.path == self.path then return self else return Animation:New(anim_data) end end -- to manually handle what frame function Animation:DrawFrame(frame, x, y, rotate, sx, sy) if frame > self.frames then frame = self.frames end local x = x or 0 local y = y or 0 local sx = sx or 1 local sy = sy or 1 love.graphics.draw( self.imgs[frame], math.floor(x - Camera.pos.x), math.floor(y - Camera.pos.y), rotate, sx, sy ) end -- to linearly animate function Animation:Animate() if self.speed ~= 0 then -- try to animate self.subframe = self.subframe + current_dt if self.subframe > self.speed then self.frame = self.frame + 1 self.subframe = self.subframe - self.speed end -- cycle if self.frame >= self.frames+1 then self.frame = self.frame - self.frames end end end -- to draw the current frame function Animation:Draw(x, y, rotate, sx, sy) local x = x or 0 local y = y or 0 local sx = sx or 1 local sy = sy or 1 love.graphics.draw( self.imgs[self.frame], math.floor(x), math.floor(y), rotate, sx, sy ) end