diff --git a/.luacheckrc b/.luacheckrc index f16500d..2ecf4d8 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -6,6 +6,7 @@ globals = { "Decision", "Situation", "Screen", + "Sprite", "UI", "Print", "Input", @@ -19,6 +20,7 @@ globals = { "keyp", "music", "sfx", + "spr", "rect", "rectb", "circ", diff --git a/impostor.inc b/impostor.inc index 44ae875..a254b6a 100644 --- a/impostor.inc +++ b/impostor.inc @@ -5,6 +5,8 @@ init/init.minigames.lua init/init.meters.lua system/system.util.lua init/init.windows.lua +sprite/sprite.manager.lua +sprite/sprite.norman.lua situation/situation.manager.lua situation/situation.drink_coffee.lua decision/decision.manager.lua diff --git a/inc/init/init.context.lua b/inc/init/init.context.lua index 0d7ae1e..cdc133a 100644 --- a/inc/init/init.context.lua +++ b/inc/init/init.context.lua @@ -45,7 +45,9 @@ on than meets the eye.]] minigame_ddr = Minigames.get_default_ddr(), minigame_button_mash = Minigames.get_default_button_mash(), minigame_rhythm = Minigames.get_default_rhythm(), - meters = Meters.get_initial() + meters = Meters.get_initial(), + sprites = {}, + current_situation = nil, } end diff --git a/inc/init/init.modules.lua b/inc/init/init.modules.lua index 58d56e6..77b4817 100644 --- a/inc/init/init.modules.lua +++ b/inc/init/init.modules.lua @@ -18,5 +18,5 @@ Map = {} UI = {} Print = {} Input = {} - +Sprite = {} Audio = {} diff --git a/inc/situation/situation.drink_coffee.lua b/inc/situation/situation.drink_coffee.lua index d791d0e..16cf71e 100644 --- a/inc/situation/situation.drink_coffee.lua +++ b/inc/situation/situation.drink_coffee.lua @@ -2,5 +2,6 @@ Situation.register({ id = "drink_coffee", handle = function() Audio.sfx_select() + Sprite.show("norman", 100, 100) end, }) diff --git a/inc/situation/situation.manager.lua b/inc/situation/situation.manager.lua index dfc8abf..88c6b2d 100644 --- a/inc/situation/situation.manager.lua +++ b/inc/situation/situation.manager.lua @@ -31,6 +31,7 @@ function Situation.apply(id) return end + Context.current_situation = id situation.handle() end diff --git a/inc/sprite/sprite.manager.lua b/inc/sprite/sprite.manager.lua new file mode 100644 index 0000000..60d5a97 --- /dev/null +++ b/inc/sprite/sprite.manager.lua @@ -0,0 +1,72 @@ +local _sprites = {} + +function Sprite.register(sprite_data) + if not sprite_data or not sprite_data.id then + trace("Error: Invalid sprite object registered (missing id)!") + return + end + if _sprites[sprite_data.id] then + trace("Warning: Overwriting sprite with id: " .. sprite_data.id) + end + _sprites[sprite_data.id] = sprite_data +end + +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 + end + + Context.sprites[id] = { + id = id, + x = x, + y = y, + colorkey = colorkey, + scale = scale, + flip_x = flip_x, + flip_y = flip_y, + rot = rot, + } +end + +function Sprite.hide(id) + Context.sprites[id] = nil +end + +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. + 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 + for i = 1, #sprite_data.sprites do + local sub_sprite = sprite_data.sprites[i] + spr( + sub_sprite.s, + params.x + (sub_sprite.x_offset or 0), + params.y + (sub_sprite.y_offset or 0), + sub_sprite.colorkey or colorkey, + sub_sprite.scale or scale, + sub_sprite.flip_x or flip_x, + sub_sprite.flip_y or flip_y, + sub_sprite.rot or rot + ) + end + else -- Simple sprite + spr(sprite_data.s, params.x, params.y, colorkey, scale, flip_x, flip_y, rot) + end + end +end diff --git a/inc/sprite/sprite.norman.lua b/inc/sprite/sprite.norman.lua new file mode 100644 index 0000000..67cfbcb --- /dev/null +++ b/inc/sprite/sprite.norman.lua @@ -0,0 +1,17 @@ +Sprite.register({ + id = "norman", + sprites = { + -- Body + { s = 0, x_offset = 0, y_offset = 0 }, + -- Head + { s = 1, x_offset = 0, y_offset = -8 }, + -- Left Arm + { s = 2, x_offset = -4, y_offset = 4 }, + -- Right Arm + { s = 3, x_offset = 4, y_offset = 4, flip_x = 1 }, -- Flipped arm + -- Left Leg + { s = 4, x_offset = -2, y_offset = 8 }, + -- Right Leg + { s = 5, x_offset = 2, y_offset = 8, flip_x = 1 } -- Flipped leg + } +}) diff --git a/inc/window/window.game.lua b/inc/window/window.game.lua index 766537b..85b9375 100644 --- a/inc/window/window.game.lua +++ b/inc/window/window.game.lua @@ -14,6 +14,7 @@ function GameWindow.draw() UI.draw_decision_selector(available_decisions, Context.selected_decision_index) end end + Sprite.draw() end function GameWindow.update()