Self contained changetracker

This commit is contained in:
Ukendio 2024-07-28 14:17:44 +02:00
parent 18ead3ed19
commit d5de1ad425
2 changed files with 136 additions and 106 deletions

View file

@ -935,7 +935,7 @@ do
setmetatable(it, it) setmetatable(it, it)
function world_query(world: World, ...: any): Query function world_query(world: World, ...: any): Query
-- breaking? -- breaking
if (...) == nil then if (...) == nil then
error("Missing components") error("Missing components")
end end
@ -1161,26 +1161,28 @@ World.parent = world_parent
function World.new() function World.new()
local self = setmetatable({ local self = setmetatable({
archetypeIndex = {} :: { [string]: Archetype }, archetypeIndex = {} :: { [string]: Archetype },
archetypes = {} :: Archetypes, archetypes = {} :: Archetypes,
componentIndex = {} :: ComponentIndex, componentIndex = {} :: ComponentIndex,
entityIndex = { entityIndex = {
dense = {} :: { [i24]: i53 }, dense = {} :: { [i24]: i53 },
sparse = {} :: { [i53]: Record }, sparse = {} :: { [i53]: Record },
} :: EntityIndex, } :: EntityIndex,
hooks = { hooks = {
[EcsOnAdd] = {}, [EcsOnAdd] = {},
}, },
nextArchetypeId = 0, nextArchetypeId = 0,
nextComponentId = 0, nextComponentId = 0,
nextEntityId = 0, nextEntityId = 0,
ROOT_ARCHETYPE = (nil :: any) :: Archetype, ROOT_ARCHETYPE = (nil :: any) :: Archetype,
}, World) }, World)
self.ROOT_ARCHETYPE = archetype_of(self, {}) self.ROOT_ARCHETYPE = archetype_of(self, {})
-- Initialize built-in components for i = HI_COMPONENT_ID + 1, EcsRest do
entity_index_new_id(self.entityIndex, EcsChildOf) -- Initialize built-in components
entity_index_new_id(self.entityIndex, i)
end
return self return self
end end

View file

@ -564,10 +564,48 @@ TEST("world", function()
end) end)
type Tracker<T> = { track: (world: World, fn: (changes: {
added: () -> () -> (number, T),
removed: () -> () -> number,
changed: () -> () -> (number, T, T)
}) -> ()) -> ()
}
TEST("changetracker", function() type Entity<T = any> = number & { __nominal_type_dont_use: T }
local world = jecs.World.new()
local Previous = world:component() local ChangeTracker: <T>(component: Entity<T>) -> Tracker<T>
do
local world: World
local T
local PreviousT
local addedComponents
local removedComponents
local isTrivial
local added
local removed
local function changes_added()
added = true
local q = world:query(T):without(PreviousT)
return function()
local id, data = q:next()
if not id then
return nil
end
if isTrivial == nil then
isTrivial = typeof(data) ~= "table"
end
if not isTrivial then
data = table.clone(data)
end
addedComponents[id] = data
return id, data
end
end
local function shallowEq(a, b) local function shallowEq(a, b)
for k, v in a do for k, v in a do
@ -578,112 +616,102 @@ TEST("changetracker", function()
return true return true
end end
local function ChangeTracker(world, component) local function changes_changed()
local addedComponents = {} local q = world:query(T, PreviousT)
local removedComponents = {}
local previous = jecs.pair(Previous, component)
local isTrivial = nil
local function track(fn) return function()
local added = false local id, new, old = q:next()
local removed = false while true do
if not id then
local changes = {} return nil
function changes.added()
added = true
local q = world:query(component):without(previous)
return function()
local id, data = q:next()
if not id then
return nil
end
if isTrivial == nil then
isTrivial = typeof(data) ~= "table"
end
if not isTrivial then
data = table.clone(data)
end
addedComponents[id] = data
return id, data
end end
end
function changes.changed() if not isTrivial then
local q = world:query(component, previous) if not shallowEq(new, old) then
break
return function()
local id, new, old = q:next()
while true do
if not id then
return nil
end
if not isTrivial then
if not shallowEq(new, old) then
break
end
elseif new ~= old then
break
end
id, new, old = q:next()
end end
elseif new ~= old then
addedComponents[id] = new break
return id, old, new
end end
id, new, old = q:next()
end end
function changes.removed() addedComponents[id] = new
removed = true
local q = world:query(previous):without(component) return id, old, new
return function() end
local id = q:next() end
if id then
table.insert(removedComponents, id) local function changes_removed()
end removed = true
return id
end local q = world:query(PreviousT):without(T)
return function()
local id = q:next()
if id then
table.insert(removedComponents, id)
end end
return id
end
end
fn(changes) local changes = {
if not added then added = changes_added,
for _ in changes.added() do changed = changes_changed,
end removed = changes_removed,
end }
if not removed then local function track(worldToTrack, fn)
for _ in changes.removed() do world = worldToTrack
end added = true
end removed = true
for e, data in addedComponents do fn(changes)
world:set(e, previous, if isTrivial then data else table.clone(data))
end
for _, e in removedComponents do if not added then
world:remove(e, previous) for _ in changes_added() do
end end
end end
return { if not removed then
track = track for _ in changes_removed() do
} end
end
for e, data in addedComponents do
world:set(e, PreviousT, if isTrivial then data else table.clone(data))
end
for _, e in removedComponents do
world:remove(e, PreviousT)
end
end end
local tracker = { track = track }
function ChangeTracker<T>(component: Entity<T>): Tracker<T>
T = component
-- We just use jecs.Rest because people will probably not use it anyways
PreviousT = jecs.pair(jecs.Rest, T)
addedComponents = {}
removedComponents = {}
return tracker
end
end
TEST("changetracker", function()
local world = jecs.World.new()
do CASE "should allow change tracking" do CASE "should allow change tracking"
local Test = world:component() local Test = world:component() :: Entity<{ foo: number }>
local TestTracker = ChangeTracker(world, Test) local TestTracker = ChangeTracker(Test)
local e = world:entity() local e = world:entity()
world:set(e, Test, { foo = 11 }) world:set(e, Test, { foo = 11 })
TestTracker.track(function(changes) TestTracker.track(world, function(changes)
local added = 0 local added = 0
local changed = 0 local changed = 0
local removed = 0 local removed = 0
@ -705,7 +733,7 @@ TEST("changetracker", function()
test.foo = test.foo + 1 test.foo = test.foo + 1
end end
TestTracker.track(function(changes) TestTracker.track(world, function(changes)
local added = 0 local added = 0
local changed = 0 local changed = 0
local removed = 0 local removed = 0
@ -727,7 +755,7 @@ TEST("changetracker", function()
world:remove(e, Test) world:remove(e, Test)
TestTracker.track(function(changes) TestTracker.track(world, function(changes)
local added = 0 local added = 0
local changed = 0 local changed = 0
local removed = 0 local removed = 0