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

75 lines
1.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
{
public static GameManager Instance { get; private set; }
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;
public static string map = "Platformer With Headroom";
2025-02-19 20:11:57 -05:00
public static List<GameObject> players = new List<GameObject>();
public Vector2 spawnPosition;
private Dictionary<GameObject, int> playerLives = new Dictionary<GameObject, int>();
public int maxLives = 3;
private void Start()
{
StartGame();
}
public void StartGame()
2025-02-19 20:11:57 -05:00
{
foreach (GameObject player in players)
{
if (gameMode == GameMode.freeForAll)
{
playerLives[player] = maxLives;
}
else
{
playerLives[player] = 1;
}
2025-02-19 20:11:57 -05:00
player.transform.position = spawnPosition;
}
}
2025-02-21 17:29:28 -05:00
public void PlayerDied(GameObject player)
2025-02-26 18:16:51 -05:00
{
if (gameMode == GameMode.freeForAll)
2025-02-26 18:16:51 -05:00
{
playerLives[player]--;
if (playerLives[player] <= 0)
{
GameOver(player);
}
else
{
RespawnPlayer(player);
}
2025-02-26 18:16:51 -05:00
}
}
private void RespawnPlayer(GameObject player)
2025-02-26 18:16:51 -05:00
{
RespawnOnTriggerEnter respawnScript = player.GetComponent<RespawnOnTriggerEnter>();
if (respawnScript != null)
2025-02-26 18:16:51 -05:00
{
player.transform.position = respawnScript.spawnPoint;
player.GetComponent<Damageable>().ResetDamage();
2025-02-26 18:16:51 -05:00
}
}
private void GameOver(GameObject player)
{
// Disable player controls and show game over screen
player.SetActive(false);
}
2025-02-17 18:23:05 -05:00
}