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

126 lines
2.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;
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;
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()
{
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;
2025-02-26 18:16:51 -05:00
StartFreeForAll();
}
if (gameMode == GameMode.keepAway)
{
2025-02-28 14:01:55 -05:00
currentLives = 1;
2025-02-26 18:16:51 -05:00
StartKeepAway();
}
if (gameMode == GameMode.obstacleCourse)
{
2025-02-28 14:01:55 -05:00
currentLives = 1;
2025-02-26 18:16:51 -05:00
StartObstacleCourse();
}
}
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;
2025-02-19 20:11:57 -05:00
2025-02-28 14:01:55 -05:00
public static string map = "Platformer With Headroom"; //called for in PlayerManager and should be changed to load from here instead
2025-02-19 20:11:57 -05:00
public static List<GameObject> players = new List<GameObject>();
public Vector2 spawnPosition;
2025-02-26 18:16:51 -05:00
private void StartFreeForAll()
2025-02-19 20:11:57 -05:00
{
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
2025-02-21 17:29:28 -05:00
2025-02-26 18:16:51 -05:00
private void StartKeepAway()
{
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
}
}
2025-02-28 14:01:55 -05:00
2025-02-26 18:16:51 -05:00
private void StartObstacleCourse()
{
foreach (GameObject player in players)
{
player.transform.position = 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)
{
// Disable player controls and show game over screen
player.SetActive(false);
}
2025-02-17 18:23:05 -05:00
}