mirror of
https://github.com/vrld/HC.git
synced 2024-11-28 14:04:21 +00:00
Merge start and persits callback. Add HC.setPassive
This commit is contained in:
parent
8b29ebc616
commit
06dc6bed68
110
init.lua
110
init.lua
@ -30,12 +30,6 @@ local Polygon = require(_NAME .. '.polygon')
|
|||||||
local Spatialhash = require(_NAME .. '.spatialhash')
|
local Spatialhash = require(_NAME .. '.spatialhash')
|
||||||
local vector = require(_NAME .. '.vector')
|
local vector = require(_NAME .. '.vector')
|
||||||
|
|
||||||
-- hide submodules
|
|
||||||
_M.shapes = nil
|
|
||||||
_M.polygon = nil
|
|
||||||
_M.spatialhash = nil
|
|
||||||
_M.vector = nil
|
|
||||||
|
|
||||||
local PolygonShape = Shapes.PolygonShape
|
local PolygonShape = Shapes.PolygonShape
|
||||||
local CircleShape = Shapes.CircleShape
|
local CircleShape = Shapes.CircleShape
|
||||||
local PointShape = Shapes.PointShape
|
local PointShape = Shapes.PointShape
|
||||||
@ -43,36 +37,45 @@ local PointShape = Shapes.PointShape
|
|||||||
local is_initialized = false
|
local is_initialized = false
|
||||||
local hash = nil
|
local hash = nil
|
||||||
|
|
||||||
local shapes, ghosts = {}, {}
|
local active_shapes, passive_shapes, ghosts = {}, {}, {}
|
||||||
|
local current_shape_id = 0
|
||||||
local shape_ids = setmetatable({}, {__mode = "k"})
|
local shape_ids = setmetatable({}, {__mode = "k"})
|
||||||
local groups = {}
|
local groups = {}
|
||||||
|
|
||||||
local function __NOT_INIT() error("Not yet initialized") end
|
local function __NOT_INIT() error("Not yet initialized") end
|
||||||
local function __NULL() end
|
local function __NULL() end
|
||||||
local cb_start, cb_persist, cb_stop = __NOT_INIT, __NOT_INIT, __NOT_INIT
|
local cb_collide, cb_stop = __NOT_INIT, __NOT_INIT
|
||||||
|
|
||||||
function init(cell_size, callback_start, callback_persist, callback_stop)
|
function init(cell_size, callback_collide, callback_stop)
|
||||||
cb_start = callback_start or __NULL
|
cb_collide = callback_collide or __NULL
|
||||||
cb_persist = callback_persist or __NULL
|
|
||||||
cb_stop = callback_stop or __NULL
|
cb_stop = callback_stop or __NULL
|
||||||
hash = Spatialhash(cell_size)
|
hash = Spatialhash(cell_size)
|
||||||
is_initialized = true
|
is_initialized = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function setCallbacks(start,persist,stop)
|
function setCallbacks(collide, stop)
|
||||||
local tbl = start
|
if type(collide) == "table" and not (getmetatable(collide) or {}).__call then
|
||||||
if type(start) == "function" then
|
stop = collide.stop
|
||||||
tbl = {start = start, persist = persist, stop = stop}
|
collide = collide.collide
|
||||||
|
end
|
||||||
|
|
||||||
|
if collide then
|
||||||
|
assert(type(collide) == "function" or (getmetatable(collide) or {}).__call,
|
||||||
|
"collision callback must be a function or callable table")
|
||||||
|
cb_collide = collide
|
||||||
|
end
|
||||||
|
|
||||||
|
if stop then
|
||||||
|
assert(type(stop) == "function" or (getmetatable(stop) or {}).__call,
|
||||||
|
"stop callback must be a function or callable table")
|
||||||
|
cb_stop = stop
|
||||||
end
|
end
|
||||||
if tbl.start then cb_start = tbl.start end
|
|
||||||
if tbl.persist then cb_persist = tbl.persist end
|
|
||||||
if tbl.stop then cb_stop = tbl.stop end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function new_shape(shape, ul,lr)
|
local function new_shape(shape, ul,lr)
|
||||||
local id = #shapes+1
|
current_shape_id = current_shape_id + 1
|
||||||
shapes[id] = shape
|
active_shapes[current_shape_id] = shape
|
||||||
shape_ids[shape] = id
|
shape_ids[shape] = current_shape_id
|
||||||
hash:insert(shape, ul,lr)
|
hash:insert(shape, ul,lr)
|
||||||
shape._groups = {}
|
shape._groups = {}
|
||||||
return shape
|
return shape
|
||||||
@ -223,19 +226,17 @@ local colliding_last_frame = {}
|
|||||||
function update(dt)
|
function update(dt)
|
||||||
-- collect colliding shapes
|
-- collect colliding shapes
|
||||||
local tested, colliding = {}, {}
|
local tested, colliding = {}, {}
|
||||||
for _,shape in pairs(shapes) do
|
for _,shape in pairs(active_shapes) do
|
||||||
if not ghosts[shape] then
|
local neighbors = shape:_getNeighbors()
|
||||||
local neighbors = shape:_getNeighbors()
|
for _,other in pairs(neighbors) do
|
||||||
for _,other in pairs(neighbors) do
|
local id = collision_id(shape,other)
|
||||||
local id = collision_id(shape,other)
|
if not tested[id] then
|
||||||
if not tested[id] then
|
if not (ghosts[other] or share_group(shape, other)) then
|
||||||
if not (ghosts[other] or share_group(shape, other)) then
|
local collide, sep = shape:collidesWith(other)
|
||||||
local collide, sep = shape:collidesWith(other)
|
if collide then
|
||||||
if collide then
|
colliding[id] = {shape, other, sep.x, sep.y}
|
||||||
colliding[id] = {shape, other, sep.x, sep.y}
|
|
||||||
end
|
|
||||||
tested[id] = true
|
|
||||||
end
|
end
|
||||||
|
tested[id] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -243,12 +244,8 @@ function update(dt)
|
|||||||
|
|
||||||
-- call colliding callbacks on colliding shapes
|
-- call colliding callbacks on colliding shapes
|
||||||
for id,info in pairs(colliding) do
|
for id,info in pairs(colliding) do
|
||||||
if colliding_last_frame[id] then
|
colliding_last_frame[id] = nil
|
||||||
colliding_last_frame[id] = nil
|
cb_collide( dt, unpack(info) )
|
||||||
cb_persist( dt, unpack(info) )
|
|
||||||
else
|
|
||||||
cb_start( dt, unpack(info) )
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- call stop callback on shapes that do not collide
|
-- call stop callback on shapes that do not collide
|
||||||
@ -263,9 +260,12 @@ end
|
|||||||
-- remove shape from internal tables and the hash
|
-- remove shape from internal tables and the hash
|
||||||
function remove(shape)
|
function remove(shape)
|
||||||
local id = shape_ids[shape]
|
local id = shape_ids[shape]
|
||||||
if not id or not shapes[id] then return end
|
if id then
|
||||||
shapes[id] = nil
|
active_shapes[id] = nil
|
||||||
|
passive_shapes[id] = nil
|
||||||
|
end
|
||||||
ghosts[shape] = nil
|
ghosts[shape] = nil
|
||||||
|
shape_ids[shape] = nil
|
||||||
shape:_removeFromHash()
|
shape:_removeFromHash()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -287,12 +287,38 @@ function removeFromGroup(group, shape, ...)
|
|||||||
return removeFromGroup(group, ...)
|
return removeFromGroup(group, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function setPassive(shape, ...)
|
||||||
|
if not shape then return end
|
||||||
|
assert(shape_ids[shape], "Shape was not created by main module!")
|
||||||
|
local id = shape_ids[shape]
|
||||||
|
if not id or ghosts[shape] then return end
|
||||||
|
|
||||||
|
active_shapes[id] = nil
|
||||||
|
passive_shapes[id] = shape
|
||||||
|
|
||||||
|
return setPassive(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function setActive(shape, ...)
|
||||||
|
if not shape then return end
|
||||||
|
assert(shape_ids[shape], "Shape was not created by main module!")
|
||||||
|
local id = shape_ids[shape]
|
||||||
|
if not id or ghosts[shape] then return end
|
||||||
|
|
||||||
|
active_shapes[id] = shape
|
||||||
|
passive_shapes[id] = nil
|
||||||
|
|
||||||
|
return setActive(...)
|
||||||
|
end
|
||||||
|
|
||||||
function setGhost(shape, ...)
|
function setGhost(shape, ...)
|
||||||
if not shape then return end
|
if not shape then return end
|
||||||
assert(shape_ids[shape], "Shape was not created by main module!")
|
assert(shape_ids[shape], "Shape was not created by main module!")
|
||||||
local id = shape_ids[shape]
|
local id = shape_ids[shape]
|
||||||
if not id then return end
|
if not id then return end
|
||||||
|
|
||||||
|
active_shapes[id] = nil
|
||||||
|
passive_shapes[id] = nil
|
||||||
ghosts[shape] = shape
|
ghosts[shape] = shape
|
||||||
return setGhost(...)
|
return setGhost(...)
|
||||||
end
|
end
|
||||||
@ -303,6 +329,8 @@ function setSolid(shape, ...)
|
|||||||
local id = shape_ids[shape]
|
local id = shape_ids[shape]
|
||||||
if not id then return end
|
if not id then return end
|
||||||
|
|
||||||
|
active_shapes[id] = shape
|
||||||
|
passive_shapes[id] = nil
|
||||||
ghosts[shape] = nil
|
ghosts[shape] = nil
|
||||||
return setSolid(...)
|
return setSolid(...)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user