diff --git a/src/bullet.lua b/src/bullet.lua index b8cfc51..a1bb283 100644 --- a/src/bullet.lua +++ b/src/bullet.lua @@ -21,10 +21,12 @@ local POWER_DAMAGE_LOOKUP = { [4] = 10, } -function Bullet:init(power, friendly) +-- Bullet's defaults assume a friendly bullet +function Bullet:init(power, vector, collisionMask) Bullet.super.init(self) self.power = power local size = POWER_SIZE_LOOKUP[power] + local mask = collisionMask or 0x4 local img = gfx.image.new(size, size) gfx.pushContext(img) @@ -35,20 +37,14 @@ function Bullet:init(power, friendly) self:setCollideRect(0, 0, self:getSize()) self.collisionResponse = gfx.sprite.kCollisionTypeOverlap - local friendly = friendly or true - if friendly then - self:setGroupMask(0x4) - self:setCollidesWithGroupsMask(0x8) - self.direction = 1 - else - self:setGroupMask(0x10) - self:setCollidesWithGroupsMask(0x2) - self.direction = -1 - end + self:setGroupMask(0x10) + self:setCollidesWithGroupsMask(mask) + + self.vector = vector or {x=3, y=0} end function Bullet:update() - local collisions = select(3, self:moveWithCollisions(self.x+self.direction, self.y)) + local collisions = select(3, self:moveWithCollisions(self.x+self.vector.x, self.y+self.vector.y)) for i=1, #collisions, 1 do -- anything the bullet can collide with should be damaged by it local obj = collisions[i].other diff --git a/src/enemy/ebi.lua b/src/enemy/ebi.lua index dc92ba1..c09f2f4 100644 --- a/src/enemy/ebi.lua +++ b/src/enemy/ebi.lua @@ -11,4 +11,6 @@ class("Ebi").extends(Entity) function Ebi:init() local img = gfx.image.new(10, 10, gfx.kColorBlack) Ebi.super.init(self, img, 5) + + self:setCollidesWithGroupsMask(0x3) end diff --git a/src/entity.lua b/src/entity.lua index 6c701d2..d53cfe5 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -15,9 +15,9 @@ function Entity:init(img, health, armor) 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) + -- We don't set a collider mask because collision is a bit too variable + -- (but we should always include 0x2 and handle player collisions) + self:setGroupMask(0x4) end function Entity:damage(amount) diff --git a/src/kani.lua b/src/kani.lua index e00fa06..c00e5aa 100644 --- a/src/kani.lua +++ b/src/kani.lua @@ -28,7 +28,7 @@ function Kani:init(ui) self.vector = {x=0,y=0} -- movement direction self:setGroupMask(0x2) - self:setCollidesWithGroupsMask(0x19) + self:setCollidesWithGroupsMask(0xd) self.reserveCharge = 100 diff --git a/src/main.lua b/src/main.lua index 9ca2a32..49c937f 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,9 +1,9 @@ -- Collision groups: --- 0x1 - walls +-- 0x1 - top and bottom walls -- 0x2 - player --- 0x4 - player bullets --- 0x8 - enemies --- 0x10 - enemy bullets +-- 0x4 - enemies +-- 0x8 - side walls +-- 0x10 - bullets import "CoreLibs/object" import "CoreLibs/graphics" import "CoreLibs/sprites" @@ -30,15 +30,15 @@ function setup() enemy:add() enemy = Ebi() - enemy:moveTo(300, 50) + enemy:moveTo(270, 50) enemy:add() enemy = Ebi() - enemy:moveTo(300, 100) + enemy:moveTo(280, 100) enemy:add() enemy = Ebi() - enemy:moveTo(300, 150) + enemy:moveTo(290, 150) enemy:add() enemy = Ebi() @@ -51,18 +51,18 @@ function setup() end function makeWalls() - makeWall(200, 0, 400, 1) - makeWall(0, 120, 1, 240) - makeWall(200, 240, 400, 1) - makeWall(400, 120, 1, 240) + makeWall(200, 0, 400, 1, 0x1) + makeWall(0, 120, 1, 240, 0x8) + makeWall(200, 240, 400, 1, 0x1) + makeWall(400, 120, 1, 240, 0x8) end -function makeWall(x, y, w, h) +function makeWall(x, y, w, h, mask) local wall = gfx.sprite.new() wall:setSize(w, h) wall:setCollideRect(0, 0, wall:getSize()) wall:moveTo(x, y) - wall:setGroupMask(0x1) + wall:setGroupMask(mask) wall:add() end