From 8ef960dad64a37d7254839e7b4e2b8c440bbe757 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 29 Apr 2024 15:21:05 +0200 Subject: [PATCH] Add without (#4) * Add without * Return self * Set compatibleAchetype after without * Add test for Without * Adding test --- lib/init.lua | 13 ++++++--- lib/init.spec.lua | 73 ++++++++++++++++++++++++++++++++++++----------- test.project.json | 3 -- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index d2b4aa0..6b0dd51 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -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 diff --git a/lib/init.spec.lua b/lib/init.spec.lua index a0e4aa7..36b9443 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -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() diff --git a/test.project.json b/test.project.json index 28d10df..b931a84 100644 --- a/test.project.json +++ b/test.project.json @@ -23,9 +23,6 @@ "benches": { "$path": "benches" }, - "rewrite": { - "$path": "matterRewrite" - }, "mirror": { "$path": "mirror" }