Shared health

This commit is contained in:
IQuant 2024-05-10 23:43:32 +03:00
parent 4aa4c01646
commit 6d20864db9
5 changed files with 48 additions and 5 deletions

View file

@ -0,0 +1,14 @@
function damage_received( damage, message, entity_thats_responsible, is_fatal, projectile_thats_responsible )
local entity_id = GetUpdatedEntityID()
local damageModelComponent = EntityGetFirstComponentIncludingDisabled( entity_id, "DamageModelComponent" )
if damageModelComponent ~= nil then
local health = ComponentGetValue2( damageModelComponent, "hp" )
if health then
ComponentSetValue2( damageModelComponent, "hp", health + damage )
end
end
local host_ent = EntityGetWithTag("ew_host")[1]
if host_ent ~= nil then
EntityInflictDamage(host_ent, damage, "DAMAGE_CURSE", message, "NONE", 0, 0, entity_thats_responsible)
end
end

View file

@ -20,6 +20,7 @@ end
function net_handling.proxy.peer_id(_, value)
ctx.my_id = tonumber(value)
ctx.is_host = ctx.my_id == ctx.host_id
end
function net_handling.mod.player(peer_id, value)
@ -79,6 +80,7 @@ function net_handling.mod.host_player(peer_id, player_infos)
if player_infos[id] ~= nil then
local info = player_infos[id]
util.set_ent_health(player_data.entity, {info[1], info[2]})
util.set_ent_air(player_data.entity, {info[3], info[4]})
end
end
end

View file

@ -396,7 +396,9 @@ function player_fns.spawn_player_for(peer_id, x, y)
local new = EntityLoad("mods/quant.ew/files/entities/client.xml", x, y)
local new_playerdata = player_fns.make_playerdata_for(new, peer_id)
ctx.players[peer_id] = new_playerdata
if not ctx.is_host then
if ctx.is_host then
EntityAddComponent2(new, "LuaComponent", {script_damage_received = "mods/quant.ew/files/cbs/redirect_damage_to_host.lua"})
else
EntityAddComponent2(new, "LuaComponent", {script_damage_about_to_be_received = "mods/quant.ew/files/cbs/immortal.lua"})
end
end

View file

@ -52,6 +52,16 @@ function util.get_ent_health(entity)
return hp, max_hp
end
function util.get_ent_air(entity)
local damage_model = EntityGetFirstComponentIncludingDisabled(entity, "DamageModelComponent")
if damage_model == nil then
return 0, 0
end
local air = ComponentGetValue2(damage_model, "air_in_lungs")
local max_air = ComponentGetValue2(damage_model, "air_in_lungs_max")
return air, max_air
end
function util.set_ent_health(entity, hp_data)
local damage_model = EntityGetFirstComponentIncludingDisabled(entity, "DamageModelComponent")
if damage_model == nil then
@ -65,6 +75,19 @@ function util.set_ent_health(entity, hp_data)
end
end
function util.set_ent_air(entity, air_data)
local damage_model = EntityGetFirstComponentIncludingDisabled(entity, "DamageModelComponent")
if damage_model == nil then
return
end
if air_data[1] ~= nil then
ComponentSetValue2(damage_model, "air_in_lungs", air_data[1])
end
if air_data[2] ~= nil then
ComponentSetValue2(damage_model, "air_in_lungs_max", air_data[2])
end
end
function util.lerp(a, b, alpha)
return a * alpha + b * (1 - alpha)
end

View file

@ -80,12 +80,13 @@ function OnPlayerSpawned( player_entity ) -- This runs when player entity has be
GamePrint("My peer_id: "..ctx.my_id)
ctx.players[ctx.my_id] = my_player
ctx.ready = true
ctx.is_host = ctx.my_id == ctx.host_id
np.SetPauseState(4)
np.SetPauseState(0)
if not ctx.is_host then
if ctx.is_host then
EntityAddTag(player_entity, "ew_host")
else
EntityAddComponent2(player_entity, "LuaComponent", {script_damage_about_to_be_received = "mods/quant.ew/files/cbs/immortal.lua"})
end
@ -129,10 +130,11 @@ function on_world_pre_update_inner()
if ctx.is_host and GameGetFrameNum() % 4 == 3 then
local player_info = {}
local hp, max_hp = util.get_ent_health(my_player.entity)
for id, player_data in pairs(ctx.players) do
local entity = player_data.entity
local hp, max_hp = util.get_ent_health(entity)
player_info[id] = {hp, max_hp}
local air, max_air = util.get_ent_air(entity)
player_info[id] = {hp, max_hp, air, max_air}
end
net.send_host_player_info(player_info)
end