Skip to content

Commit f74f736

Browse files
committed
Adding check for gameover when player disconnects
1 parent f5c919f commit f74f736

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public override void OnNetworkSpawn()
7171
m_NetPortal = GameObject.FindGameObjectWithTag("GameNetPortal").GetComponent<GameNetPortal>();
7272
m_ServerNetPortal = m_NetPortal.GetComponent<ServerGameNetPortal>();
7373

74+
NetworkManager.OnClientDisconnectCallback += OnClientDisconnect;
7475
NetworkManager.SceneManager.OnSceneEvent += OnClientSceneChanged;
7576

7677
DoInitialSpawnIfPossible();
@@ -99,6 +100,15 @@ private bool DoInitialSpawnIfPossible()
99100
return false;
100101
}
101102

103+
void OnClientDisconnect(ulong clientId)
104+
{
105+
if (clientId != NetworkManager.LocalClientId)
106+
{
107+
// If a client disconnects, check for game over in case all other players are already down
108+
CheckForGameOver();
109+
}
110+
}
111+
102112
public void OnClientSceneChanged(SceneEvent sceneEvent)
103113
{
104114
if (sceneEvent.SceneEventType != SceneEventType.LoadComplete) return;
@@ -226,8 +236,12 @@ void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message)
226236
case CharacterTypeEnum.Archer:
227237
case CharacterTypeEnum.Mage:
228238
case CharacterTypeEnum.Rogue:
229-
// Every time a player's life state changes we check to see if game is over
230-
OnHeroLifeStateChanged(message.NewLifeState);
239+
// Every time a player's life state changes to fainted we check to see if game is over
240+
if (message.NewLifeState == LifeState.Fainted)
241+
{
242+
CheckForGameOver();
243+
}
244+
231245
break;
232246
case CharacterTypeEnum.ImpBoss:
233247
if (message.NewLifeState == LifeState.Dead)
@@ -240,24 +254,20 @@ void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message)
240254
}
241255
}
242256

243-
private void OnHeroLifeStateChanged(LifeState lifeState)
257+
void CheckForGameOver()
244258
{
245-
// If this Hero is down, check the rest of the party also
246-
if (lifeState == LifeState.Fainted)
259+
// Check the life state of all players in the scene
260+
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
247261
{
248-
// Check the life state of all players in the scene
249-
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
262+
// if any player is alive just retun
263+
if (serverCharacter.NetState && serverCharacter.NetState.LifeState == LifeState.Alive)
250264
{
251-
// if any player is alive just retun
252-
if (serverCharacter.NetState && serverCharacter.NetState.LifeState == LifeState.Alive)
253-
{
254-
return;
255-
}
265+
return;
256266
}
257-
258-
// If we made it this far, all players are down! switch to post game
259-
StartCoroutine(CoroGameOver(k_LoseDelay, false));
260267
}
268+
269+
// If we made it this far, all players are down! switch to post game
270+
StartCoroutine(CoroGameOver(k_LoseDelay, false));
261271
}
262272

263273
void BossDefeated()

0 commit comments

Comments
 (0)