mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 09:30:03 +00:00
Add has function
This commit is contained in:
parent
0292574b5f
commit
83e1d9c493
2 changed files with 55 additions and 0 deletions
|
@ -669,6 +669,32 @@ do
|
|||
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)
|
||||
export type Query = typeof({
|
||||
__iter = function(): Item
|
||||
|
@ -1077,6 +1103,7 @@ World.component = world_component
|
|||
World.add = world_add
|
||||
World.set = world_set
|
||||
World.get = world_get
|
||||
World.has = world_has
|
||||
World.target = world_target
|
||||
World.parent = world_parent
|
||||
|
||||
|
|
|
@ -530,6 +530,34 @@ TEST("world", function()
|
|||
|
||||
CHECK(withoutCount == 0)
|
||||
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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue