SWS-Smarta / project /Assets /Scripts /Game /DinoGameManager.cs
alfapro's picture
Clean deploy: Code only for SWS
defab59
Raw History Blame Contribute Delete
7.53 kB
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);
}
}