- Fix bugs/inconsistencies related to spectator shields.

This commit is contained in:
IQuant 2024-09-01 12:51:50 +03:00
parent 491f29948a
commit b51e4db750
3 changed files with 57 additions and 22 deletions

View file

@ -9,4 +9,5 @@
- `ctx.hook.on_client_spawned(peer_id, new_playerdata)` - `ctx.hook.on_client_spawned(peer_id, new_playerdata)`
- `ctx.hook.on_should_send_updates()` - called either when we connect, or somebody else connects (and sends "welcome" message) - `ctx.hook.on_should_send_updates()` - called either when we connect, or somebody else connects (and sends "welcome" message)
- `ctx.hook.on_draw_debug_window(imgui)` - `ctx.hook.on_draw_debug_window(imgui)`
- `ctx.hook.on_local_player_polymorphed(currently_polymorphed)`
- `ctx.hook.on_client_polymorphed(peer_id, player_data)`

View file

@ -22,6 +22,7 @@ local function entity_changed()
rpc.change_entity(nil) rpc.change_entity(nil)
wandfinder.set_wands_after_poly() wandfinder.set_wands_after_poly()
end end
ctx.hook.on_local_player_polymorphed(currently_polymorphed)
end end
function module.on_local_player_spawn() function module.on_local_player_spawn()
@ -131,6 +132,7 @@ function rpc.change_entity(seri_ent)
EntityKill(ctx.rpc_player_data.entity) EntityKill(ctx.rpc_player_data.entity)
player_fns.replace_player_entity(ent, ctx.rpc_player_data) player_fns.replace_player_entity(ent, ctx.rpc_player_data)
ctx.rpc_player_data.currently_polymorphed = true ctx.rpc_player_data.currently_polymorphed = true
ctx.hook.on_client_polymorphed(ctx.rpc_peer_id, ctx.rpc_player_data)
else else
if ctx.rpc_player_data.currently_polymorphed then if ctx.rpc_player_data.currently_polymorphed then
EntityKill(ctx.rpc_player_data.entity) EntityKill(ctx.rpc_player_data.entity)

View file

@ -2,24 +2,14 @@ local rpc = net.new_rpc_namespace()
local shield_entities = {} local shield_entities = {}
local function create_shield_for(spectator_peer_id, target_entity) local function delete_shields()
local ent = EntityLoad("mods/quant.ew/files/system/spectator_helps/shield_base.xml") for _, child in ipairs(EntityGetAllChildren(ctx.my_player.entity) or {}) do
EntityAddChild(target_entity, ent) if EntityGetName(child) == "spectator_shield" then
shield_entities[spectator_peer_id] = ent EntityKill(child)
end
-- EntityAddComponent2(ent, "EnergyShieldComponent", {}) end
end end
local spectating_which = rpc:create_var("spectating_which", function(new_value)
if shield_entities[ctx.rpc_peer_id] ~= nil then
EntityKill(shield_entities[ctx.rpc_peer_id])
shield_entities[ctx.rpc_peer_id] = nil
end
if ctx.rpc_peer_id ~= new_value and new_value ~= nil then
create_shield_for(ctx.rpc_peer_id, player_fns.peer_get_player_data(new_value).entity)
end
end)
local shield_angle = rpc:create_var("shield_angle", function(new_value) local shield_angle = rpc:create_var("shield_angle", function(new_value)
local shield_ent = shield_entities[ctx.rpc_peer_id] local shield_ent = shield_entities[ctx.rpc_peer_id]
if shield_ent ~= nil then if shield_ent ~= nil then
@ -28,6 +18,39 @@ local shield_angle = rpc:create_var("shield_angle", function(new_value)
end end
end) end)
local function create_shield_for(spectator_peer_id, target_entity)
local ent = EntityLoad("mods/quant.ew/files/system/spectator_helps/shield_base.xml")
EntityAddChild(target_entity, ent)
shield_entities[spectator_peer_id] = ent
if shield_angle.values[spectator_peer_id] ~= nil then
local x, y = EntityGetTransform(ent)
EntitySetTransform(ent, x, y, shield_angle.values[spectator_peer_id])
end
end
local function maybe_clean_existing_shield(spectator_peer_id)
if shield_entities[spectator_peer_id] ~= nil then
EntityKill(shield_entities[spectator_peer_id])
shield_entities[spectator_peer_id] = nil
end
end
local spectating_which = rpc:create_var("spectating_which", function(new_value)
maybe_clean_existing_shield(ctx.rpc_peer_id)
if ctx.rpc_peer_id ~= new_value and new_value ~= nil then
create_shield_for(ctx.rpc_peer_id, player_fns.peer_get_player_data(new_value).entity)
end
end)
local function recreate_shields_of(updated_peer_id)
for peer_id, target_peer_id in pairs(spectating_which.values) do
if target_peer_id == updated_peer_id then
maybe_clean_existing_shield(peer_id)
create_shield_for(peer_id, player_fns.peer_get_player_data(target_peer_id).entity)
end
end
end
local module = {} local module = {}
local function is_acceptable_help_target(spectating_over) local function is_acceptable_help_target(spectating_over)
@ -43,6 +66,11 @@ local function is_acceptable_help_target(spectating_over)
return true return true
end end
function module.on_local_player_polymorphed(_currently_polymorphed)
delete_shields()
recreate_shields_of(ctx.my_id)
end
function module.on_world_update() function module.on_world_update()
local notplayer_active = GameHasFlagRun("ew_flag_notplayer_active") local notplayer_active = GameHasFlagRun("ew_flag_notplayer_active")
if notplayer_active and ctx.spectating_over_peer_id ~= nil and is_acceptable_help_target(ctx.spectating_over_peer_id) then if notplayer_active and ctx.spectating_over_peer_id ~= nil and is_acceptable_help_target(ctx.spectating_over_peer_id) then
@ -61,11 +89,15 @@ end
function module.on_local_player_spawn() function module.on_local_player_spawn()
-- Cleanup after restarts -- Cleanup after restarts
for _, child in ipairs(EntityGetAllChildren(ctx.my_player.entity) or {}) do delete_shields()
if EntityGetName(child) == "spectator_shield" then end
EntityKill(child)
end function module.on_client_polymorphed(peer_id, _player_data)
end recreate_shields_of(peer_id)
end
function module.on_client_spawned(peer_id, _new_playerdata)
recreate_shields_of(peer_id)
end end
return module return module