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

View File

@@ -45,7 +45,7 @@ registry.exclude -> query -> entity... -> query
registry.execute -> query -> (() -> (chunk?))
registry.chunk -> entity -> entity... -> (chunk)
registry.entities -> chunk -> entity -> (entity[])
registry.components -> chunk -> entity -> (any[])
registry.components -> chunk -> entity... -> (any[]...)
```
### Instance `entity`
@@ -79,7 +79,7 @@ query:execute -> (() -> (chunk?))
```
chunk:entities -> entity -> (entity[])
chunk:components -> entity -> (any[])
chunk:components -> entity... -> (any[]...)
```
## Module `singles`

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

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

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()

View File

@@ -768,17 +768,35 @@ function registry.entities(chunk)
end
---@param chunk evolved.chunk
---@param fragment evolved.entity
---@return any[]
---@param ... evolved.entity fragments
---@return any[] ... components
---@nodiscard
function registry.components(chunk, fragment)
local components = chunk.__components[fragment]
function registry.components(chunk, ...)
local components = chunk.__components
if components == nil then
error(string.format('chunk %s does not have fragment %s', chunk, fragment), 2)
local fragment_count = select('#', ...)
if fragment_count == 0 then return end
if fragment_count == 1 then
local f1 = ...
return components[f1]
end
return components
if fragment_count == 2 then
local f1, f2 = ...
return components[f1], components[f2]
end
if fragment_count == 3 then
local f1, f2, f3 = ...
return components[f1], components[f2], components[f3]
end
do
local f1, f2, f3 = ...
return components[f1], components[f2], components[f3],
registry.components(chunk, select(4, ...))
end
end
---