--# Main
function setup()
braid = animation("Dropbox:braid", 130, 150)
end
function draw()
background(136, 188, 56, 255)
braid:play()
end
--# Animation
animation = class()
function animation:init(spritesheet, width, height, ...)
self.atlas = mesh()
self.mask = self.atlas:addRect(0, 0, 0, 0)
self.img = type(spritesheet) == "string" and readImage(spritesheet) or spritesheet
self.atlas.texture = self.img
self.width = width
self.height = height
self.cols = self.img.width / self.width
self.rows = self.img.height / self.height
self.fps = type(arg[1]) == "number" and arg[1] or 24
self.frames = type(arg[1]) == "table" and arg[1] or (type(arg[2]) == "table" and arg[2] or {})
self.mode = { -- spriteMode() equivalent
[0] = { -- CORNER
pos = vec2(self.width/2, self.height/2),
size = vec2(self.width, self.height)
},
[1] = { -- CORNERS
pos = vec2(self.width/2, self.height/2),
size = vec2(self.width, self.height)
},
[2] = { -- CENTER
pos = vec2(0, 0),
size = vec2(self.width, self.height)
},
[3] = { -- RADIUS
pos = vec2(0, 0),
size = vec2(self.width*2, self.height*2)
}
}
local frameset = {}
for row = 1, self.rows do
for col = 1, self.cols do
table.insert(frameset, vec2(col, row))
end
end
if #self.frames == 0 then
self.frames = frameset
else
local cache = self.frames
self.frames = {}
for i = 1, #cache do
local x, y = cache[i].x, cache[i].y
if x < 1 or y < 1 then
local fillin = false
for n, vec in ipairs(frameset) do
if fillin == true then
table.insert(self.frames, vec)
end
if not self.frames[#self.frames] then
table.insert(self.frames, vec2(1,1))
end
if vec == self.frames[#self.frames] then fillin = true end
if vec == vec2(math.abs(x), math.abs(y)) then fillin = false end
end
else
table.insert(self.frames, cache[i])
end
end
end
end
function animation:play()
self.timer = self.timer or ElapsedTime
self.frame = self.frame or 1
local u, v = 1/self.cols, 1/self.rows
local mode = spriteMode()
if (ElapsedTime-self.timer) > (1/self.fps) then
self.frame = self.frames[self.frame+1] and (self.frame+1) or 1
self.timer = nil
end
self.atlas:setRect(self.mask, self.mode[mode].pos.x, self.mode[mode].pos.y, self.mode[mode].size.x, self.mode[mode].size.y)
self.atlas:setRectTex(self.mask, u*(self.frames[self.frame].x-1), v*(self.rows-self.frames[self.frame].y), u, v)
self.atlas:draw()
end
braid spritesheet: https://www.dropbox.com/s/5xb1oe5r8x54qfb/braid_130x150.png?dl=0