2025-02-19 20:11:57 -05:00
|
|
|
using System.Collections.Generic;
|
2025-02-17 18:23:05 -05:00
|
|
|
using UnityEngine;
|
2025-03-07 23:32:42 -05:00
|
|
|
using UnityEngine.Events;
|
2025-03-07 11:56:19 -05:00
|
|
|
using UnityEngine.InputSystem;
|
2025-02-17 18:23:05 -05:00
|
|
|
|
|
|
|
|
public class GameManager : MonoBehaviour
|
|
|
|
|
{
|
2025-02-28 14:01:55 -05:00
|
|
|
public static GameManager Instance { get; private set; }
|
|
|
|
|
public int maxLives = 3;
|
2025-03-06 01:27:42 -05:00
|
|
|
public delegate void GameEvent();
|
|
|
|
|
public event GameEvent StartGameEvent;
|
|
|
|
|
public event GameEvent EndGameEvent;
|
|
|
|
|
public static List<GameObject> players = new List<GameObject>();
|
2025-03-07 16:25:59 -05:00
|
|
|
public static List<Color> playerColors = new List<Color>();
|
2025-03-07 16:50:04 -05:00
|
|
|
public float offset = 1f;
|
|
|
|
|
|
2025-03-07 23:32:42 -05:00
|
|
|
public static bool music = true;
|
|
|
|
|
|
2025-03-07 16:50:04 -05:00
|
|
|
public bool gameOver = false;
|
2025-03-20 14:45:29 -04:00
|
|
|
public GameTimer gameTimer;
|
|
|
|
|
public static Dictionary<GameObject, float> playerHoldTimes = new Dictionary<GameObject, float>();
|
2025-02-28 14:01:55 -05:00
|
|
|
|
2025-03-04 20:10:28 -05:00
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
if (Instance == null)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 13:17:06 -05:00
|
|
|
private void Start()
|
|
|
|
|
{
|
2025-03-07 23:32:42 -05:00
|
|
|
MusicManager.Instance.StartPlaylist();
|
2025-02-28 13:17:06 -05:00
|
|
|
StartGame();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 18:16:51 -05:00
|
|
|
public void StartGame()
|
|
|
|
|
{
|
2025-03-20 14:45:29 -04:00
|
|
|
GameManager.playerHoldTimes.Clear();
|
2025-03-07 16:50:04 -05:00
|
|
|
if (GameManager.players.Count == 0) return;
|
|
|
|
|
|
2025-03-06 01:27:42 -05:00
|
|
|
StartGameEvent?.Invoke();
|
2025-03-03 18:23:54 -05:00
|
|
|
print("Starting game with mode: " + gameMode + " and map: " + map);
|
2025-02-26 18:16:51 -05:00
|
|
|
if (gameMode == GameMode.freeForAll)
|
|
|
|
|
{
|
2025-03-06 01:27:42 -05:00
|
|
|
foreach (GameObject player in players)
|
|
|
|
|
{
|
2025-03-07 16:50:04 -05:00
|
|
|
player.transform.position = spawnPosition + (players.IndexOf(player) * Vector2.right * offset);
|
2025-03-17 19:59:41 -04:00
|
|
|
player.GetComponent<Damageable>().lives = 5;
|
2025-03-06 01:27:42 -05:00
|
|
|
}
|
2025-02-26 18:16:51 -05:00
|
|
|
}
|
|
|
|
|
if (gameMode == GameMode.keepAway)
|
|
|
|
|
{
|
2025-03-20 14:45:29 -04:00
|
|
|
gameTimer.StartTimer();
|
2025-03-06 01:27:42 -05:00
|
|
|
foreach (GameObject player in players)
|
|
|
|
|
{
|
|
|
|
|
player.transform.position = spawnPosition;
|
2025-03-17 19:59:41 -04:00
|
|
|
player.GetComponent<Damageable>().lives = 0;
|
2025-03-06 01:27:42 -05:00
|
|
|
}
|
2025-02-26 18:16:51 -05:00
|
|
|
}
|
|
|
|
|
if (gameMode == GameMode.obstacleCourse)
|
|
|
|
|
{
|
2025-03-06 01:27:42 -05:00
|
|
|
foreach (GameObject player in players)
|
|
|
|
|
{
|
|
|
|
|
player.transform.position = spawnPosition;
|
|
|
|
|
}
|
2025-02-26 18:16:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-17 18:23:05 -05:00
|
|
|
public enum GameMode
|
|
|
|
|
{
|
|
|
|
|
freeForAll,
|
2025-02-17 19:02:14 -05:00
|
|
|
keepAway,
|
|
|
|
|
obstacleCourse
|
2025-02-17 18:23:05 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-06 01:27:42 -05:00
|
|
|
public static GameMode gameMode = GameMode.freeForAll; // loads a default gamemode as a safety net
|
|
|
|
|
public static string map = "Platformer With Headroom"; // loads a default map as a safety net
|
2025-02-19 20:11:57 -05:00
|
|
|
public Vector2 spawnPosition;
|
2025-03-17 19:59:41 -04:00
|
|
|
public Vector2 hatSpawnPosition;
|
2025-02-19 20:11:57 -05:00
|
|
|
|
2025-03-07 10:03:16 -05:00
|
|
|
public void PlayerDied(Damageable player)
|
2025-02-28 14:01:55 -05:00
|
|
|
{
|
|
|
|
|
if (gameMode == GameMode.freeForAll)
|
|
|
|
|
{
|
2025-03-07 10:03:16 -05:00
|
|
|
player.lives--;
|
2025-03-08 13:33:19 -05:00
|
|
|
if (player.lives <= 0 && !gameOver)
|
2025-02-28 14:01:55 -05:00
|
|
|
{
|
2025-03-08 13:33:19 -05:00
|
|
|
player.gameObject.SetActive(false);
|
|
|
|
|
if (AlivePlayers().Count <= 1)
|
|
|
|
|
{
|
|
|
|
|
GameOver();
|
|
|
|
|
}
|
2025-02-28 14:01:55 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-03-07 10:03:16 -05:00
|
|
|
RespawnPlayer(player.gameObject);
|
2025-02-28 14:01:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (gameMode == GameMode.keepAway)
|
|
|
|
|
{
|
2025-03-17 19:59:41 -04:00
|
|
|
RespawnPlayer(player.gameObject);
|
2025-02-28 14:01:55 -05:00
|
|
|
}
|
|
|
|
|
if (gameMode == GameMode.obstacleCourse)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RespawnPlayer(GameObject player)
|
|
|
|
|
{
|
|
|
|
|
RespawnOnTriggerEnter respawnScript = player.GetComponent<RespawnOnTriggerEnter>();
|
|
|
|
|
if (respawnScript != null)
|
|
|
|
|
{
|
|
|
|
|
player.transform.position = respawnScript.spawnPoint;
|
|
|
|
|
player.GetComponent<Damageable>().ResetDamage();
|
2025-03-07 10:03:16 -05:00
|
|
|
player.GetComponent<Damageable>().Respawn();
|
2025-02-28 14:01:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 14:45:29 -04:00
|
|
|
public void GameOver()
|
2025-02-28 14:01:55 -05:00
|
|
|
{
|
2025-03-08 13:33:19 -05:00
|
|
|
gameOver = true;
|
|
|
|
|
EndGameEvent?.Invoke();
|
2025-03-20 14:45:29 -04:00
|
|
|
if (gameMode == GameMode.freeForAll)
|
|
|
|
|
{
|
|
|
|
|
print(AlivePlayers()[0].name + " is the winner");
|
|
|
|
|
FindFirstObjectByType<PlayerCameraMovement>().WinScene(AlivePlayers()[0]);
|
|
|
|
|
WinScreen.Instance.ShowWinScreen(players.IndexOf(AlivePlayers()[0]) + 1);
|
|
|
|
|
FindFirstObjectByType<LifeDisplayManager>().HideLifeDisplay();
|
|
|
|
|
}
|
|
|
|
|
if (gameMode == GameMode.keepAway)
|
|
|
|
|
{
|
|
|
|
|
GameObject winner = null;
|
|
|
|
|
float maxHoldTime = 0f;
|
|
|
|
|
foreach (var player in GameManager.playerHoldTimes)
|
|
|
|
|
{
|
|
|
|
|
if (player.Value > maxHoldTime)
|
|
|
|
|
{
|
|
|
|
|
maxHoldTime = player.Value;
|
|
|
|
|
winner = player.Key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (winner != null)
|
|
|
|
|
{
|
|
|
|
|
print(winner.name + " is the winner with " + maxHoldTime + " seconds!");
|
|
|
|
|
FindFirstObjectByType<PlayerCameraMovement>().WinScene(winner);
|
|
|
|
|
WinScreen.Instance.ShowWinScreen(players.IndexOf(winner) + 1);
|
|
|
|
|
FindFirstObjectByType<LifeDisplayManager>().HideLifeDisplay();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-07 10:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 13:33:19 -05:00
|
|
|
public List<GameObject> AlivePlayers()
|
2025-03-07 10:03:16 -05:00
|
|
|
{
|
|
|
|
|
List<GameObject> alivePlayers = new();
|
|
|
|
|
|
|
|
|
|
foreach (GameObject player in players)
|
|
|
|
|
{
|
|
|
|
|
if (player.activeInHierarchy) alivePlayers.Add(player);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return alivePlayers;
|
2025-02-28 14:01:55 -05:00
|
|
|
}
|
2025-03-20 14:45:29 -04:00
|
|
|
|
|
|
|
|
public void UpdateLeaderboard()
|
|
|
|
|
{
|
|
|
|
|
List<KeyValuePair<GameObject, float>> sortedList =
|
|
|
|
|
new List<KeyValuePair<GameObject, float>>(playerHoldTimes);
|
|
|
|
|
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-17 18:23:05 -05:00
|
|
|
}
|