188 lines
5.7 KiB
C#
188 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using Data.ScriptableObjects;
|
|
using FishNet.Connection;
|
|
using FishNet.Object;
|
|
using FishNet.Object.Synchronizing;
|
|
using Types;
|
|
using UnityEngine;
|
|
|
|
namespace Managers
|
|
{
|
|
public enum GameStatus
|
|
{
|
|
WaitingForPlayers,
|
|
Playing,
|
|
Paused,
|
|
GameOver
|
|
}
|
|
|
|
public class GameManager : NetworkBehaviour
|
|
{
|
|
[SerializeField] private SaveManager saveManager;
|
|
[SerializeField] private HotkeyManager hotkeyManager;
|
|
[SerializeField] private AudioManager audioManager;
|
|
[SerializeField] private UIManager uiManager;
|
|
|
|
// person who's running the game, owner.
|
|
[SerializeField] private PlayerManager gameManagerPlayer;
|
|
// [SerializeField] private List<PlayerManager> gameManagerPlayerData;
|
|
|
|
|
|
[SerializeField] public ItemDatabase itemDatabase;
|
|
|
|
// int = clientID
|
|
[SerializeField] private readonly SyncDictionary<int, GamePlayer> _players = new();
|
|
|
|
public readonly SyncVar<GameStatus> gameStatus = new(GameStatus.WaitingForPlayers);
|
|
|
|
public static GameManager Instance { get; set; }
|
|
|
|
private void Awake()
|
|
{
|
|
// uiManager = GetComponent<UIManager>();
|
|
Debug.Log("Starting GameManager");
|
|
itemDatabase = Resources.Load<ItemDatabase>("GameItems/ItemDatabase");
|
|
|
|
_players.OnChange += OnPlayersChange;
|
|
gameStatus.OnChange += OnStatusChange;
|
|
|
|
if (Instance != null) Destroy(this);
|
|
Instance = this;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this)
|
|
Instance = null;
|
|
|
|
Debug.Log("GameManager destroyed");
|
|
}
|
|
|
|
private void OnStatusChange(GameStatus prev, GameStatus next, bool asServer)
|
|
{
|
|
Debug.Log("Current Game Status changed: " + prev + " -> " + next);
|
|
}
|
|
|
|
private void OnCurrentStateChange(GameState prev, GameState next, bool asserver)
|
|
{
|
|
Debug.Log("Current Game State changed: " + prev + " -> " + next);
|
|
}
|
|
|
|
private void OnPlayersChange(SyncDictionaryOperation op, int clientId,
|
|
GamePlayer newPlayer, bool asServer)
|
|
{
|
|
// log changes
|
|
Debug.Log($"Players changed: {op} {clientId} {newPlayer} {asServer}");
|
|
}
|
|
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
public void RegisterPlayerServerRpc(NetworkConnection conn, GamePlayer player)
|
|
{
|
|
Debug.Log("Registering player...");
|
|
|
|
if (player != null && !_players.ContainsKey(conn.ClientId))
|
|
{
|
|
_players.Add(conn.ClientId, player);
|
|
_players.Dirty(conn.ClientId);
|
|
Debug.Log($"Player registered: {conn.ClientId}");
|
|
OnPlayerRegisteredObserversRpc(conn.ClientId);
|
|
}
|
|
}
|
|
|
|
[ObserversRpc]
|
|
private void OnPlayerRegisteredObserversRpc(int clientId)
|
|
{
|
|
Debug.Log($"Player with ID {clientId} has joined the game!");
|
|
// You can add any client-side logic here when a new player joins
|
|
}
|
|
|
|
public Dictionary<int, GamePlayer> GetPlayers()
|
|
{
|
|
return new Dictionary<int, GamePlayer>(_players);
|
|
} // ReSharper disable Unity.PerformanceAnalysis
|
|
public override void OnStartNetwork()
|
|
{
|
|
base.OnStartNetwork();
|
|
Debug.Log("Network started");
|
|
}
|
|
|
|
// method to handle player connections
|
|
// ReSharper disable Unity.PerformanceAnalysis
|
|
public override void OnStartClient()
|
|
{
|
|
Debug.Log("Client connected to server");
|
|
base.OnStartClient();
|
|
if (IsServerInitialized)
|
|
{
|
|
// Server logic for new player connections
|
|
}
|
|
}
|
|
|
|
|
|
// get player by client id
|
|
public GamePlayer GetPlayer(int clientId)
|
|
{
|
|
return _players[clientId];
|
|
}
|
|
|
|
public override void OnStopClient()
|
|
{
|
|
base.OnStopClient();
|
|
// client specific cleanup code?
|
|
|
|
if (IsServerInitialized)
|
|
{
|
|
// server logic for disconnection
|
|
}
|
|
// client logic for disconnection
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
public void StartGameServerRpc()
|
|
{
|
|
UIManager.ShowMainMenuScreen(false);
|
|
// AudioManager.PlayMusic("MainTheme");
|
|
}
|
|
|
|
[ObserversRpc]
|
|
public void StartGameObserversRpc()
|
|
{
|
|
UIManager.ShowMainMenuScreen(false);
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
// client ot server comm? i.e client is telling the server to run this code
|
|
public void EndGameServerRpc()
|
|
{
|
|
Debug.Log("ServerRPC Game Over");
|
|
UIManager.ShowGameOverScreen(true);
|
|
// AudioManager.PlayMusic("GameOverTheme");
|
|
}
|
|
|
|
[ObserversRpc]
|
|
// server to client communication i.e server is telling the client to run this code
|
|
public void EndGameObserversRpc()
|
|
{
|
|
Debug.Log("ObserverRPC Game Over");
|
|
UIManager.ShowGameOverScreen(true);
|
|
}
|
|
|
|
public void StartGame()
|
|
{
|
|
if (IsServerInitialized)
|
|
StartGameServerRpc();
|
|
else
|
|
StartGameObserversRpc();
|
|
}
|
|
|
|
// add player
|
|
|
|
|
|
public void AddPlayer(GamePlayer gamePlayer)
|
|
{
|
|
_players.Add(Owner.ClientId, gamePlayer);
|
|
_players.Dirty(Owner.ClientId);
|
|
}
|
|
}
|
|
} |