2025-06-05 20:48:13 +00:00
|
|
|
local types = require("../types")
|
|
|
|
local jecs = require(game:GetService("ReplicatedStorage").ecs)
|
|
|
|
local remotes = require("../remotes")
|
|
|
|
local collect = require("../collect")
|
2025-07-02 16:25:20 +00:00
|
|
|
local components = require("../components")
|
2025-06-05 20:48:13 +00:00
|
|
|
|
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
local client_ids: {[jecs.Entity]: jecs.Entity } = {}
|
2025-06-05 20:48:13 +00:00
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
local function ecs_ensure_entity(world: jecs.World, id: jecs.Entity)
|
|
|
|
local e = 0
|
|
|
|
|
|
|
|
local ser_id = id
|
|
|
|
local deser_id = client_ids[ser_id]
|
|
|
|
if deser_id then
|
|
|
|
if deser_id == 0 then
|
|
|
|
local new_id = world:entity()
|
|
|
|
client_ids[ser_id] = new_id
|
|
|
|
deser_id = new_id
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if not world:exists(ser_id)
|
|
|
|
or (world:contains(ser_id) and not world:get(ser_id, jecs.Name))
|
|
|
|
then
|
|
|
|
deser_id = world:entity()
|
2025-06-05 20:48:13 +00:00
|
|
|
else
|
2025-07-02 16:25:20 +00:00
|
|
|
if world:contains(ser_id) and world:get(ser_id, jecs.Name) then
|
|
|
|
deser_id = ser_id
|
|
|
|
else
|
|
|
|
deser_id = world:entity()
|
|
|
|
end
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
2025-07-02 16:25:20 +00:00
|
|
|
client_ids[ser_id] = deser_id
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
e = deser_id
|
2025-06-05 20:48:13 +00:00
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
return e
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
-- local rel_render = `e{jecs.ECS_ID(rel)}v{jecs.ECS_GENERATION(rel)}`
|
|
|
|
-- local tgt_render = `e{jecs.ECS_ID(tgt)}v{jecs.ECS_GENERATION(tgt)}`
|
|
|
|
local function ecs_deser_pairs(world, token)
|
|
|
|
local tokens = string.split(token, ",")
|
|
|
|
local rel = tonumber(tokens[1])
|
|
|
|
local tgt = tonumber(tokens[2])
|
2025-06-05 20:48:13 +00:00
|
|
|
|
2025-07-02 16:25:20 +00:00
|
|
|
rel = ecs_ensure_entity(world, rel)
|
|
|
|
tgt = ecs_ensure_entity(world, tgt)
|
2025-06-05 20:48:13 +00:00
|
|
|
|
|
|
|
return jecs.pair(rel, tgt)
|
|
|
|
end
|
|
|
|
|
|
|
|
local snapshots = collect(remotes.replication.OnClientEvent)
|
|
|
|
|
|
|
|
return function(world: types.World)
|
2025-07-02 16:25:20 +00:00
|
|
|
for entity in world:each(components.Destroy) do
|
|
|
|
client_ids[entity] = nil
|
|
|
|
end
|
2025-06-05 20:48:13 +00:00
|
|
|
for snapshot in snapshots do
|
2025-07-02 16:25:20 +00:00
|
|
|
for ser_id, map in snapshot do
|
|
|
|
local id = tonumber(ser_id)
|
|
|
|
if not id then
|
|
|
|
id = ecs_deser_pairs(world, ser_id)
|
|
|
|
else
|
|
|
|
id = ecs_ensure_entity(world, id)
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local set = map.set
|
|
|
|
if set then
|
|
|
|
if jecs.is_tag(world, id) then
|
|
|
|
for _, entity in set do
|
2025-07-02 16:25:20 +00:00
|
|
|
entity = ecs_ensure_entity(world, entity)
|
2025-06-05 20:48:13 +00:00
|
|
|
world:add(entity, id)
|
|
|
|
end
|
|
|
|
else
|
2025-07-02 16:25:20 +00:00
|
|
|
local t = os.clock()
|
2025-06-05 20:48:13 +00:00
|
|
|
local values = map.values
|
|
|
|
for i, entity in set do
|
2025-07-02 16:25:20 +00:00
|
|
|
entity = ecs_ensure_entity(world, entity)
|
|
|
|
world:set(entity, id, values[i])
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local removed = map.removed
|
|
|
|
|
|
|
|
if removed then
|
2025-06-30 20:41:29 +00:00
|
|
|
for _, entity in removed do
|
2025-07-02 16:25:20 +00:00
|
|
|
entity = ecs_ensure_entity(world, entity)
|
2025-06-30 20:41:29 +00:00
|
|
|
world:remove(entity, id)
|
2025-06-05 20:48:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|