Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Merge branch 'archetype-negation' of https://github.com/evaera/matter
Browse files Browse the repository at this point in the history
…into archetype-negation
  • Loading branch information
Ukendio committed Dec 24, 2023
2 parents 1304db4 + 90ef6db commit 234d8d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/World.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,20 @@ function World:_newQueryArchetype(queryArchetype)
for entityArchetype in storage do
local archetype = string.split(queryArchetype, "||")
local negatedArchetype = archetype[1]
local exclude = archetype[2]
local filter = { unpack(archetype, 2, #archetype) }

if exclude then
local skip = false
for _, exclude in filter do
if areArchetypesCompatible(exclude, entityArchetype) then
continue
skip = true
break
end
end

if skip then
continue
end

if areArchetypesCompatible(negatedArchetype, entityArchetype) then
self._queryCache[queryArchetype][entityArchetype] = true
end
Expand Down Expand Up @@ -413,7 +419,7 @@ local noopQuery = setmetatable({
view = noop,
}, {
__iter = function()
return noopQuery
return noop
end,
})

Expand Down
6 changes: 3 additions & 3 deletions lib/World.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ return function()
expect(world:size()).to.equal(1)
end)

it("without prototype", function()
it("should not find any entities", function()
local world = World.new()

local Hello = component()
Expand All @@ -149,11 +149,11 @@ return function()
local _helloShirley = world:spawn(Hello(), Shirley())

local withoutCount = 0
for _ in world:query(Hello):without(Bob) do
for _ in world:query(Hello):without(Bob, Shirley) do
withoutCount += 1
end

expect(withoutCount).to.equal(1)
expect(withoutCount).to.equal(0)
end)

it("should be queryable", function()
Expand Down
4 changes: 3 additions & 1 deletion lib/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ end

function QueryResult:without(...)
local world = self.world
local negativeArchetype = `{self._queryArchetype}||{archetypeOf(...)}`
local filter = table.concat(string.split(archetypeOf(...), "_"), "||")

local negativeArchetype = `{self._queryArchetype}||{filter}`

if world._queryCache[negativeArchetype] == nil then
world:_newQueryArchetype(negativeArchetype)
Expand Down

0 comments on commit 234d8d9

Please sign in to comment.