Implement first enemy, bullet damage and collision.
This commit is contained in:
parent
032cc0a2ed
commit
4aa4c129dc
|
@ -7,20 +7,24 @@ local gfx <const> = playdate.graphics
|
|||
|
||||
class("Bullet").extends(gfx.sprite)
|
||||
|
||||
local POWER_SIZE_LOOKUP <const> = {
|
||||
[1] = 2,
|
||||
[2] = 4,
|
||||
[3] = 6,
|
||||
[4] = 10,
|
||||
}
|
||||
|
||||
local POWER_DAMAGE_LOOKUP <const> = {
|
||||
[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
|
||||
|
|
28
src/entity.lua
Normal file
28
src/entity.lua
Normal file
|
@ -0,0 +1,28 @@
|
|||
-- A superclass for all player and AI-controlled units
|
||||
import "CoreLibs/object"
|
||||
import "CoreLibs/graphics"
|
||||
import "CoreLibs/sprites"
|
||||
|
||||
local gfx <const> = 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
|
14
src/ika.lua
Normal file
14
src/ika.lua
Normal file
|
@ -0,0 +1,14 @@
|
|||
-- A squid-like enemy unit
|
||||
import "CoreLibs/object"
|
||||
import "CoreLibs/graphics"
|
||||
import "CoreLibs/sprites"
|
||||
import "entity"
|
||||
|
||||
local gfx <const> = 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
|
18
src/kani.lua
18
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 <const> = {
|
|||
[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()
|
||||
|
|
|
@ -9,6 +9,7 @@ import "CoreLibs/graphics"
|
|||
import "CoreLibs/sprites"
|
||||
import "CoreLibs/timer"
|
||||
import "kani"
|
||||
import "ika"
|
||||
|
||||
local gfx <const> = playdate.graphics
|
||||
|
||||
|
@ -23,6 +24,10 @@ function setup()
|
|||
player:add()
|
||||
ui:add()
|
||||
|
||||
local enemy = Ika()
|
||||
enemy:moveTo(350, 120)
|
||||
enemy:add()
|
||||
|
||||
makeWalls()
|
||||
|
||||
drawBackground()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user