Initial implementation of player and bullets.
This commit is contained in:
parent
2341e8711f
commit
8d87364afb
6 changed files with 157 additions and 9 deletions
78
src/kani.lua
Normal file
78
src/kani.lua
Normal file
|
@ -0,0 +1,78 @@
|
|||
-- Code for the player's ship.
|
||||
import "CoreLibs/object"
|
||||
import "CoreLibs/graphics"
|
||||
import "CoreLibs/sprites"
|
||||
import "bullet"
|
||||
|
||||
local gfx <const> = playdate.graphics
|
||||
|
||||
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 = 0
|
||||
self.shotCharge = 0
|
||||
|
||||
-- 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:chargeShot(1) end,
|
||||
AButtonUp = function() self:fire() end,
|
||||
}
|
||||
end
|
||||
|
||||
function Kani:chargeReserve(change)
|
||||
self.reserveCharge += change / 10
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
function Kani:fire()
|
||||
if self.shotCharge <= 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local bullet = Bullet(self.shotCharge)
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue