mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
Add replace method to query (#46)
* Add replace function * Add next method * Remove tostring * Fix EmptyQuery * add replace method * merge conflicts * return self in without * Make aliases relative * Add test * add to changelog
This commit is contained in:
parent
b73d7e12b7
commit
0fe23e151c
5 changed files with 120 additions and 53 deletions
4
.luaurc
4
.luaurc
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"jecs": "C:/Users/Marcus/Documents/packages/jecs/src",
|
"jecs": "src",
|
||||||
"testkit": "C:/Users/Marcus/Documents/packages/jecs/testkit"
|
"testkit": "testkit"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -10,6 +10,11 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added `query:replace(function(...T) return ...U end)` for replacing components in place
|
||||||
|
- Method is fast pathed to replacing the data to the components for each corresponding entity
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Iterator now goes backwards instead to prevent common cases of iterator invalidation
|
- Iterator now goes backwards instead to prevent common cases of iterator invalidation
|
||||||
|
@ -109,12 +114,3 @@ The format is based on [Keep a Changelog][kac], and this project adheres to
|
||||||
[0.0.0-prototype-rc.3]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3
|
[0.0.0-prototype-rc.3]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3
|
||||||
[0.0.0-prototype.rc.2]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.2
|
[0.0.0-prototype.rc.2]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.2
|
||||||
[0.0.0-prototype-rc.1]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.1
|
[0.0.0-prototype-rc.1]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ local function TITLE(title: string)
|
||||||
print()
|
print()
|
||||||
print(testkit.color.white(title))
|
print(testkit.color.white(title))
|
||||||
end
|
end
|
||||||
|
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local mirror = require("../mirror/init")
|
local mirror = require("../mirror/init")
|
||||||
|
|
||||||
|
|
|
@ -662,16 +662,24 @@ local function noop(_self: Query, ...): () -> ()
|
||||||
end
|
end
|
||||||
|
|
||||||
local EmptyQuery = {
|
local EmptyQuery = {
|
||||||
__iter = noop,
|
__iter = iterNoop,
|
||||||
without = noop,
|
next = noop,
|
||||||
|
replace = noop,
|
||||||
|
without = function(self)
|
||||||
|
return self
|
||||||
|
end
|
||||||
}
|
}
|
||||||
EmptyQuery.__index = EmptyQuery
|
|
||||||
setmetatable(EmptyQuery, EmptyQuery)
|
|
||||||
|
|
||||||
export type Query = typeof(EmptyQuery)
|
export type Query = typeof(EmptyQuery)
|
||||||
|
|
||||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||||
|
|
||||||
|
local function replaceMult(row, columns, ...)
|
||||||
|
for i, column in columns do
|
||||||
|
column[row] = select(i, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function preparedQuery(compatibleArchetypes: { Archetype },
|
local function preparedQuery(compatibleArchetypes: { Archetype },
|
||||||
components: { i53? }, indices: { { number } })
|
components: { i53? }, indices: { { number } })
|
||||||
|
|
||||||
|
@ -783,17 +791,60 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
local it = {
|
local function iter()
|
||||||
__iter = function()
|
lastArchetype = 1
|
||||||
lastArchetype = 1
|
archetype = compatibleArchetypes[1]
|
||||||
archetype = compatibleArchetypes[1]
|
entities = archetype.entities
|
||||||
entities = archetype.entities
|
i = #entities
|
||||||
i = #entities
|
|
||||||
|
|
||||||
return queryNext
|
return queryNext
|
||||||
end,
|
end
|
||||||
|
|
||||||
|
local function replace(_, fn)
|
||||||
|
for i, archetype in compatibleArchetypes do
|
||||||
|
local tr = indices[i]
|
||||||
|
local columns = archetype.columns
|
||||||
|
|
||||||
|
for row in archetype.entities do
|
||||||
|
if queryLength == 1 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local pa = fn(a[row])
|
||||||
|
|
||||||
|
a[row] = pa
|
||||||
|
elseif queryLength == 2 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
|
||||||
|
a[row], b[row] = fn(a[row], b[row])
|
||||||
|
elseif queryLength == 3 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
local c = columns[tr[3]]
|
||||||
|
|
||||||
|
a[row], b[row], c[row] = fn(a[row], b[row], c[row])
|
||||||
|
elseif queryLength == 4 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
local c = columns[tr[3]]
|
||||||
|
local d = columns[tr[4]]
|
||||||
|
|
||||||
|
a[row], b[row], c[row], d[row] = fn(
|
||||||
|
a[row], b[row], c[row], d[row])
|
||||||
|
else
|
||||||
|
for i = 1, queryLength do
|
||||||
|
queryOutput[i] = columns[tr[i]][row]
|
||||||
|
end
|
||||||
|
replaceMult(row, columns, fn(unpack(queryOutput)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local it = {
|
||||||
|
__iter = iter,
|
||||||
next = queryNext,
|
next = queryNext,
|
||||||
without = without
|
without = without,
|
||||||
|
replace = replace
|
||||||
}
|
}
|
||||||
|
|
||||||
return setmetatable(it, it) :: any
|
return setmetatable(it, it) :: any
|
||||||
|
|
|
@ -449,9 +449,28 @@ TEST("world", function()
|
||||||
count += 1
|
count += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
print(count)
|
|
||||||
CHECK(count == 2)
|
CHECK(count == 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "should replace component data"
|
||||||
|
local world = jecs.World.new()
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:set(e, A, 1)
|
||||||
|
world:set(e, B, true)
|
||||||
|
world:set(e, C, "hello ")
|
||||||
|
|
||||||
|
world:query(A, B, C):replace(function(a, b, c)
|
||||||
|
return a * 2, not b, c.."world"
|
||||||
|
end)
|
||||||
|
|
||||||
|
CHECK(world:get(e, A) == 2)
|
||||||
|
CHECK(world:get(e, B) == false)
|
||||||
|
CHECK(world:get(e, C) == "hello world")
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
FINISH()
|
FINISH()
|
||||||
|
|
Loading…
Reference in a new issue