Skip to content

Commit 83e1d9c

Browse files
committed
Add has function
1 parent 0292574 commit 83e1d9c

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/init.luau

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,32 @@ do
669669
end
670670
end
671671

672+
local world_has: () -> boolean
673+
do
674+
function world_has(world, entity_id, ...)
675+
local id = entity_id
676+
local record = world.entityIndex.sparse[id]
677+
if not record then
678+
return false
679+
end
680+
681+
local archetype = record.archetype
682+
if not archetype then
683+
return false
684+
end
685+
686+
local tr = archetype.records
687+
688+
for i = 1, select("#", ...) do
689+
if not tr[select(i, ...)] then
690+
return false
691+
end
692+
end
693+
694+
return true
695+
end
696+
end
697+
672698
type Item = () -> (number, ...any)
673699
export type Query = typeof({
674700
__iter = function(): Item
@@ -1077,6 +1103,7 @@ World.component = world_component
10771103
World.add = world_add
10781104
World.set = world_set
10791105
World.get = world_get
1106+
World.has = world_has
10801107
World.target = world_target
10811108
World.parent = world_parent
10821109

test/tests.luau

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,34 @@ TEST("world", function()
530530

531531
CHECK(withoutCount == 0)
532532
end
533+
534+
do CASE "should find Tag on entity"
535+
local world = jecs.World.new()
536+
537+
local Tag = world:component()
538+
539+
local e = world:entity()
540+
world:add(e, Tag)
541+
542+
CHECK(world:has(e, Tag))
543+
end
544+
545+
do CASE "should return false when missing one tag"
546+
local world = jecs.World.new()
547+
548+
local A = world:component()
549+
local B = world:component()
550+
local C = world:component()
551+
local D = world:component()
552+
553+
local e = world:entity()
554+
world:add(e, A)
555+
world:add(e, C)
556+
world:add(e, D)
557+
558+
CHECK(world:has(e, A, B, C, D) == false)
559+
end
560+
533561
end)
534562

535563

0 commit comments

Comments
 (0)