Improve firing code.

This commit is contained in:
Anna Rose 2023-09-29 22:07:02 -04:00
parent 8d87364afb
commit 5d1c36ea2f
4 changed files with 24 additions and 12 deletions

View File

@ -10,7 +10,7 @@ class("Bullet").extends(gfx.sprite)
function Bullet:init(size, friendly)
local img = gfx.image.new(size, size)
gfx.pushContext(img)
gfx.drawCircleInRect(0, 0, size, size)
gfx.fillCircleInRect(0, 0, size, size)
gfx.popContext()
Bullet.super.init(self, img)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 659 B

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -2,10 +2,16 @@
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "bullet"
local gfx <const> = playdate.graphics
-- the amount of charge needed to increase the shot size by 1
local SHOT_INCREMENT <const> = 10
-- maximum value the reserve charge can reach
local MAX_CHARGE <const> = 100
class("Kani").extends(gfx.sprite)
function Kani:init()
@ -17,8 +23,9 @@ function Kani:init()
self:setGroupMask(0x2)
self:setCollidesWithGroupsMask(0x19)
self.reserveCharge = 0
self.reserveCharge = 100
self.shotCharge = 0
self.firingMode = false
-- input handlers
self.inputHandlers = {
@ -31,31 +38,35 @@ function Kani:init()
leftButtonUp = function() self.vector.x = 0 end,
rightButtonUp = function() self.vector.x = 0 end,
cranked = function(change, accelChange) self:chargeReserve(change) end,
AButtonDown = function() self:chargeShot(1) end,
AButtonDown = function()
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(0, 500, self.chargeShot, self)
end,
AButtonUp = function() self:fire() end,
}
end
function Kani:chargeReserve(change)
self.reserveCharge += change / 10
self.reserveCharge = math.min(self.reserveCharge + change / 15, 100)
print("Reserve charge at " .. self.reserveCharge)
end
function Kani:chargeShot(amount)
if self.reserveCharge > amount then
self.shotCharge += amount
self.reserveCharge -= amount
elseif self.reserveCharge > 0 then
self.shotCharge += self.reserveCharge
self.reserveCharge = 0
function Kani:chargeShot()
if self.reserveCharge > SHOT_INCREMENT then
self.shotCharge += 1
self.reserveCharge -= SHOT_INCREMENT
print("Shot charged to size " .. self.shotCharge)
end
end
function Kani:fire()
self.firingTimer:remove()
if self.shotCharge <= 0 then
return
end
local bullet = Bullet(self.shotCharge)
print("Creating bullet of size " .. self.shotCharge)
local bullet = Bullet(self.shotCharge * 2)
bullet:moveTo(self.x+16, self.y)
bullet:add()
self.shotCharge = 0

View File

@ -49,6 +49,7 @@ end
function playdate.update()
gfx.sprite.update()
playdate.timer.updateTimers()
end
setup()