multi registry.components

This commit is contained in:
BlackMATov
2024-11-28 21:35:20 +07:00
parent a69a98af65
commit 1d4ee1a1a3
5 changed files with 101 additions and 24 deletions
+15 -9
View File
@@ -1,5 +1,7 @@
local evo = require 'evolved.evolved'
local v2 = evo.vectors.vector2
local singles = {
delta_time = evo.singles.single(0.016),
}
@@ -10,25 +12,29 @@ local fragments = {
}
local queries = {
bodies = evo.registry.query(fragments.position, fragments.velocity),
bodies = evo.registry.query(
fragments.position,
fragments.velocity),
}
do
evo.registry.entity()
:set(fragments.position, evo.vectors.vector2(512, 50))
:set(fragments.velocity, evo.vectors.vector2(math.random(-20, 20), 20))
:set(fragments.position, v2(512, 50))
:set(fragments.velocity, v2(math.random(-20, 20), 20))
end
do
local dt = evo.singles.get(singles.delta_time)
local delta_time = evo.singles.get(singles.delta_time)
for chunk in queries.bodies:execute() do
local es = chunk:entities()
local ps = chunk:components(fragments.position)
local vs = chunk:components(fragments.velocity)
local entities = chunk:entities()
for i = 1, #es do
ps[i] = ps[i] + vs[i] * dt
local positions, velocities = chunk:components(
fragments.position,
fragments.velocity)
for i = 1, #entities do
positions[i] = positions[i] + velocities[i] * delta_time
end
end
end
+3 -6
View File
@@ -103,24 +103,21 @@ end)
describe('Simple Iteration', function(a, b, c, d, e, AB, CD, CE)
for chunk in AB:execute() do
local as = chunk:components(a)
local bs = chunk:components(b)
local as, bs = chunk:components(a, b)
for i = 1, #chunk:entities() do
as[i], bs[i] = bs[i], as[i]
end
end
for chunk in CD:execute() do
local cs = chunk:components(c)
local ds = chunk:components(d)
local cs, ds = chunk:components(c, d)
for i = 1, #chunk:entities() do
cs[i], ds[i] = ds[i], cs[i]
end
end
for chunk in CE:execute() do
local cs = chunk:components(c)
local es = chunk:components(e)
local cs, es = chunk:components(c, e)
for i = 1, #chunk:entities() do
cs[i], es[i] = es[i], cs[i]
end
+56
View File
@@ -212,6 +212,62 @@ do
end
end
do
local f1, f2, f3, f4, f5 =
evo.registry.entity(),
evo.registry.entity(),
evo.registry.entity(),
evo.registry.entity()
local chunk = evo.registry.chunk(f1, f2, f3, f4)
do
evo.registry.entity():set(f1, 1)
evo.registry.entity():set(f1, 1):set(f2, 2)
evo.registry.entity():set(f1, 1):set(f2, 2):set(f3, 3)
evo.registry.entity():set(f1, 1):set(f2, 2):set(f3, 3):set(f4, 4)
end
do
assert(chunk:components() == nil)
end
do
local fs1 = chunk:components(f1)
assert(fs1 and fs1[1] == 1)
end
do
local fs1, fs2 = chunk:components(f1, f2)
assert(fs1 and fs1[1] == 1)
assert(fs2 and fs2[1] == 2)
end
do
local fs1, fs2, fs3 = chunk:components(f1, f2, f3)
assert(fs1 and fs1[1] == 1)
assert(fs2 and fs2[1] == 2)
assert(fs3 and fs3[1] == 3)
end
do
local fs1, fs2, fs3, fs4 = chunk:components(f1, f2, f3, f4)
assert(fs1 and fs1[1] == 1)
assert(fs2 and fs2[1] == 2)
assert(fs3 and fs3[1] == 3)
assert(fs4 and fs4[1] == 4)
end
do
local a, fs1, b, fs2, c, fs3, d, fs4, e = chunk:components(f5, f1, f5, f2, f5, f3, f5, f4, f5)
assert(fs1 and fs1[1] == 1)
assert(fs2 and fs2[1] == 2)
assert(fs3 and fs3[1] == 3)
assert(fs4 and fs4[1] == 4)
assert(a == nil and b == nil and c == nil and d == nil and e == nil)
end
end
do
local f1, f2 = evo.registry.entity(), evo.registry.entity()
local e = evo.registry.entity()