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

113 lines
2.7 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;
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-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()
{
StartGame();
}
2025-02-26 18:16:51 -05:00
public void StartGame()
{
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)
{
player.transform.position = spawnPosition;
}
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-02-28 14:01:55 -05:00
public void PlayerDied(GameObject player)
{
if (gameMode == GameMode.freeForAll)
{
currentLives--;
if (currentLives <= 0)
{
GameOver(player);
}
else
{
RespawnPlayer(player);
}
}
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();
}
}
private void GameOver(GameObject player)
{
// Add game over screen
2025-02-28 14:01:55 -05:00
player.SetActive(false);
EndGameEvent?.Invoke();
2025-02-28 14:01:55 -05:00
}
2025-02-17 18:23:05 -05:00
}