66 lines
1.7 KiB
Lua
66 lines
1.7 KiB
Lua
local data = require("data")
|
|
local pprint = require("lib.pprint")
|
|
local Sprite = {}
|
|
|
|
-- TODO: Maybe add a texture atlas library for packing frame data
|
|
-- TODO: For production maybe use another type of loader? (https://github.com/elloramir/packer, https://github.com/EngineerSmith/Runtime-TextureAtlas)
|
|
|
|
function Sprite:addToWorld(group, e)
|
|
if group ~= "sprite" then return end
|
|
|
|
local sprite = data.sprites[e.sprite.variant]
|
|
assert(sprite, ("Attempt to draw unknown sprite: %s"):format(e.sprite.name))
|
|
end
|
|
|
|
function Sprite:update(dt)
|
|
for _, e in ipairs(self.pool.groups.sprite.entities) do
|
|
local sprite = data.sprites[e.sprite.name]
|
|
if sprite then
|
|
local variant = sprite.variants[e.sprite.variant or "default"]
|
|
e.sprite.timer = (e.sprite.timer or 0) + dt
|
|
|
|
if variant then
|
|
local frame = variant[e.sprite.frame]
|
|
if not frame then
|
|
frame = variant[1]
|
|
end
|
|
if e.sprite.timer > frame.duration then
|
|
e.sprite.timer = e.sprite.timer % 0.1
|
|
e.sprite.frame = (e.sprite.frame or 1) % #variant + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function Sprite:draw()
|
|
love.graphics.setColor(1, 1, 1)
|
|
for _, e in ipairs(self.pool.groups.sprite.entities) do
|
|
local sprite = data.sprites[e.sprite.name]
|
|
assert(sprite, ("Attempt to draw unknown sprite: %s"):format(e.sprite.name))
|
|
|
|
if not e.hidden then
|
|
local variant = sprite.variants[e.sprite.variant or "default"]
|
|
if variant and e.sprite.frame then
|
|
local frame = variant[e.sprite.frame]
|
|
if not frame then
|
|
frame = variant[1]
|
|
end
|
|
local sx = 1
|
|
if e.sprite.flip then
|
|
sx = -1
|
|
end
|
|
love.graphics.draw(
|
|
frame.image,
|
|
e.pos.x,
|
|
e.pos.y,
|
|
0, sx, 1,
|
|
sprite.width/2, sprite.height/2
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return Sprite
|