mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Add has function (#85)
This commit is contained in:
parent
0292574b5f
commit
76d1ff91c0
2 changed files with 55 additions and 0 deletions
|
@ -669,6 +669,32 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local world_has: () -> boolean
|
||||||
|
do
|
||||||
|
function world_has(world, entity_id, ...)
|
||||||
|
local id = entity_id
|
||||||
|
local record = world.entityIndex.sparse[id]
|
||||||
|
if not record then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local archetype = record.archetype
|
||||||
|
if not archetype then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local tr = archetype.records
|
||||||
|
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
if not tr[select(i, ...)] then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
type Item = () -> (number, ...any)
|
type Item = () -> (number, ...any)
|
||||||
export type Query = typeof({
|
export type Query = typeof({
|
||||||
__iter = function(): Item
|
__iter = function(): Item
|
||||||
|
@ -1077,6 +1103,7 @@ World.component = world_component
|
||||||
World.add = world_add
|
World.add = world_add
|
||||||
World.set = world_set
|
World.set = world_set
|
||||||
World.get = world_get
|
World.get = world_get
|
||||||
|
World.has = world_has
|
||||||
World.target = world_target
|
World.target = world_target
|
||||||
World.parent = world_parent
|
World.parent = world_parent
|
||||||
|
|
||||||
|
|
|
@ -530,6 +530,34 @@ TEST("world", function()
|
||||||
|
|
||||||
CHECK(withoutCount == 0)
|
CHECK(withoutCount == 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "should find Tag on entity"
|
||||||
|
local world = jecs.World.new()
|
||||||
|
|
||||||
|
local Tag = world:component()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, Tag)
|
||||||
|
|
||||||
|
CHECK(world:has(e, Tag))
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "should return false when missing one tag"
|
||||||
|
local world = jecs.World.new()
|
||||||
|
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
local D = world:component()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
world:add(e, C)
|
||||||
|
world:add(e, D)
|
||||||
|
|
||||||
|
CHECK(world:has(e, A, B, C, D) == false)
|
||||||
|
end
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue