diff --git a/readme.md b/readme.md index 153495a..e0fbd4a 100644 --- a/readme.md +++ b/readme.md @@ -1,12 +1,14 @@ -# +# Crankani - is a game for the [Playdate](https://play.date/) handheld console. +Crankani is a game for the [Playdate](https://play.date/) handheld console. It is a shoot-em-up inspired by Galaga, Gradius, and most of all, Ikaruga. + +In it, you control a space crab (or perhaps a crab-like spacecraft. You decide!) and must dodge bullets, fire bullets of your own, and defend yourself with a potent but limited phase shifting ability. Good luck! ### Building To build this code, install the [Playdate SDK](https://play.date/dev/) and run `make`. Update `PLAYDATE_SDK_PATH` in the Makefile if you have installed the SDK somewhere other than `~/playdate`. -`make` should produce a directory called `.pdx`. You can open this in the Playdate Simulator and sideload it onto your Playdate from there. +`make` should produce a directory called `Crankani.pdx`. You can open this in the Playdate Simulator and sideload it onto your Playdate from there. The Makefile is designed to be run under Linux and is untested on other platforms. diff --git a/src/Makefile b/src/Makefile index 708b956..ff1ad6c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ PLAYDATE_SDK_PATH := ~/playdate -GAME := Crong +GAME := Crankani all: build diff --git a/src/bullet.lua b/src/bullet.lua new file mode 100644 index 0000000..3fcd6a1 --- /dev/null +++ b/src/bullet.lua @@ -0,0 +1,33 @@ +-- Code for bullets! +import "CoreLibs/object" +import "CoreLibs/graphics" +import "CoreLibs/sprites" + +local gfx = 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 diff --git a/src/images/kani.png b/src/images/kani.png new file mode 100644 index 0000000..ede83a6 Binary files /dev/null and b/src/images/kani.png differ diff --git a/src/kani.lua b/src/kani.lua new file mode 100644 index 0000000..208c9fc --- /dev/null +++ b/src/kani.lua @@ -0,0 +1,78 @@ +-- Code for the player's ship. +import "CoreLibs/object" +import "CoreLibs/graphics" +import "CoreLibs/sprites" +import "bullet" + +local gfx = 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 diff --git a/src/main.lua b/src/main.lua index 5199022..4fd47ea 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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 = 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