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) 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) local img = gfx.image.new(size, size)
gfx.pushContext(img) gfx.pushContext(img)
gfx.fillCircleInRect(0, 0, size, size) gfx.fillCircleInRect(0, 0, size, size)
gfx.popContext() gfx.popContext()
Bullet.super.init(self, img) self:setImage(img)
self:setCollideRect(0, 0, self:getSize()) self:setCollideRect(0, 0, self:getSize())
self.collisionResponse = gfx.sprite.kCollisionTypeOverlap
local friendly = friendly or true local friendly = friendly or true
if friendly then if friendly then
self:setGroupMask(0x4) self:setGroupMask(0x4)
self:setCollidesWithGroupsMask(0x9) self:setCollidesWithGroupsMask(0x8)
self.direction = 1 self.direction = 1
else else
self:setGroupMask(0x10) self:setGroupMask(0x10)

View File

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

View File

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