jecs/demo/src/ReplicatedStorage/systems/receive_replication.luau

122 lines
3.4 KiB
Text
Raw Normal View History

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]
2025-09-29 21:02:54 +00:00
if deser_id then
2025-07-02 16:25:20 +00:00
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)}`
2025-09-29 21:02:54 +00:00
-- local function ecs_deser_pairs_str(world, token)
-- local tokens = string.split(token, ",")
-- local rel = tonumber(tokens[1]) :: jecs.Entity
-- local tgt = tonumber(tokens[2]) :: jecs.Entity
2025-06-05 20:48:13 +00:00
2025-09-29 21:02:54 +00:00
-- rel = ecs_ensure_entity(world, rel)
-- tgt = ecs_ensure_entity(world, tgt)
-- return jecs.pair(rel, tgt)
-- end
local function ecs_deser_pairs(world, rel, tgt)
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)
2025-09-29 21:02:54 +00:00
return function(world: jecs.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
2025-09-29 21:02:54 +00:00
local id = (tonumber(ser_id) :: any) :: jecs.Entity
if jecs.IS_PAIR(id) and map.pair then
id = ecs_deser_pairs(world, map.relation, map.target)
elseif id then
2025-07-02 16:25:20 +00:00
id = ecs_ensure_entity(world, id)
2025-06-05 20:48:13 +00:00
end
2025-09-29 21:02:54 +00:00
-- if not id then
-- id = ecs_deser_pairs_str(world, ser_id)
-- else
-- id = ecs_ensure_entity(world, id)
-- end
local members = world:get(id, components.NetworkedMembers)
2025-06-05 20:48:13 +00:00
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-09-29 21:02:54 +00:00
local values = map.values :: { any }
2025-06-05 20:48:13 +00:00
for i, entity in set do
2025-07-02 16:25:20 +00:00
entity = ecs_ensure_entity(world, entity)
2025-09-29 21:02:54 +00:00
local value = values[i]
if members then
for _, member in members do
local data = value[member] :: {jecs.Entity} | jecs.Entity -- targets
if typeof(data) == "table" then
for pos, tgt in data :: { jecs.Entity } do
data[pos] = ecs_ensure_entity(world, tgt)
end
else
value[member] = ecs_ensure_entity(world, data :: any)
end
end
end
world:set(entity, id, value)
2025-06-05 20:48:13 +00:00
end
end
end
local removed = map.removed
if removed then
for _, entity in removed do
2025-07-02 16:25:20 +00:00
entity = ecs_ensure_entity(world, entity)
world:remove(entity, id)
2025-06-05 20:48:13 +00:00
end
end
end
end
end