Skip to content

Instantly share code, notes, and snippets.

@mebens mebens/gist:4035772
Created Nov 8, 2012

Embed
What would you like to do?
Spritemap that works with Ammo
Spritemap = class("Spritemap")
Spritemap._mt = {}
function Spritemap._mt:__index(key)
return rawget(self, "_" .. key) or self.class.__instanceDict[key]
end
Spritemap:enableAccessors()
function Spritemap:initialize(img, fw, fh, complete, ...)
self.complete = complete
self.frame = 1
self._image = type(img) == "string" and love.graphics.newImage(img) or img
self._width = self._image:getWidth()
self._height = self._image:getHeight()
self._columns = math.floor(self._width / fw)
self._rows = math.floor(self._height / fh)
self._quads = {}
self._animations = {}
self._completeArgs = { ... }
for x = 0, self._columns - 1 do
for y = 0, self._rows - 1 do
self._quads[#self._quads + 1] = love.graphics.newQuad(x * fw, y * fh, fw, fh, self._width, self._height)
end
end
self:stop()
self:applyAccessors()
end
function Spritemap:update(dt)
if self.playing then
local anim = self._animations[self._playing]
if self._timer > anim.time then
self._timer = self._timer - anim.time
if self._animIndex == #anim.frames and not anim.loop then
self.complete(unpack(self._completeArgs))
self:stop()
else
self._animIndex = self._animIndex % #anim.frames + 1
end
else
self._timer = self._timer + dt
end
end
end
function Spritemap:draw(x, y, r, sx, sy, ox, oy, kx, ky)
love.graphics.drawq(self._image, self._quads[self.frame], x, y, r, sx, sy, ox, oy, kx, ky)
end
function Spritemap:add(name, frames, rate, loop)
self._animations[name] = { frames = frames, loop = loop, time = 1 / rate }
end
function Spritemap:play(name)
self._playing = name
end
function Spritemap:stop()
self._playing = nil
self._animIndex = 1
self._timer = 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.