Spaces:
Sleeping
Sleeping
File size: 7,533 Bytes
defab59 | 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.Collections.Generic;
using Fusion;
using UnityEngine;
public class DinoGameManager : NetworkBehaviour
{
public float checkInterval = 0.5f;
public float countdownSeconds = 2f;
public float roundEndDelay = 3f;
public float matchRestartDelay = 6f;
public int maxRounds = 5;
public int winsToFinishMatch = 3;
public bool autoRestartMatch = true;
[Networked] public NetworkBool IsGameOver { get; set; }
[Networked] public NetworkBool IsGameStarted { get; set; }
[Networked] public TickTimer StartTimer { get; set; }
[Networked] public NetworkString<_32> WinnerName { get; set; }
[Networked] public int WinnerId { get; set; }
[Networked, Capacity(8)] public NetworkArray<int> WinCounts => default;
[Networked] public int CurrentRound { get; set; }
[Networked] public NetworkBool IsMatchOver { get; set; }
[Networked] public TickTimer RoundEndTimer { get; set; }
[Networked] public TickTimer MatchRestartTimer { get; set; }
public static DinoGameManager Instance { get; private set; }
double nextCheck;
bool matchReported;
void Awake()
{
Instance = this;
}
public override void Spawned()
{
if (Object.HasStateAuthority)
{
IsGameOver = false;
IsGameStarted = false;
IsMatchOver = false;
CurrentRound = 1;
StartTimer = TickTimer.CreateFromSeconds(Runner, countdownSeconds);
}
}
public override void FixedUpdateNetwork()
{
if (IsMatchOver)
{
if (Object.HasStateAuthority)
{
if (!matchReported)
{
matchReported = true;
RpcMatchOver(WinnerName.ToString());
ReportMatch();
}
if (autoRestartMatch)
{
if (!MatchRestartTimer.IsRunning)
MatchRestartTimer = TickTimer.CreateFromSeconds(Runner, matchRestartDelay);
if (MatchRestartTimer.Expired(Runner))
ResetMatch();
}
}
return;
}
if (IsGameOver)
{
if (!Object.HasStateAuthority)
return;
if (!RoundEndTimer.IsRunning)
RoundEndTimer = TickTimer.CreateFromSeconds(Runner, roundEndDelay);
if (RoundEndTimer.Expired(Runner))
{
if (ShouldEndMatch())
{
IsMatchOver = true;
return;
}
StartNextRound();
}
return;
}
if (!IsGameStarted)
{
if (Object.HasStateAuthority && StartTimer.Expired(Runner))
{
IsGameStarted = true;
}
return;
}
if (!Object.HasStateAuthority)
return;
if (Runner.SimulationTime < nextCheck)
return;
nextCheck = Runner.SimulationTime + checkInterval;
DinoPlayer[] players = FindObjectsOfType<DinoPlayer>();
int aliveCount = 0;
string lastAliveName = "";
int lastAliveId = -1;
foreach (DinoPlayer p in players)
{
if (p.IsAlive)
{
aliveCount++;
lastAliveName = GetDisplayName(p);
lastAliveId = GetPlayerId(p);
}
}
if (aliveCount <= 1 && players.Length > 0)
{
IsGameOver = true;
WinnerName = lastAliveName;
WinnerId = lastAliveId;
if (WinnerId >= 0 && WinnerId < WinCounts.Length)
WinCounts.Set(WinnerId, WinCounts.Get(WinnerId) + 1);
RpcGameOver(WinnerName.ToString());
}
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
void RpcGameOver(string winnerName)
{
Debug.Log($"Game Over - Winner: {winnerName}");
DinoAudio audio = DinoAudio.Instance;
if (audio != null)
audio.PlayWin();
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
void RpcMatchOver(string winnerName)
{
Debug.Log($"Match Over - Winner: {winnerName}");
}
public float GetCountdownRemaining()
{
if (IsGameStarted)
return 0f;
float? remaining = StartTimer.RemainingTime(Runner);
if (!remaining.HasValue)
return 0f;
return Mathf.Max(0f, remaining.Value);
}
string GetDisplayName(DinoPlayer p)
{
if (p.PlayerName.ToString() != "")
return p.PlayerName.ToString();
if (p.Object != null)
return $"P{p.Object.InputAuthority.PlayerId}";
return "Player";
}
int GetPlayerId(DinoPlayer p)
{
if (p.Object != null)
return p.Object.InputAuthority.PlayerId;
return -1;
}
bool ShouldEndMatch()
{
if (CurrentRound >= maxRounds)
return true;
for (int i = 0; i < WinCounts.Length; i++)
{
if (WinCounts.Get(i) >= winsToFinishMatch)
return true;
}
return false;
}
void StartNextRound()
{
CurrentRound = Mathf.Min(CurrentRound + 1, maxRounds);
ResetRound();
}
void ResetRound()
{
IsGameOver = false;
IsGameStarted = false;
StartTimer = TickTimer.CreateFromSeconds(Runner, countdownSeconds);
RoundEndTimer = TickTimer.None;
WinnerName = "";
WinnerId = -1;
CleanupRoundObjects();
RespawnPlayers();
}
void ResetMatch()
{
for (int i = 0; i < WinCounts.Length; i++)
WinCounts.Set(i, 0);
CurrentRound = 1;
IsMatchOver = false;
matchReported = false;
MatchRestartTimer = TickTimer.None;
DinoResultReporter reporter = FindObjectOfType<DinoResultReporter>();
if (reporter != null)
reporter.ResetReporter();
ResetRound();
}
void CleanupRoundObjects()
{
NetworkObject[] all = FindObjectsOfType<NetworkObject>();
foreach (NetworkObject obj in all)
{
if (obj == null || obj.Object == null)
continue;
if (obj.GetComponent<DinoPlayer>() != null)
continue;
if (obj.GetComponent<Obstacle>() != null ||
obj.GetComponent<ItemBox>() != null ||
obj.GetComponent<ItemProjectile>() != null ||
obj.GetComponent<ItemTrap>() != null ||
obj.GetComponent<MovingObstacle>() != null ||
obj.GetComponent<DroppingObstacle>() != null)
{
Runner.Despawn(obj);
}
}
}
void RespawnPlayers()
{
DinoPlayer[] players = FindObjectsOfType<DinoPlayer>();
DinoSpawnManager spawns = DinoSpawnManager.Instance;
foreach (DinoPlayer p in players)
{
if (p.Object == null)
continue;
int id = p.Object.InputAuthority.PlayerId;
Vector3 pos = spawns != null ? spawns.GetSpawnPosition(id) : new Vector3(0f, 0.5f, 0f);
p.ResetForRound(pos);
}
}
void ReportMatch()
{
DinoResultReporter reporter = FindObjectOfType<DinoResultReporter>();
if (reporter != null)
reporter.ReportMatch(this);
}
}
|