-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerBossRoomState.cs
271 lines (225 loc) · 10.4 KB
/
ServerBossRoomState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.BossRoom.ConnectionManagement;
using Unity.BossRoom.Gameplay.GameplayObjects;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.BossRoom.Gameplay.Messages;
using Unity.BossRoom.Infrastructure;
using Unity.BossRoom.Utils;
using Unity.Multiplayer.Samples.BossRoom;
using Unity.Multiplayer.Samples.Utilities;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
using VContainer;
using Random = UnityEngine.Random;
namespace Unity.BossRoom.Gameplay.GameState
{
/// <summary>
/// Server specialization of core BossRoom game logic.
/// </summary>
[RequireComponent(typeof(NetcodeHooks))]
public class ServerBossRoomState : GameStateBehaviour
{
[FormerlySerializedAs("m_NetworkWinState")]
[SerializeField]
PersistentGameState persistentGameState;
[SerializeField]
NetcodeHooks m_NetcodeHooks;
[SerializeField]
[Tooltip("Make sure this is included in the NetworkManager's list of prefabs!")]
private NetworkObject m_PlayerPrefab;
[SerializeField]
[Tooltip("A collection of locations for spawning players")]
private Transform[] m_PlayerSpawnPoints;
private List<Transform> m_PlayerSpawnPointsList = null;
public override GameState ActiveState { get { return GameState.BossRoom; } }
// Wait time constants for switching to post game after the game is won or lost
private const float k_WinDelay = 7.0f;
private const float k_LoseDelay = 2.5f;
/// <summary>
/// Has the ServerBossRoomState already hit its initial spawn? (i.e. spawned players following load from character select).
/// </summary>
public bool InitialSpawnDone { get; private set; }
/// <summary>
/// Keeping the subscriber during this GameState's lifetime to allow disposing of subscription and re-subscribing
/// when despawning and spawning again.
/// </summary>
[Inject] ISubscriber<LifeStateChangedEventMessage> m_LifeStateChangedEventMessageSubscriber;
[Inject] ConnectionManager m_ConnectionManager;
[Inject] PersistentGameState m_PersistentGameState;
protected override void Awake()
{
base.Awake();
m_NetcodeHooks.OnNetworkSpawnHook += OnNetworkSpawn;
m_NetcodeHooks.OnNetworkDespawnHook += OnNetworkDespawn;
}
void OnNetworkSpawn()
{
if (!NetworkManager.Singleton.IsServer)
{
enabled = false;
return;
}
m_PersistentGameState.Reset();
m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
NetworkManager.Singleton.SceneManager.OnSynchronizeComplete += OnSynchronizeComplete;
SessionManager<SessionPlayerData>.Instance.OnSessionStarted();
}
void OnNetworkDespawn()
{
m_LifeStateChangedEventMessageSubscriber?.Unsubscribe(OnLifeStateChangedEventMessage);
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnect;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= OnLoadEventCompleted;
NetworkManager.Singleton.SceneManager.OnSynchronizeComplete -= OnSynchronizeComplete;
}
protected override void OnDestroy()
{
m_LifeStateChangedEventMessageSubscriber?.Unsubscribe(OnLifeStateChangedEventMessage);
if (m_NetcodeHooks)
{
m_NetcodeHooks.OnNetworkSpawnHook -= OnNetworkSpawn;
m_NetcodeHooks.OnNetworkDespawnHook -= OnNetworkDespawn;
}
base.OnDestroy();
}
void OnSynchronizeComplete(ulong clientId)
{
if (InitialSpawnDone && !PlayerServerCharacter.GetPlayerServerCharacter(clientId))
{
//somebody joined after the initial spawn. This is a Late Join scenario. This player may have issues
//(either because multiple people are late-joining at once, or because some dynamic entities are
//getting spawned while joining. But that's not something we can fully address by changes in
//ServerBossRoomState.
SpawnPlayer(clientId, true);
}
}
void OnLoadEventCompleted(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
{
if (!InitialSpawnDone && loadSceneMode == LoadSceneMode.Single)
{
InitialSpawnDone = true;
foreach (var kvp in NetworkManager.Singleton.ConnectedClients)
{
SpawnPlayer(kvp.Key, false);
}
}
}
void OnClientDisconnect(ulong clientId)
{
if (clientId != NetworkManager.Singleton.LocalClientId)
{
// If a client disconnects, check for game over in case all other players are already down
StartCoroutine(WaitToCheckForGameOver());
}
}
IEnumerator WaitToCheckForGameOver()
{
// Wait until next frame so that the client's player character has despawned
yield return null;
CheckForGameOver();
}
void SpawnPlayer(ulong clientId, bool lateJoin)
{
Transform spawnPoint = null;
if (m_PlayerSpawnPointsList == null || m_PlayerSpawnPointsList.Count == 0)
{
m_PlayerSpawnPointsList = new List<Transform>(m_PlayerSpawnPoints);
}
Debug.Assert(m_PlayerSpawnPointsList.Count > 0,
$"PlayerSpawnPoints array should have at least 1 spawn points.");
int index = Random.Range(0, m_PlayerSpawnPointsList.Count);
spawnPoint = m_PlayerSpawnPointsList[index];
m_PlayerSpawnPointsList.RemoveAt(index);
var playerNetworkObject = NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId);
var newPlayer = Instantiate(m_PlayerPrefab, Vector3.zero, Quaternion.identity);
var newPlayerCharacter = newPlayer.GetComponent<ServerCharacter>();
var physicsTransform = newPlayerCharacter.physicsWrapper.Transform;
if (spawnPoint != null)
{
physicsTransform.SetPositionAndRotation(spawnPoint.position, spawnPoint.rotation);
}
var persistentPlayerExists = playerNetworkObject.TryGetComponent(out PersistentPlayer persistentPlayer);
Assert.IsTrue(persistentPlayerExists,
$"Matching persistent PersistentPlayer for client {clientId} not found!");
// pass character type from persistent player to avatar
var networkAvatarGuidStateExists =
newPlayer.TryGetComponent(out NetworkAvatarGuidState networkAvatarGuidState);
Assert.IsTrue(networkAvatarGuidStateExists,
$"NetworkCharacterGuidState not found on player avatar!");
// if reconnecting, set the player's position and rotation to its previous state
if (lateJoin)
{
SessionPlayerData? sessionPlayerData = SessionManager<SessionPlayerData>.Instance.GetPlayerData(clientId);
if (sessionPlayerData is { HasCharacterSpawned: true })
{
physicsTransform.SetPositionAndRotation(sessionPlayerData.Value.PlayerPosition, sessionPlayerData.Value.PlayerRotation);
}
}
networkAvatarGuidState.AvatarGuid.Value =
persistentPlayer.NetworkAvatarGuidState.AvatarGuid.Value;
// pass name from persistent player to avatar
if (newPlayer.TryGetComponent(out NetworkNameState networkNameState))
{
networkNameState.Name.Value = persistentPlayer.NetworkNameState.Name.Value;
}
// spawn players characters with destroyWithScene = true
newPlayer.SpawnWithOwnership(clientId, true);
}
void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message)
{
switch (message.CharacterType)
{
case CharacterTypeEnum.Tank:
case CharacterTypeEnum.Archer:
case CharacterTypeEnum.Mage:
case CharacterTypeEnum.Rogue:
// Every time a player's life state changes to fainted we check to see if game is over
if (message.NewLifeState == LifeState.Fainted)
{
CheckForGameOver();
}
break;
case CharacterTypeEnum.ImpBoss:
if (message.NewLifeState == LifeState.Dead)
{
BossDefeated();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
void CheckForGameOver()
{
// Check the life state of all players in the scene
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
{
// if any player is alive just return
if (serverCharacter && serverCharacter.LifeState == LifeState.Alive)
{
return;
}
}
// If we made it this far, all players are down! switch to post game
StartCoroutine(CoroGameOver(k_LoseDelay, false));
}
void BossDefeated()
{
// Boss is dead - set game won to true
StartCoroutine(CoroGameOver(k_WinDelay, true));
}
IEnumerator CoroGameOver(float wait, bool gameWon)
{
m_PersistentGameState.SetWinState(gameWon ? WinState.Win : WinState.Loss);
// wait 5 seconds for game animations to finish
yield return new WaitForSeconds(wait);
SceneLoaderWrapper.Instance.LoadScene("PostGame", useNetworkSceneManager: true);
}
}
}