Improve firing code.
This commit is contained in:
parent
8d87364afb
commit
5d1c36ea2f
|
@ -10,7 +10,7 @@ class("Bullet").extends(gfx.sprite)
|
||||||
function Bullet:init(size, friendly)
|
function Bullet:init(size, friendly)
|
||||||
local img = gfx.image.new(size, size)
|
local img = gfx.image.new(size, size)
|
||||||
gfx.pushContext(img)
|
gfx.pushContext(img)
|
||||||
gfx.drawCircleInRect(0, 0, size, size)
|
gfx.fillCircleInRect(0, 0, size, size)
|
||||||
gfx.popContext()
|
gfx.popContext()
|
||||||
|
|
||||||
Bullet.super.init(self, img)
|
Bullet.super.init(self, img)
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 659 B After Width: | Height: | Size: 4.5 KiB |
33
src/kani.lua
33
src/kani.lua
|
@ -2,10 +2,16 @@
|
||||||
import "CoreLibs/object"
|
import "CoreLibs/object"
|
||||||
import "CoreLibs/graphics"
|
import "CoreLibs/graphics"
|
||||||
import "CoreLibs/sprites"
|
import "CoreLibs/sprites"
|
||||||
|
import "CoreLibs/timer"
|
||||||
import "bullet"
|
import "bullet"
|
||||||
|
|
||||||
local gfx <const> = playdate.graphics
|
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)
|
class("Kani").extends(gfx.sprite)
|
||||||
|
|
||||||
function Kani:init()
|
function Kani:init()
|
||||||
|
@ -17,8 +23,9 @@ function Kani:init()
|
||||||
self:setGroupMask(0x2)
|
self:setGroupMask(0x2)
|
||||||
self:setCollidesWithGroupsMask(0x19)
|
self:setCollidesWithGroupsMask(0x19)
|
||||||
|
|
||||||
self.reserveCharge = 0
|
self.reserveCharge = 100
|
||||||
self.shotCharge = 0
|
self.shotCharge = 0
|
||||||
|
self.firingMode = false
|
||||||
|
|
||||||
-- input handlers
|
-- input handlers
|
||||||
self.inputHandlers = {
|
self.inputHandlers = {
|
||||||
|
@ -31,31 +38,35 @@ function Kani:init()
|
||||||
leftButtonUp = function() self.vector.x = 0 end,
|
leftButtonUp = function() self.vector.x = 0 end,
|
||||||
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() self:chargeShot(1) end,
|
AButtonDown = function()
|
||||||
|
self.firingTimer = playdate.timer.keyRepeatTimerWithDelay(0, 500, self.chargeShot, self)
|
||||||
|
end,
|
||||||
AButtonUp = function() self:fire() end,
|
AButtonUp = function() self:fire() end,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Kani:chargeReserve(change)
|
function Kani:chargeReserve(change)
|
||||||
self.reserveCharge += change / 10
|
self.reserveCharge = math.min(self.reserveCharge + change / 15, 100)
|
||||||
|
print("Reserve charge at " .. self.reserveCharge)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Kani:chargeShot(amount)
|
function Kani:chargeShot()
|
||||||
if self.reserveCharge > amount then
|
if self.reserveCharge > SHOT_INCREMENT then
|
||||||
self.shotCharge += amount
|
self.shotCharge += 1
|
||||||
self.reserveCharge -= amount
|
self.reserveCharge -= SHOT_INCREMENT
|
||||||
elseif self.reserveCharge > 0 then
|
print("Shot charged to size " .. self.shotCharge)
|
||||||
self.shotCharge += self.reserveCharge
|
|
||||||
self.reserveCharge = 0
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Kani:fire()
|
function Kani:fire()
|
||||||
|
self.firingTimer:remove()
|
||||||
|
|
||||||
if self.shotCharge <= 0 then
|
if self.shotCharge <= 0 then
|
||||||
return
|
return
|
||||||
end
|
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:moveTo(self.x+16, self.y)
|
||||||
bullet:add()
|
bullet:add()
|
||||||
self.shotCharge = 0
|
self.shotCharge = 0
|
||||||
|
|
|
@ -49,6 +49,7 @@ end
|
||||||
|
|
||||||
function playdate.update()
|
function playdate.update()
|
||||||
gfx.sprite.update()
|
gfx.sprite.update()
|
||||||
|
playdate.timer.updateTimers()
|
||||||
end
|
end
|
||||||
|
|
||||||
setup()
|
setup()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user