Make Ebi move, refactor bullet code to make more logic per-entity type.

This commit is contained in:
Anna Rose 2023-09-30 16:02:37 -04:00
parent bb25e255a3
commit 0db42e8c87
4 changed files with 46 additions and 21 deletions

View File

@ -7,25 +7,14 @@ local gfx <const> = playdate.graphics
class("Bullet").extends(gfx.sprite) 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,
}
-- Bullet's defaults assume a friendly bullet -- Bullet's defaults assume a friendly bullet
function Bullet:init(power, vector, collisionMask) function Bullet:init(size, damage, vector, collisionMask)
Bullet.super.init(self) Bullet.super.init(self)
self.power = power self.power = power
local size = POWER_SIZE_LOOKUP[power] self.damage = damage or 1
self.vector = vector or {x=5, y=0}
local mask = collisionMask or 0x4 local mask = collisionMask or 0x4
local img = gfx.image.new(size, size) local img = gfx.image.new(size, size)
@ -39,8 +28,6 @@ function Bullet:init(power, vector, collisionMask)
self:setGroupMask(0x10) self:setGroupMask(0x10)
self:setCollidesWithGroupsMask(mask) self:setCollidesWithGroupsMask(mask)
self.vector = vector or {x=3, y=0}
end end
function Bullet:update() function Bullet:update()
@ -48,7 +35,7 @@ function Bullet:update()
for i=1, #collisions, 1 do for i=1, #collisions, 1 do
-- anything the bullet can collide with should be damaged by it -- anything the bullet can collide with should be damaged by it
local obj = collisions[i].other local obj = collisions[i].other
obj:damage(POWER_DAMAGE_LOOKUP[self.power]) obj:damage(self.damage)
end end
if #collisions >= 1 then if #collisions >= 1 then

View File

@ -13,4 +13,18 @@ function Ebi:init()
Ebi.super.init(self, img, 5) Ebi.super.init(self, img, 5)
self:setCollidesWithGroupsMask(0x3) self:setCollidesWithGroupsMask(0x3)
local dir = 1
if math.random(2) == 1 then
dir = -1
end
self.vector = {x=0, y=dir}
end
function Ebi:update()
local collisions = Ebi.super.update(self)
for i=1, #collisions, 1 do
if collisions[i].other:getGroupMask() == 0x1 then
self.vector.y *= -1
end
end
end end

View File

@ -12,6 +12,10 @@ function Entity:init(img, health, armor)
self.health = health or 10 self.health = health or 10
self.armor = armor or 0 self.armor = armor or 0
-- movement direction, every update() the entity will move along this vector and return
-- collision data to the subclass
self.vector = {x=0,y=0}
self:setCollideRect(0, 0, self:getSize()) self:setCollideRect(0, 0, self:getSize())
-- most entities will be enemies, so we configure this mask by default -- most entities will be enemies, so we configure this mask by default
@ -29,3 +33,8 @@ function Entity:damage(amount)
self:remove() self:remove()
end end
end end
function Entity:update()
local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y))
return collisions
end

View File

@ -20,13 +20,28 @@ local WEAPON_CHARGE_LOOKUP <const> = {
[3]=50 [3]=50
} }
-- size of bullet at each power level
local POWER_SIZE_LOOKUP <const> = {
[1] = 2,
[2] = 4,
[3] = 6,
[4] = 10,
}
-- damage of bullet at each power level
local POWER_DAMAGE_LOOKUP <const> = {
[1] = 1,
[2] = 3,
[3] = 5,
[4] = 10,
}
class("Kani").extends(Entity) class("Kani").extends(Entity)
function Kani:init(ui) function Kani:init(ui)
local img = gfx.image.new("images/kani.png") local img = gfx.image.new("images/kani.png")
Kani.super.init(self, img, 100) Kani.super.init(self, img, 100)
self.vector = {x=0,y=0} -- movement direction
self:setGroupMask(0x2) self:setGroupMask(0x2)
self:setCollidesWithGroupsMask(0xd) self:setCollidesWithGroupsMask(0xd)
@ -96,7 +111,7 @@ function Kani:fire()
return return
end end
local bullet = Bullet(self.weaponPower) local bullet = Bullet(POWER_SIZE_LOOKUP[self.weaponPower], POWER_DAMAGE_LOOKUP[self.weaponPower])
bullet:moveTo(self.x+16, self.y) bullet:moveTo(self.x+16, self.y)
bullet:add() bullet:add()
self.weaponPower = 0 self.weaponPower = 0
@ -122,7 +137,7 @@ end
-- move that crab! -- move that crab!
function Kani:update() function Kani:update()
local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y)) local collisions = Kani.super.update(self)
for i=0, #collisions, 1 do for i=0, #collisions, 1 do
-- handle player-triggered collisions -- handle player-triggered collisions
end end