Fix up collision code and improve bullet semantics.

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

View File

@ -7,19 +7,34 @@ local gfx <const> = playdate.graphics
class("Bullet").extends(gfx.sprite)
function Bullet:init(size, friendly)
function Bullet:init(power, friendly)
Bullet.super.init(self)
self.power = power
local size = 1
if power == 1 then
size = 2
elseif power == 2 then
size = 4
elseif power == 3 then
size = 6
elseif power == 4 then
size = 10
end
local img = gfx.image.new(size, size)
gfx.pushContext(img)
gfx.fillCircleInRect(0, 0, size, size)
gfx.popContext()
Bullet.super.init(self, img)
self:setImage(img)
self:setCollideRect(0, 0, self:getSize())
self.collisionResponse = gfx.sprite.kCollisionTypeOverlap
local friendly = friendly or true
if friendly then
self:setGroupMask(0x4)
self:setCollidesWithGroupsMask(0x9)
self:setCollidesWithGroupsMask(0x8)
self.direction = 1
else
self:setGroupMask(0x10)

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()

View File

@ -26,14 +26,15 @@ end
function makeWalls()
makeWall(200, 0, 400, 1)
makeWall(0, 120, 1, 240)
makeWall(200, 0, 400, 1)
makeWall(0, 120, 1, 240)
makeWall(200, 240, 400, 1)
makeWall(400, 120, 1, 240)
end
function makeWall(x, y, w, h)
local wall = gfx.sprite.new()
wall:setSize(w, h)
wall:setCollideRect(0, 0, wall:getSize())
wall:moveTo(x, y)
wall:setGroupMask(0x1)
wall:add()
end