Fix defend state, add animation tables (not used quite yet), and add onExit functions to the state machine.

This commit is contained in:
Anna Rose Wiggins 2023-10-04 01:19:05 -04:00
parent 9a26eb4b3a
commit feb7541944
4 changed files with 99 additions and 47 deletions

View file

@ -69,25 +69,7 @@ function Kani:init(ui)
-- the UI gets passed in to Kani so that we can update pieces of it easily.
self.ui = ui
self.inputHandlers = {
upButtonDown = function() self.vector.dy = -3 end,
downButtonDown = function() self.vector.dy = 3 end,
leftButtonDown = function() self.vector.dx = -3 end,
rightButtonDown = function() self.vector.dx = 3 end,
upButtonUp = function() self.vector.dy = 0 end,
downButtonUp = function() self.vector.dy = 0 end,
leftButtonUp = function() self.vector.dx = 0 end,
rightButtonUp = function() self.vector.dx = 0 end,
cranked = function(change, accelChange) self:chargeReserve(change) end,
AButtonDown = function()
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(self.weaponChargeSpeed / 2,
self.weaponChargeSpeed,
self.chargeShot, self)
end,
AButtonUp = function() self:fire() end,
}
self.fsm:changeState("READY")
self.fsm:addState("DEFEND", self.runDefend, self.onDefend, self.onDefendExit)
end
function Kani:chargeReserve(change)
@ -138,24 +120,65 @@ end
function Kani:damage(amount)
Kani.super.damage(self, amount)
self.ui.healthMeter:setValue(self.health)
print("Kani health now at " .. self.health) -- debug
if self.health == 0 then
-- TODO: end game here
end
end
function Kani:addInputHandlers()
playdate.inputHandlers.push(self.inputHandlers)
end
function Kani:removeInputHandlers()
playdate.inputHandlers.pop()
end
-- move that crab!
function Kani:runReady()
-- input handling
if playdate.buttonIsPressed(playdate.kButtonDown) then
self.vector.dy = 3
elseif playdate.buttonIsPressed(playdate.kButtonUp) then
self.vector.dy = -3
else
self.vector.dy = 0
end
if playdate.buttonIsPressed(playdate.kButtonLeft) then
self.vector.dx = -3
elseif playdate.buttonIsPressed(playdate.kButtonRight) then
self.vector.dx = 3
else
self.vector.dx = 0
end
local change = select(1, playdate.getCrankChange())
self:chargeReserve(change)
if playdate.buttonJustPressed(playdate.kButtonA) then
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(self.weaponChargeSpeed / 2,
self.weaponChargeSpeed,
self.chargeShot, self)
end
if playdate.buttonJustReleased(playdate.kButtonA) then
self:fire()
end
if playdate.buttonIsPressed(playdate.kButtonB) then
print("Triggering entrance to DEFEND mode")
self.fsm:changeState("DEFEND")
end
-- collision handling
local collisions = Kani.super.runReady(self)
for i=0, #collisions, 1 do
-- TODO: handle player-triggered collisions
end
end
function Kani:onDefend()
self.armor = 5
end
function Kani:onDefendExit()
self.armor = 0
end
function Kani:runDefend()
if not playdate.buttonIsPressed(playdate.kButtonB) then
self.fsm:changeState("READY")
end
end