Skip to content

Commit 38608eb

Browse files
fix: dead player not dead when reconnecting [MTT-2965] (#583)
* Moved HP LifeState initialization to ServerCharacter and adding LifeState initialization for reconnecting players with 0 HP or less
1 parent 821c8a3 commit 38608eb

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

Assets/BossRoom/Scripts/Server/Game/Character/ServerAnimationHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using Unity.Netcode;
34
using Unity.Netcode.Components;
45
using UnityEngine;
@@ -22,7 +23,18 @@ public override void OnNetworkSpawn()
2223
{
2324
if (IsServer)
2425
{
25-
m_NetworkLifeState.LifeState.OnValueChanged += OnLifeStateChanged;
26+
// Wait until next frame before registering on OnValueChanged to make sure NetworkAnimator has spawned before.
27+
StartCoroutine(WaitToRegisterOnLifeStateChanged());
28+
}
29+
}
30+
31+
IEnumerator WaitToRegisterOnLifeStateChanged()
32+
{
33+
yield return new WaitForEndOfFrame();
34+
m_NetworkLifeState.LifeState.OnValueChanged += OnLifeStateChanged;
35+
if (m_NetworkLifeState.LifeState.Value != LifeState.Alive)
36+
{
37+
OnLifeStateChanged(LifeState.Alive, m_NetworkLifeState.LifeState.Value);
2638
}
2739
}
2840

Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public override void OnNetworkSpawn()
8585
var startingAction = new ActionRequestData() { ActionTypeEnum = m_StartingAction };
8686
PlayAction(ref startingAction);
8787
}
88+
InitializeHitPoints();
8889
}
8990
}
9091

@@ -105,6 +106,24 @@ public override void OnNetworkDespawn()
105106
}
106107
}
107108

109+
void InitializeHitPoints()
110+
{
111+
NetState.HitPoints = NetState.CharacterClass.BaseHP.Value;
112+
113+
if (!IsNpc)
114+
{
115+
SessionPlayerData? sessionPlayerData = SessionManager<SessionPlayerData>.Instance.GetPlayerData(OwnerClientId);
116+
if (sessionPlayerData is {HasCharacterSpawned: true})
117+
{
118+
NetState.HitPoints = sessionPlayerData.Value.CurrentHitPoints;
119+
if (NetState.HitPoints <= 0)
120+
{
121+
NetState.LifeState = LifeState.Fainted;
122+
}
123+
}
124+
}
125+
}
126+
108127
/// <summary>
109128
/// Play a sequence of actions!
110129
/// </summary>

Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacterMovement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public bool IsMoving()
141141
/// </summary>
142142
public void CancelMove()
143143
{
144-
m_NavPath.Clear();
144+
m_NavPath?.Clear();
145145
m_MovementState = MovementState.Idle;
146146
}
147147

Assets/BossRoom/Scripts/Shared/Game/Entity/NetworkCharacterState.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,6 @@ public LifeState LifeState
110110
/// </summary>
111111
public event Action<Vector3> ReceivedClientInput;
112112

113-
public override void OnNetworkSpawn()
114-
{
115-
if (!IsServer) return;
116-
HitPoints = GetInitialHitPoints();
117-
}
118-
119-
int GetInitialHitPoints()
120-
{
121-
if (!IsNpc)
122-
{
123-
SessionPlayerData? sessionPlayerData = SessionManager<SessionPlayerData>.Instance.GetPlayerData(OwnerClientId);
124-
if (sessionPlayerData is { HasCharacterSpawned: true })
125-
{
126-
return sessionPlayerData.Value.CurrentHitPoints;
127-
}
128-
}
129-
130-
return CharacterClass.BaseHP.Value;
131-
}
132-
133113
/// <summary>
134114
/// RPC to send inputs for this character from a client to a server.
135115
/// </summary>

0 commit comments

Comments
 (0)