Add without (#4)

* Add without

* Return self

* Set compatibleAchetype after without

* Add test for Without

* Adding test
This commit is contained in:
Marcus 2024-04-29 15:21:05 +02:00 committed by GitHub
parent 13c703211d
commit 8ef960dad6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 66 additions and 23 deletions

View file

@ -408,10 +408,7 @@ function World.query(world: World, ...: i53): any
function preparedQuery:without(...)
local components = { ... }
for i, component in components do
components[i] = #component
end
for i = #compatibleArchetypes, 1, - 1 do
for i = #compatibleArchetypes, 1, -1 do
local archetype = compatibleArchetypes[i].archetype
local shouldRemove = false
for _, componentId in components do
@ -424,9 +421,17 @@ function World.query(world: World, ...: i53): any
table.remove(compatibleArchetypes, i)
end
end
lastArchetype, compatibleArchetype = next(compatibleArchetypes)
if not lastArchetype then
return noop()
end
return self
end
local lastRow
function preparedQuery:__iter()
return function()
local archetype = compatibleArchetype.archetype

View file

@ -19,7 +19,7 @@ local function flip()
return math.random() >= 0.5
end
local hm = 0
local amountOfCombination = 0
for i = 1, N do
local entity = world:entity()
local combination = ""
@ -64,22 +64,11 @@ for i = 1, N do
and combination:find("4")
and combination:find("6")
then
hm += 1
amountOfCombination += 1
end
archetypes[combination] = true
end
local arch = 0
for combination in archetypes do
if combination:find("2")
and combination:find("3")
and combination:find("4")
and combination:find("6")
then
arch += 1
end
end
return function()
describe("World", function()
it("should add component", function()
@ -93,6 +82,7 @@ return function()
expect(world:get(id, B)).to.equal(1)
expect(world:get(id1, A)).to.equal("hello")
end)
it("should remove component", function()
local id = world:entity()
world:set(id, A, true)
@ -101,6 +91,7 @@ return function()
expect(world:get(id, A)).to.equal(nil)
end)
it("should override component data", function()
local id = world:entity()
@ -111,13 +102,63 @@ return function()
expect(world:get(id, A)).to.equal(false)
end)
it("query", function()
it("should not query a removed component", function()
local Tag = world:entity()
local AnotherTag = world:entity()
local entity = world:entity()
world:set(entity, Tag)
world:set(entity, AnotherTag)
world:remove(entity, AnotherTag)
local added = 0
for e, t, a in world:query(Tag, AnotherTag) do
added += 1
end
expect(added).to.equal(0)
end)
it("should query correct number of compatible archetypes", function()
local added = 0
for _ in world:query(B, C, D, F) do
added += 1
end
expect(added).to.equal(hm)
print(added, hm)
expect(added).to.equal(amountOfCombination)
end)
it("should not query poisoned players", function()
local Player = world:entity()
local Health = world:entity()
local Poison = world:entity()
local one = world:entity()
world:set(one, Player, { name = "alice"})
world:set(one, Health, 100)
world:set(one, Poison)
local two = world:entity()
world:set(two, Player, { name = "bob"})
world:set(two, Health, 90)
local withoutCount = 0
for _id, _player in world:query(Player):without(Poison) do
withoutCount += 1
end
expect(withoutCount).to.equal(1)
end)
it("should skip iteration", function()
local Position, Velocity = world:entity(), world:entity()
local e = world:entity()
world:set(e, Position, Vector3.zero)
world:set(e, Velocity, Vector3.one)
local added = 0
for i in world:query(Position):without(Velocity) do
added += 1
end
expect(added).to.equal(0)
end)
it("track changes", function()

View file

@ -23,9 +23,6 @@
"benches": {
"$path": "benches"
},
"rewrite": {
"$path": "matterRewrite"
},
"mirror": {
"$path": "mirror"
}