90 lines
2.5 KiB
Lua
90 lines
2.5 KiB
Lua
-- Code for the player's ship.
|
|
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()
|
|
local img = gfx.image.new("images/kani.png")
|
|
Kani.super.init(self, img)
|
|
print(self)
|
|
self:setCollideRect(0, 0, self:getSize())
|
|
self.vector = {x=0,y=0} -- movement direction
|
|
self:setGroupMask(0x2)
|
|
self:setCollidesWithGroupsMask(0x19)
|
|
|
|
self.reserveCharge = 100
|
|
self.shotCharge = 0
|
|
self.firingMode = false
|
|
|
|
-- input handlers
|
|
self.inputHandlers = {
|
|
upButtonDown = function() self.vector.y = -1 end,
|
|
downButtonDown = function() self.vector.y = 1 end,
|
|
leftButtonDown = function() self.vector.x = -1 end,
|
|
rightButtonDown = function() self.vector.x = 1 end,
|
|
upButtonUp = function() self.vector.y = 0 end,
|
|
downButtonUp = function() self.vector.y = 0 end,
|
|
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.firingTimer = playdate.timer.keyRepeatTimerWithDelay(0, 500, self.chargeShot, self)
|
|
end,
|
|
AButtonUp = function() self:fire() end,
|
|
}
|
|
end
|
|
|
|
function Kani:chargeReserve(change)
|
|
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)
|
|
end
|
|
end
|
|
|
|
function Kani:fire()
|
|
self.firingTimer:remove()
|
|
|
|
if self.shotCharge <= 0 then
|
|
return
|
|
end
|
|
|
|
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
|
|
end
|
|
|
|
function Kani:addInputHandlers()
|
|
playdate.inputHandlers.push(self.inputHandlers)
|
|
end
|
|
|
|
function Kani:removeInputHandlers()
|
|
playdate.inputHandlers.pop()
|
|
end
|
|
|
|
-- move that crab!
|
|
function Kani:update()
|
|
local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y))
|
|
for i=0, #collisions, 1 do
|
|
-- handle collisions
|
|
end
|
|
end
|