Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has function #85

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down