Fix up collision code and improve bullet semantics.

This commit is contained in:
Anna Rose Wiggins 2023-09-29 22:30:01 -04:00
parent 5d1c36ea2f
commit b222ce3d67
3 changed files with 44 additions and 17 deletions

View file

@ -7,8 +7,14 @@ import "bullet"
local gfx <const> = playdate.graphics
-- the amount of charge needed to increase the shot size by 1
local SHOT_INCREMENT <const> = 10
-- the amount of charge needed to increase the shot power by 1 at each power level
local WEAPON_CHARGE_LOOKUP <const> = {
[0]=5,
[1]=15,
[2]=30,
[3]=49
}
-- maximum value the reserve charge can reach
local MAX_CHARGE <const> = 100
@ -24,7 +30,7 @@ function Kani:init()
self:setCollidesWithGroupsMask(0x19)
self.reserveCharge = 100
self.shotCharge = 0
self.weaponPower = 0
self.firingMode = false
-- input handlers
@ -39,37 +45,42 @@ function Kani:init()
rightButtonUp = function() self.vector.x = 0 end,
cranked = function(change, accelChange) self:chargeReserve(change) end,
AButtonDown = function()
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(0, 500, self.chargeShot, self)
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(500, 1000, self.chargeShot, self)
end,
AButtonUp = function() self:fire() end,
}
end
function Kani:chargeReserve(change)
if change < 0 then return end
self.reserveCharge = math.min(self.reserveCharge + change / 15, 100)
print("Reserve charge at " .. self.reserveCharge)
end
function Kani:chargeShot()
if self.reserveCharge > SHOT_INCREMENT then
self.shotCharge += 1
self.reserveCharge -= SHOT_INCREMENT
print("Shot charged to size " .. self.shotCharge)
if self.weaponPower >= 4 then
return -- weapon is fully charged
end
local requiredPower = WEAPON_CHARGE_LOOKUP[self.weaponPower]
if self.reserveCharge > requiredPower then
self.reserveCharge -= requiredPower
self.weaponPower += 1
print("Shot charged to size " .. self.weaponPower)
end
end
function Kani:fire()
self.firingTimer:remove()
if self.shotCharge <= 0 then
if self.weaponPower <= 0 then
return
end
print("Creating bullet of size " .. self.shotCharge)
local bullet = Bullet(self.shotCharge * 2)
print("Firing bullet of size " .. self.weaponPower)
local bullet = Bullet(self.weaponPower)
bullet:moveTo(self.x+16, self.y)
bullet:add()
self.shotCharge = 0
self.weaponPower = 0
end
function Kani:addInputHandlers()