Files
Crash-Course/Assets/Scripts/GameManager.cs

144 lines
3.8 KiB
C#
Raw Normal View History

2025-02-19 20:11:57 -05:00
using System.Collections.Generic;
2025-02-17 18:23:05 -05:00
using UnityEngine;
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;
public int currentLives;
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;
public static bool music = true;
2025-03-07 16:50:04 -05:00
public bool gameOver = false;
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()
{
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-07 16:50:04 -05:00
if (GameManager.players.Count == 0) return;
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-02-28 14:01:55 -05:00
currentLives = maxLives;
foreach (GameObject player in players)
{
2025-03-07 16:50:04 -05:00
player.transform.position = spawnPosition + (players.IndexOf(player) * Vector2.right * offset);
}
2025-02-26 18:16:51 -05:00
}
if (gameMode == GameMode.keepAway)
{
2025-02-28 14:01:55 -05:00
currentLives = 1;
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
2025-02-26 18:16:51 -05:00
}
if (gameMode == GameMode.obstacleCourse)
{
2025-02-28 14:01:55 -05:00
currentLives = 1;
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
}
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-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)
{
}
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-08 13:33:19 -05:00
private void GameOver()
2025-02-28 14:01:55 -05:00
{
2025-03-08 13:33:19 -05:00
gameOver = true;
EndGameEvent?.Invoke();
print(AlivePlayers()[0].name + " is the winner");
FindFirstObjectByType<PlayerCameraMovement>().WinScene(AlivePlayers()[0]);
WinScreen.Instance.ShowWinScreen(players.IndexOf(AlivePlayers()[0]) + 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-02-17 18:23:05 -05:00
}