Add weapon charge UI.

This commit is contained in:
Anna Rose Wiggins 2023-09-30 03:15:36 -04:00
parent a5bb3744b5
commit 032cc0a2ed
3 changed files with 89 additions and 11 deletions

View file

@ -8,6 +8,9 @@ import "ui"
local gfx <const> = playdate.graphics
local BASE_CHARGE_FACTOR <const> = 15
local BASE_WEAPON_CHARGE_SPEED <const> = 1000
-- the amount of charge needed to increase the shot power by 1 at each power level
local WEAPON_CHARGE_LOOKUP <const> = {
[0]=5,
@ -16,9 +19,6 @@ local WEAPON_CHARGE_LOOKUP <const> = {
[3]=50
}
-- maximum value the reserve charge can reach
local MAX_CHARGE <const> = 100
class("Kani").extends(gfx.sprite)
function Kani:init(ui)
@ -30,14 +30,22 @@ function Kani:init(ui)
self:setGroupMask(0x2)
self:setCollidesWithGroupsMask(0x19)
-- stats
self.health = 100
self.reserveCharge = 100
-- controls the speed the crank recharges the player's reserves. A lower number allows faster charging.
-- should never be set to 0
self.chargeFactor = BASE_CHARGE_FACTOR
-- weapon variables
self.weaponPower = 0
self.firingMode = false
self.weaponChargeSpeed = BASE_WEAPON_CHARGE_SPEED -- speed in ms between weapon charge levels. Level 2 always takes half this time.
-- the UI gets passed in to Kani so that we can update pieces of it easily.
self.ui = ui
-- input handlers
self.inputHandlers = {
upButtonDown = function() self.vector.y = -3 end,
downButtonDown = function() self.vector.y = 3 end,
@ -49,7 +57,9 @@ function Kani:init(ui)
rightButtonUp = function() self.vector.x = 0 end,
cranked = function(change, accelChange) self:chargeReserve(change) end,
AButtonDown = function()
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(500, 1000, self.chargeShot, self)
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(self.weaponChargeSpeed / 2,
self.weaponChargeSpeed,
self.chargeShot, self)
end,
AButtonUp = function() self:fire() end,
}
@ -57,7 +67,7 @@ end
function Kani:chargeReserve(change)
if change <= 0 then return end
self.reserveCharge = math.min(self.reserveCharge + change / 15, 100)
self.reserveCharge = math.min(self.reserveCharge + change / self.chargeFactor, 100)
self.ui.chargeMeter:setValue(self.reserveCharge)
end
@ -76,8 +86,8 @@ function Kani:chargeShot()
if effectiveCharge >= requiredPower then
self.reserveCharge = effectiveCharge - requiredPower
self.weaponPower += 1
print("Shot charged to size " .. self.weaponPower)
self.ui.chargeMeter:setValue(self.reserveCharge)
self.ui.weaponPowerMeter:setValue(self.weaponPower-1)
end
end
@ -88,11 +98,20 @@ function Kani:fire()
return
end
print("Firing bullet of size " .. self.weaponPower)
local bullet = Bullet(self.weaponPower)
bullet:moveTo(self.x+16, self.y)
bullet:add()
self.weaponPower = 0
self.ui.weaponPowerMeter:setValue(0)
end
function Kani:damage(amount)
self.health = math.max(self.health - amount, 0)
if self.health <= 0 then
-- TODO: destroy ship
end
self.ui.healthMeter:setValue(self.health)
end
function Kani:addInputHandlers()
@ -107,6 +126,6 @@ end
function Kani:update()
local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y))
for i=0, #collisions, 1 do
-- handle collisions
-- handle player-triggered collisions
end
end