diff --git a/main.lua b/main.lua index 3f9b65f..b7881ee 100644 --- a/main.lua +++ b/main.lua @@ -3,6 +3,7 @@ local Animator = require "animator" local Atlas = require "atlas" local Font = require "font" local MultiMenu = require "multimenu" +local Units = require "units" local push = require "lib/push" local gameWidth, gameHeight = 480, 360 @@ -49,37 +50,69 @@ function love.load() local options = {} - for name, state in pairs(states) do + for idx, unit in ipairs(Units) do + local state_menu = {} + + for name, state in pairs(unit.states) do + state_menu[#state_menu + 1] = { + text = name, + callback = function() ActiveUnit:setState(name) end + } + end + options[#options + 1] = { - text = name, - callback = function() LeafRanger:setState(name) end, + text = unit.name, + callback = function() StateMenu = MultiMenu(BlockyFont, state_menu) end, } end - Menu = MultiMenu(BlockyFont, options) + UnitMenu = MultiMenu(BlockyFont, options) + ActiveUnit = Animator(Units[1].states, "idle") end function love.draw() push:start() - Menu:draw() BlockyFont:draw("Hello, world!") - LeafRanger:draw(0, 0) + ActiveUnit:draw() + + if StateMenu then + StateMenu:draw() + else + UnitMenu:draw() + end push:finish() end function love.update(dt) - LeafRanger:update(dt) + ActiveUnit:update(dt) end function love.keypressed(key) + local menu + + if StateMenu then + menu = StateMenu + else + menu = UnitMenu + end + + local changed = false if key == "up" then - Menu:up() + menu:up() + changed = true elseif key == "down" then - Menu:down() + menu:down() + changed = true elseif key == "space" then - Menu:select() + menu:select() + elseif key == "escape" then + StateMenu = nil + end + + if changed and not StateMenu then + ActiveUnit = Animator(Units[UnitMenu.selected].states, "idle") end end diff --git a/units.lua b/units.lua new file mode 100644 index 0000000..f88f5b1 --- /dev/null +++ b/units.lua @@ -0,0 +1,22 @@ +local Animation = require "animation" +local Atlas = require "atlas" + +local function spannedState(atlas, row, len, on_finish, next) + local start = (row - 1) * atlas.w + 1 + local animation = Animation:new_spanned(atlas, start, start + len - 1) + return { animation = animation, on_finish = on_finish, next = next } +end + +local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128) + +return { + { + name = "Leaf Ranger", + states = { + idle = spannedState(leafRanger, 1, 12), + run = spannedState(leafRanger, 2, 10), + hurt = spannedState(leafRanger, 16, 6, "goto", "idle"), + death = spannedState(leafRanger, 17, 19, "stop"), + } + } +}