Rework collision code for future features.
This commit is contained in:
parent
f6ae85dcb1
commit
bb25e255a3
|
@ -21,10 +21,12 @@ local POWER_DAMAGE_LOOKUP <const> = {
|
||||||
[4] = 10,
|
[4] = 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
function Bullet:init(power, friendly)
|
-- Bullet's defaults assume a friendly bullet
|
||||||
|
function Bullet:init(power, vector, collisionMask)
|
||||||
Bullet.super.init(self)
|
Bullet.super.init(self)
|
||||||
self.power = power
|
self.power = power
|
||||||
local size = POWER_SIZE_LOOKUP[power]
|
local size = POWER_SIZE_LOOKUP[power]
|
||||||
|
local mask = collisionMask or 0x4
|
||||||
|
|
||||||
local img = gfx.image.new(size, size)
|
local img = gfx.image.new(size, size)
|
||||||
gfx.pushContext(img)
|
gfx.pushContext(img)
|
||||||
|
@ -35,20 +37,14 @@ function Bullet:init(power, friendly)
|
||||||
self:setCollideRect(0, 0, self:getSize())
|
self:setCollideRect(0, 0, self:getSize())
|
||||||
self.collisionResponse = gfx.sprite.kCollisionTypeOverlap
|
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:setGroupMask(0x10)
|
||||||
self:setCollidesWithGroupsMask(0x2)
|
self:setCollidesWithGroupsMask(mask)
|
||||||
self.direction = -1
|
|
||||||
end
|
self.vector = vector or {x=3, y=0}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Bullet:update()
|
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
|
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
|
||||||
|
|
|
@ -11,4 +11,6 @@ class("Ebi").extends(Entity)
|
||||||
function Ebi:init()
|
function Ebi:init()
|
||||||
local img = gfx.image.new(10, 10, gfx.kColorBlack)
|
local img = gfx.image.new(10, 10, gfx.kColorBlack)
|
||||||
Ebi.super.init(self, img, 5)
|
Ebi.super.init(self, img, 5)
|
||||||
|
|
||||||
|
self:setCollidesWithGroupsMask(0x3)
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,9 +15,9 @@ function Entity:init(img, health, armor)
|
||||||
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
|
||||||
-- We don't set a collider mask because collision is always handled by
|
-- We don't set a collider mask because collision is a bit too variable
|
||||||
-- other objects (todo: consider player staying perfectly still)
|
-- (but we should always include 0x2 and handle player collisions)
|
||||||
self:setGroupMask(0x8)
|
self:setGroupMask(0x4)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:damage(amount)
|
function Entity:damage(amount)
|
||||||
|
|
|
@ -28,7 +28,7 @@ function Kani:init(ui)
|
||||||
|
|
||||||
self.vector = {x=0,y=0} -- movement direction
|
self.vector = {x=0,y=0} -- movement direction
|
||||||
self:setGroupMask(0x2)
|
self:setGroupMask(0x2)
|
||||||
self:setCollidesWithGroupsMask(0x19)
|
self:setCollidesWithGroupsMask(0xd)
|
||||||
|
|
||||||
self.reserveCharge = 100
|
self.reserveCharge = 100
|
||||||
|
|
||||||
|
|
26
src/main.lua
26
src/main.lua
|
@ -1,9 +1,9 @@
|
||||||
-- Collision groups:
|
-- Collision groups:
|
||||||
-- 0x1 - walls
|
-- 0x1 - top and bottom walls
|
||||||
-- 0x2 - player
|
-- 0x2 - player
|
||||||
-- 0x4 - player bullets
|
-- 0x4 - enemies
|
||||||
-- 0x8 - enemies
|
-- 0x8 - side walls
|
||||||
-- 0x10 - enemy bullets
|
-- 0x10 - bullets
|
||||||
import "CoreLibs/object"
|
import "CoreLibs/object"
|
||||||
import "CoreLibs/graphics"
|
import "CoreLibs/graphics"
|
||||||
import "CoreLibs/sprites"
|
import "CoreLibs/sprites"
|
||||||
|
@ -30,15 +30,15 @@ function setup()
|
||||||
enemy:add()
|
enemy:add()
|
||||||
|
|
||||||
enemy = Ebi()
|
enemy = Ebi()
|
||||||
enemy:moveTo(300, 50)
|
enemy:moveTo(270, 50)
|
||||||
enemy:add()
|
enemy:add()
|
||||||
|
|
||||||
enemy = Ebi()
|
enemy = Ebi()
|
||||||
enemy:moveTo(300, 100)
|
enemy:moveTo(280, 100)
|
||||||
enemy:add()
|
enemy:add()
|
||||||
|
|
||||||
enemy = Ebi()
|
enemy = Ebi()
|
||||||
enemy:moveTo(300, 150)
|
enemy:moveTo(290, 150)
|
||||||
enemy:add()
|
enemy:add()
|
||||||
|
|
||||||
enemy = Ebi()
|
enemy = Ebi()
|
||||||
|
@ -51,18 +51,18 @@ function setup()
|
||||||
end
|
end
|
||||||
|
|
||||||
function makeWalls()
|
function makeWalls()
|
||||||
makeWall(200, 0, 400, 1)
|
makeWall(200, 0, 400, 1, 0x1)
|
||||||
makeWall(0, 120, 1, 240)
|
makeWall(0, 120, 1, 240, 0x8)
|
||||||
makeWall(200, 240, 400, 1)
|
makeWall(200, 240, 400, 1, 0x1)
|
||||||
makeWall(400, 120, 1, 240)
|
makeWall(400, 120, 1, 240, 0x8)
|
||||||
end
|
end
|
||||||
|
|
||||||
function makeWall(x, y, w, h)
|
function makeWall(x, y, w, h, mask)
|
||||||
local wall = gfx.sprite.new()
|
local wall = gfx.sprite.new()
|
||||||
wall:setSize(w, h)
|
wall:setSize(w, h)
|
||||||
wall:setCollideRect(0, 0, wall:getSize())
|
wall:setCollideRect(0, 0, wall:getSize())
|
||||||
wall:moveTo(x, y)
|
wall:moveTo(x, y)
|
||||||
wall:setGroupMask(0x1)
|
wall:setGroupMask(mask)
|
||||||
wall:add()
|
wall:add()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user