docs
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful

This commit is contained in:
2026-02-21 23:53:36 +01:00
parent 3b137fd48e
commit 76964f872d
28 changed files with 301 additions and 28 deletions

View File

@@ -1,5 +1,7 @@
local _sprites = {}
--- Registers a sprite definition.
-- @param sprite_data table A table containing the sprite definition.
function Sprite.register(sprite_data)
if not sprite_data or not sprite_data.id then
trace("Error: Invalid sprite object registered (missing id)!")
@@ -11,8 +13,16 @@ function Sprite.register(sprite_data)
_sprites[sprite_data.id] = sprite_data
end
--- Schedules a sprite for drawing.
-- @param id string The unique identifier of the sprite.
-- @param x number The x-coordinate.
-- @param y number The y-coordinate.
-- @param[opt] colorkey number The color index for transparency.
-- @param[opt] scale number The scaling factor.
-- @param[opt] flip_x number Set to 1 to flip horizontally.
-- @param[opt] flip_y number Set to 1 to flip vertically.
-- @param[opt] rot number The rotation in degrees.
function Sprite.show(id, x, y, colorkey, scale, flip_x, flip_y, rot)
-- Ensure the sprite exists before attempting to show it
if not _sprites[id] then
trace("Error: Attempted to show non-registered sprite with id: " .. id)
return
@@ -30,28 +40,28 @@ function Sprite.show(id, x, y, colorkey, scale, flip_x, flip_y, rot)
}
end
--- Hides a displayed sprite.
-- @param id string The unique identifier of the sprite.
function Sprite.hide(id)
Context.sprites[id] = nil
end
--- Draws all scheduled sprites.
function Sprite.draw()
for id, params in pairs(Context.sprites) do
local sprite_data = _sprites[id]
if not sprite_data then
trace("Error: Sprite id " .. id .. " in Context.sprites is not registered.")
Context.sprites[id] = nil -- Clean up invalid entry
-- We should probably continue to the next sprite instead of returning
-- so that other valid sprites can still be drawn.
Context.sprites[id] = nil
end
-- Use parameters from Context.sprites, or fall back to sprite_data, then to defaults
local colorkey = params.colorkey or sprite_data.colorkey or 0
local scale = params.scale or sprite_data.scale or 1
local flip_x = params.flip_x or sprite_data.flip_x or 0
local flip_y = params.flip_y or sprite_data.flip_y or 0
local rot = params.rot or sprite_data.rot or 0
if sprite_data.sprites then -- Complex sprite
if sprite_data.sprites then
for i = 1, #sprite_data.sprites do
local sub_sprite = sprite_data.sprites[i]
spr(
@@ -65,7 +75,7 @@ function Sprite.draw()
sub_sprite.rot or rot
)
end
else -- Simple sprite
else
spr(sprite_data.s, params.x, params.y, colorkey, scale, flip_x, flip_y, rot)
end
end

View File

@@ -1,17 +1,17 @@
Sprite.register({
id = "norman",
sprites = {
-- Body
-- Body (sprite index 0)
{ s = 0, x_offset = 0, y_offset = 0 },
-- Head
-- Head (sprite index 1)
{ s = 1, x_offset = 0, y_offset = -8 },
-- Left Arm
-- Left Arm (sprite index 2)
{ s = 2, x_offset = -4, y_offset = 4 },
-- Right Arm
-- Right Arm (sprite index 3, flipped)
{ s = 3, x_offset = 4, y_offset = 4, flip_x = 1 }, -- Flipped arm
-- Left Leg
-- Left Leg (sprite index 4)
{ s = 4, x_offset = -2, y_offset = 8 },
-- Right Leg
-- Right Leg (sprite index 5, flipped)
{ s = 5, x_offset = 2, y_offset = 8, flip_x = 1 } -- Flipped leg
}
})
})