diff --git a/src/bullet.lua b/src/bullet.lua index 7b8682a..b8cfc51 100644 --- a/src/bullet.lua +++ b/src/bullet.lua @@ -7,20 +7,24 @@ local gfx = playdate.graphics class("Bullet").extends(gfx.sprite) +local POWER_SIZE_LOOKUP = { + [1] = 2, + [2] = 4, + [3] = 6, + [4] = 10, +} + +local POWER_DAMAGE_LOOKUP = { + [1] = 1, + [2] = 3, + [3] = 5, + [4] = 10, +} + function Bullet:init(power, friendly) Bullet.super.init(self) self.power = power - - local size = 1 - if power == 1 then - size = 2 - elseif power == 2 then - size = 4 - elseif power == 3 then - size = 6 - elseif power == 4 then - size = 10 - end + local size = POWER_SIZE_LOOKUP[power] local img = gfx.image.new(size, size) gfx.pushContext(img) @@ -44,5 +48,14 @@ function Bullet:init(power, friendly) end function Bullet:update() - self:moveWithCollisions(self.x+self.direction, self.y) + local collisions = select(3, self:moveWithCollisions(self.x+self.direction, self.y)) + for i=1, #collisions, 1 do + -- anything the bullet can collide with should be damaged by it + local obj = collisions[i].other + obj:damage(POWER_DAMAGE_LOOKUP[self.power]) + end + + if #collisions >= 1 then + self:remove() + end end diff --git a/src/entity.lua b/src/entity.lua new file mode 100644 index 0000000..66c926f --- /dev/null +++ b/src/entity.lua @@ -0,0 +1,28 @@ +-- A superclass for all player and AI-controlled units +import "CoreLibs/object" +import "CoreLibs/graphics" +import "CoreLibs/sprites" + +local gfx = playdate.graphics + +class("Entity").extends(gfx.sprite) + +function Entity:init(img, health) + Entity.super.init(self, img) + self.health = health or 10 + + self:setCollideRect(0, 0, self:getSize()) + + -- most entities will be enemies, so we configure this mask by default + -- We don't set a collider mask because collision is always handled by + -- other objects (todo: consider player staying perfectly still) + self:setGroupMask(0x8) +end + +function Entity:damage(amount) + self.health = math.max(self.health - amount, 0) + + if self.health == 0 then + self:remove() + end +end diff --git a/src/ika.lua b/src/ika.lua new file mode 100644 index 0000000..5bc8905 --- /dev/null +++ b/src/ika.lua @@ -0,0 +1,14 @@ +-- A squid-like enemy unit +import "CoreLibs/object" +import "CoreLibs/graphics" +import "CoreLibs/sprites" +import "entity" + +local gfx = playdate.graphics + +class("Ika").extends(Entity) + +function Ika:init() + local img = gfx.image.new(30, 30, gfx.kColorBlack) + Ika.super.init(self, img, 25) +end diff --git a/src/kani.lua b/src/kani.lua index 21c18e2..e00fa06 100644 --- a/src/kani.lua +++ b/src/kani.lua @@ -3,6 +3,7 @@ import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" import "CoreLibs/timer" +import "entity" import "bullet" import "ui" @@ -19,19 +20,16 @@ local WEAPON_CHARGE_LOOKUP = { [3]=50 } -class("Kani").extends(gfx.sprite) +class("Kani").extends(Entity) function Kani:init(ui) local img = gfx.image.new("images/kani.png") - Kani.super.init(self, img) + Kani.super.init(self, img, 100) - self:setCollideRect(0, 0, self:getSize()) self.vector = {x=0,y=0} -- movement direction self:setGroupMask(0x2) self:setCollidesWithGroupsMask(0x19) - -- stats - self.health = 100 self.reserveCharge = 100 -- controls the speed the crank recharges the player's reserves. A lower number allows faster charging. @@ -106,12 +104,12 @@ function Kani:fire() end function Kani:damage(amount) - self.health = math.max(self.health - amount, 0) - - if self.health <= 0 then - -- TODO: destroy ship - end + Kani.super.damage(self, amount) self.ui.healthMeter:setValue(self.health) + + if self.health == 0 then + -- TODO: end game here + end end function Kani:addInputHandlers() diff --git a/src/main.lua b/src/main.lua index 5d70bb1..ad9e897 100644 --- a/src/main.lua +++ b/src/main.lua @@ -9,6 +9,7 @@ import "CoreLibs/graphics" import "CoreLibs/sprites" import "CoreLibs/timer" import "kani" +import "ika" local gfx = playdate.graphics @@ -23,6 +24,10 @@ function setup() player:add() ui:add() + local enemy = Ika() + enemy:moveTo(350, 120) + enemy:add() + makeWalls() drawBackground() diff --git a/src/pipmeter.lua b/src/pipmeter.lua index d4c2eff..61d9412 100644 --- a/src/pipmeter.lua +++ b/src/pipmeter.lua @@ -21,7 +21,7 @@ function PipMeter:init(initialValue, numPips, baseSize, sizeIncrement, spacing, local width = (numPips * (padBaseSize + lastSize)) / 2 - spacing self:setSize(width, lastSize) - if self.circle then + if circle then self.drawFunc = gfx.drawCircleInRect self.fillFunc = gfx.fillCircleInRect else @@ -39,8 +39,6 @@ function PipMeter:setValue(value) for i=1, self.numPips, 1 do local size = self.baseSize+(self.sizeIncrement*(i-1)) - print(string.format("Drawing a pip with %d %d %d %d", offset, self.height, size, size)) - if value >= i then self.fillFunc(offset, self.height - size, size, size) else diff --git a/src/ui.lua b/src/ui.lua index 338b2bb..8782947 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -9,7 +9,7 @@ class("UI", {}).extends(playdate.object) function UI:init() self.chargeMeter = Meter(100, 10, 60) self.healthMeter = Meter(100, 60, 10, false) - self.weaponPowerMeter = PipMeter(0, 4, 5, 3, 3) + self.weaponPowerMeter = PipMeter(0, 4, 4, 4, 3, true) self:moveTo(359, 199) end