diff --git a/src/bullet.lua b/src/bullet.lua index a1bb283..ac9e31b 100644 --- a/src/bullet.lua +++ b/src/bullet.lua @@ -7,25 +7,14 @@ 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, -} -- Bullet's defaults assume a friendly bullet -function Bullet:init(power, vector, collisionMask) +function Bullet:init(size, damage, vector, collisionMask) Bullet.super.init(self) + 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 img = gfx.image.new(size, size) @@ -39,8 +28,6 @@ function Bullet:init(power, vector, collisionMask) self:setGroupMask(0x10) self:setCollidesWithGroupsMask(mask) - - self.vector = vector or {x=3, y=0} end function Bullet:update() @@ -48,7 +35,7 @@ function Bullet:update() 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]) + obj:damage(self.damage) end if #collisions >= 1 then diff --git a/src/enemy/ebi.lua b/src/enemy/ebi.lua index c09f2f4..5b4f04c 100644 --- a/src/enemy/ebi.lua +++ b/src/enemy/ebi.lua @@ -13,4 +13,18 @@ function Ebi:init() Ebi.super.init(self, img, 5) 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 diff --git a/src/entity.lua b/src/entity.lua index d53cfe5..2c90352 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -12,6 +12,10 @@ function Entity:init(img, health, armor) self.health = health or 10 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()) -- most entities will be enemies, so we configure this mask by default @@ -29,3 +33,8 @@ function Entity:damage(amount) self:remove() end end + +function Entity:update() + local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y)) + return collisions +end diff --git a/src/kani.lua b/src/kani.lua index c00e5aa..013bc93 100644 --- a/src/kani.lua +++ b/src/kani.lua @@ -20,13 +20,28 @@ local WEAPON_CHARGE_LOOKUP = { [3]=50 } +-- size of bullet at each power level +local POWER_SIZE_LOOKUP = { + [1] = 2, + [2] = 4, + [3] = 6, + [4] = 10, +} + +-- damage of bullet at each power level +local POWER_DAMAGE_LOOKUP = { + [1] = 1, + [2] = 3, + [3] = 5, + [4] = 10, +} + class("Kani").extends(Entity) function Kani:init(ui) local img = gfx.image.new("images/kani.png") Kani.super.init(self, img, 100) - self.vector = {x=0,y=0} -- movement direction self:setGroupMask(0x2) self:setCollidesWithGroupsMask(0xd) @@ -96,7 +111,7 @@ function Kani:fire() return 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:add() self.weaponPower = 0 @@ -122,7 +137,7 @@ end -- move that crab! 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 -- handle player-triggered collisions end