2024-08-03 03:48:48 +00:00
|
|
|
local jecs = require("@jecs")
|
2025-02-01 12:07:55 +00:00
|
|
|
local pair = jecs.pair
|
2024-08-03 03:48:48 +00:00
|
|
|
|
|
|
|
local world = jecs.World.new()
|
|
|
|
local Name = world:component()
|
|
|
|
|
|
|
|
local function named(ctr, name)
|
2024-10-12 20:18:11 +00:00
|
|
|
local e = ctr(world)
|
|
|
|
world:set(e, Name, name)
|
|
|
|
return e
|
2024-08-03 03:48:48 +00:00
|
|
|
end
|
|
|
|
local function name(e)
|
2024-10-12 20:18:11 +00:00
|
|
|
return world:get(e, Name)
|
2024-08-03 03:48:48 +00:00
|
|
|
end
|
|
|
|
|
2025-02-01 12:07:55 +00:00
|
|
|
local Position = named(world.component, "Position") :: jecs.Entity<vector>
|
2025-01-14 10:09:18 +00:00
|
|
|
local Previous = jecs.Rest
|
|
|
|
|
|
|
|
local added = world
|
|
|
|
:query(Position)
|
2025-02-01 12:07:55 +00:00
|
|
|
:without(pair(Previous, Position))
|
2025-01-14 10:09:18 +00:00
|
|
|
:cached()
|
|
|
|
local changed = world
|
2025-02-01 12:07:55 +00:00
|
|
|
:query(Position, pair(Previous, Position))
|
2025-01-14 10:09:18 +00:00
|
|
|
:cached()
|
|
|
|
local removed = world
|
2025-02-01 12:07:55 +00:00
|
|
|
:query(pair(Previous, Position))
|
2025-01-14 10:09:18 +00:00
|
|
|
:without(Position)
|
|
|
|
:cached()
|
2024-08-03 03:48:48 +00:00
|
|
|
|
2025-02-05 21:26:12 +00:00
|
|
|
|
2024-08-03 03:48:48 +00:00
|
|
|
local e1 = named(world.entity, "e1")
|
2025-02-01 12:07:55 +00:00
|
|
|
world:set(e1, Position, vector.create(10, 20, 30))
|
2024-08-03 03:48:48 +00:00
|
|
|
local e2 = named(world.entity, "e2")
|
2025-02-01 12:07:55 +00:00
|
|
|
world:set(e2, Position, vector.create(10, 20, 30))
|
|
|
|
for entity, p in added do
|
|
|
|
print(`Added {name(entity)}: \{{p.x}, {p.y}, {p.z}}`)
|
|
|
|
world:set(entity, pair(Previous, Position), p)
|
2025-01-14 10:09:18 +00:00
|
|
|
end
|
2024-08-03 03:48:48 +00:00
|
|
|
|
2025-02-01 12:07:55 +00:00
|
|
|
world:set(e1, Position, vector.create(999, 999, 1998))
|
2024-08-03 03:48:48 +00:00
|
|
|
|
2025-02-05 21:26:12 +00:00
|
|
|
for _, archetype in changed:archetypes() do
|
2025-01-14 10:09:18 +00:00
|
|
|
if new ~= old then
|
2025-02-01 12:07:55 +00:00
|
|
|
print(`{name(e)}'s Position changed from \{{old.x}, {old.y}, {old.z}\} to \{{new.x}, {new.y}, {new.z}\}`)
|
|
|
|
world:set(e, pair(Previous, Position), new)
|
2024-10-12 20:18:11 +00:00
|
|
|
end
|
2025-01-14 10:09:18 +00:00
|
|
|
end
|
2024-08-03 03:48:48 +00:00
|
|
|
|
|
|
|
world:remove(e2, Position)
|
|
|
|
|
2025-01-14 10:09:18 +00:00
|
|
|
for e in removed:iter() do
|
|
|
|
print(`Position was removed from {name(e)}`)
|
2025-02-01 12:07:55 +00:00
|
|
|
world:remove(e, pair(Previous, Position))
|
2025-01-14 10:09:18 +00:00
|
|
|
end
|
2024-08-03 03:48:48 +00:00
|
|
|
|
|
|
|
-- Output:
|
|
|
|
-- Added 265: {10, 20, 30}
|
|
|
|
-- Added 264: {10, 20, 30}
|
|
|
|
-- e1's Position changed from {10, 20, 30} to {999, 999, 1998}
|
|
|
|
-- Position was removed from e2
|