Initial implementation of player and bullets.

This commit is contained in:
2023-09-29 20:52:06 -04:00
parent 2341e8711f
commit 8d87364afb
6 changed files with 157 additions and 9 deletions

View File

@ -1,5 +1,5 @@
PLAYDATE_SDK_PATH := ~/playdate
GAME := Crong
GAME := Crankani
all: build

33
src/bullet.lua Normal file
View File

@ -0,0 +1,33 @@
-- Code for bullets!
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
local gfx <const> = playdate.graphics
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.popContext()
Bullet.super.init(self, img)
self:setCollideRect(0, 0, self:getSize())
local friendly = friendly or true
if friendly then
self:setGroupMask(0x4)
self:setCollidesWithGroupsMask(0x9)
self.direction = 1
else
self:setGroupMask(0x10)
self:setCollidesWithGroupsMask(0x2)
self.direction = -1
end
end
function Bullet:update()
self:moveWithCollisions(self.x+self.direction, self.y)
end

BIN
src/images/kani.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 B

78
src/kani.lua Normal file
View 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

View File

@ -1,18 +1,53 @@
-- Collision groups:
-- 0x1 - walls
-- 0x2 - player
-- 0x4 - player bullets
-- 0x8 - enemies
-- 0x10 - enemy bullets
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprite"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "kani"
local gfx <const> = playdate.graphics
function setup()
local player = nil
function setup()
player = Kani()
player:moveTo(16, 120)
player:addInputHandlers()
player:add()
makeWalls()
drawBackground()
end
function makeWalls()
makeWall(200, 0, 400, 1)
makeWall(0, 120, 1, 240)
makeWall(200, 0, 400, 1)
makeWall(0, 120, 1, 240)
end
function makeWall(x, y, w, h)
local wall = gfx.sprite.new()
wall:setSize(w, h)
wall:setCollideRect(0, 0, wall:getSize())
wall:setGroupMask(0x1)
wall:add()
end
function drawBackground()
local backgroundImage = gfx.image.new(400, 240, gfx.kColorWhite)
gfx.sprite.setBackgroundDrawingCallback(
function(x, y, width, height)
backgroundImage:draw(0, 0)
end
)
end
function playdate.update()
playdate.timer.updateTimers()
gfx.sprite.update()
end