Item sync!

This commit is contained in:
IQuant 2024-05-13 16:52:29 +03:00
parent 488a828a18
commit c141e40b6b
4 changed files with 34 additions and 1 deletions

View file

@ -10,6 +10,7 @@ ctx.init = function()
ctx.entity_by_remote_id = {}
ctx.run_ended = false
ctx.player_data_by_local_entity = {}
ctx.item_prevent_localize = {}
end
return ctx

View file

@ -60,6 +60,10 @@ end
function item_sync.host_localize_item(gid, peer_id)
-- GamePrint("Localize "..g_id)
if ctx.item_prevent_localize[gid] then
GamePrint("Item localize for "..gid.." prevented")
end
ctx.item_prevent_localize[gid] = true
if peer_id ~= ctx.my_id then
item_sync.remove_item_with_id(gid)
end
@ -80,6 +84,7 @@ function item_sync.make_item_global(item)
GamePrint(item_sync.get_global_item_id(item))
local item_data = inventory_helper.serialize_single_item(item)
item_data.g_id = id
ctx.item_prevent_localize[id] = false
ctx.lib.net.send_make_global(item_data)
end
@ -109,6 +114,16 @@ function item_sync.host_upload_items(my_player)
end
function item_sync.client_tick(my_player)
if GameGetFrameNum() % 5 == 4 then
mark_in_inventory(my_player)
end
local thrown_item = get_global_ent("ew_thrown")
if thrown_item ~= nil then
GamePrint("Uploading item")
ctx.lib.net.send_item_upload(inventory_helper.serialize_single_item(thrown_item))
EntityKill(thrown_item)
end
local picked_item = get_global_ent("ew_picked")
if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then
GamePrint("Picked up "..picked_item)
@ -117,4 +132,11 @@ function item_sync.client_tick(my_player)
end
end
function item_sync.upload(item_data)
local item = inventory_helper.deserialize_single_item(item_data)
EntityAddTag(item, "ew_global_item")
item_sync.ensure_notify_component(item)
item_sync.make_item_global(item)
end
return item_sync

View file

@ -143,4 +143,8 @@ function net.send_localize_request(item_id)
net.send("item_localize_req", item_id, true)
end
function net.send_item_upload(item_data)
net.send("item_upload", item_data, true)
end
return net

View file

@ -177,9 +177,15 @@ function net_handling.mod.item_localize_req(peer_id, gid)
if not ctx.is_host then
return
end
-- TODO check for race condition from several clients
GamePrint("localize req "..peer_id.." gid "..gid)
item_sync.host_localize_item(gid, peer_id)
end
function net_handling.mod.item_upload(peer_id, item_data)
if not ctx.is_host then
return
end
item_sync.upload(item_data)
end
return net_handling