Rework collision code for future features.

This commit is contained in:
Anna Rose 2023-09-30 15:47:26 -04:00
parent f6ae85dcb1
commit bb25e255a3
5 changed files with 27 additions and 29 deletions

View File

@ -21,10 +21,12 @@ local POWER_DAMAGE_LOOKUP <const> = {
[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: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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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