Skip to content

Instantly share code, notes, and snippets.

@ripter
Created December 14, 2012 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripter/4287065 to your computer and use it in GitHub Desktop.
Save ripter/4287065 to your computer and use it in GitHub Desktop.
AnimationSheet for Löve2D and Zwoptex.
--[[
Animation Sheet
Allows you to set up several animations from a single animation sheet and play them.
This wil only play one animation at a time.
By Chris Richards
--]]
module('AnimSheet', package.seeall)
AnimSheet = {
anims = {}
, pos = 1
, currentFrames = nil
, timer = 0
, delay
, shouldStop = false
, state = 'run'
}
AnimSheet.__index = AnimSheet
function AnimSheet:new(sheetName)
local as = require(sheetName)
setmetatable(as, AnimSheet)
return as
end
-- Adds a new animation set
-- name: the name used in set
-- delay: deplay between frames
-- sequence: list of frame indexes (in order) to use for the animation
-- stop: if true, animation will stop when it reaches the end. Default false
function AnimSheet:addAnim(name, delay, sequence, stop)
local frames = {}
self.shouldStop = stop or false
for i=1, #sequence do
local index = sequence[i]
frames[i] = self.frames[index].quad
end
self.anims[name] = {
delay = delay
, frames = frames
}
end
-- Sets the animation to use on draw
-- Also resets the animation position
function AnimSheet:set(name)
self:reset()
self.currentFrames = self.anims[name].frames
self.delay = self.anims[name].delay
end
-- stops the animation
-- does not reset the position
function AnimSheet:stop()
self.state = 'stop'
end
-- starts the animation
-- does not reset the position
function AnimSheet:start()
self.state = 'run'
end
-- returns true if the animation is currently running
function AnimSheet:isRunning()
return self.state == 'run'
end
-- resets the animation position
function AnimSheet:reset()
self.pos = 1
end
-- Draws the set animation
function AnimSheet:draw(x, y, r, sx, sy, ox, oy, kx, ky)
local quad = self.currentFrames[ self.pos ]
love.graphics.drawq(self.image, quad, x, y, r, sx, sy, ox, oy, kx, ky)
end
function AnimSheet:update(dt)
self.timer = self.timer + dt
if self.timer > self.delay then
self.timer = 0
if self.pos == #self.currentFrames then
-- if we don't need to stop, then just reset the position back to 1
if not self.shouldStop then
self.pos = 1
else
self:stop()
end
else
self.pos = self.pos + 1
end
end
end
return AnimSheet
module('{{ metadata.target.textureFileName }}', package.seeall)
-- This file is for use with Löve2d and was generated by Zwoptex (http://zwoptexapp.com/)
--
-- This was written to be used with the AnimSheet lua object.
-- Format written by Chris Richards.
local fileName = '{{ metadata.target.textureFileName}}{{ metadata.target.textureFileExtension }}'
local sheet = {
image = love.graphics.newImage(fileName)
, frames = {
{% for sprite in spritesAndAliases %}
{
quad = love.graphics.newQuad({{ sprite.textureRectX }}, {{ sprite.textureRectY }}, {{ sprite.textureRectWidth }}, {{ sprite.textureRectHeight }}, {{ metadata.sizeWidth }}, {{ metadata.sizeHeight }})
, rect = { x = {{ sprite.textureRectX }}, y = {{ sprite.textureRectY }}, width = {{ sprite.textureRectWidth }}, height = {{ sprite.textureRectHeight }} }
},
{% /for %}
}
}
return sheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment