Skip to content

Commit

Permalink
implement findArchetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
memorycode committed Oct 4, 2024
1 parent 097ab69 commit e9231d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/World.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ return function()
describe("immediate", function()
itFOCUS("test", function()
local world = World.new()
local A, B = component(), component()
world:spawn(A({ a = true }), B({ b = true }))
local A, B, C = component(), component(), component()
world:spawn(A({ a = true }), B({ b = true }), C({ c = true, deep = true }))
world:spawn(A({ a = true }))
world:spawn(C({ c = true, top = true }))
world:spawn(B({ b = true }), C({ c = true, nested = true }))

for id, a in world:query(A) do
for id, a in world:query(C) do
print(id, a)
end

Expand Down
28 changes: 22 additions & 6 deletions lib/archetypeTree.luau
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,35 @@ local function newArchetypeTree()
return node
end

local function findArchetypes(self, terms: Terms)
local function findArchetypes(_, terms: Terms)
table.sort(terms)

local archetypes = {}
local function collapse(node: Node)
table.insert(archetypes, node.archetype)
local function check(node: Node, terms: Terms)
if #terms == 0 then
if node.archetype then
table.insert(archetypes, node.archetype)
end
end

for _, child in node.children do
collapse(child)
for componentId, child in node.children do
local head = terms[1]
if head then
if componentId < head then
check(child, terms)
elseif componentId == head then
local newTerms = table.clone(terms)
table.remove(newTerms, 1)

check(child, newTerms)
end
else
check(child, terms)
end
end
end

collapse(findNode(self, terms))
check(root, terms)
return archetypes
end

Expand Down

0 comments on commit e9231d9

Please sign in to comment.