KeepAway updates + GameTimer

This commit is contained in:
djkellerman
2025-03-20 14:45:29 -04:00
parent a3e2c9c6e2
commit 0a13e9ccd3
23 changed files with 462 additions and 77 deletions

View File

@@ -7,7 +7,6 @@ public class GameManager : MonoBehaviour
{
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;
@@ -18,6 +17,8 @@ public class GameManager : MonoBehaviour
public static bool music = true;
public bool gameOver = false;
public GameTimer gameTimer;
public static Dictionary<GameObject, float> playerHoldTimes = new Dictionary<GameObject, float>();
private void Awake()
{
@@ -39,6 +40,7 @@ public class GameManager : MonoBehaviour
public void StartGame()
{
GameManager.playerHoldTimes.Clear();
if (GameManager.players.Count == 0) return;
StartGameEvent?.Invoke();
@@ -53,7 +55,7 @@ public class GameManager : MonoBehaviour
}
if (gameMode == GameMode.keepAway)
{
currentLives = 1;
gameTimer.StartTimer();
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
@@ -62,7 +64,6 @@ public class GameManager : MonoBehaviour
}
if (gameMode == GameMode.obstacleCourse)
{
currentLives = 1;
foreach (GameObject player in players)
{
player.transform.position = spawnPosition;
@@ -121,14 +122,37 @@ public class GameManager : MonoBehaviour
}
}
private void GameOver()
public void GameOver()
{
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();
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();
}
}
}
public List<GameObject> AlivePlayers()
@@ -142,4 +166,12 @@ public class GameManager : MonoBehaviour
return alivePlayers;
}
public void UpdateLeaderboard()
{
List<KeyValuePair<GameObject, float>> sortedList =
new List<KeyValuePair<GameObject, float>>(playerHoldTimes);
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
}
}

View File

@@ -0,0 +1,56 @@
using UnityEngine;
using UnityEngine.UI;
public class GameTimer : MonoBehaviour
{
public float startTime = 180f;
private float timeRemaining;
private bool timerRunning = false;
public Text timerText;
private void Start()
{
timeRemaining = startTime;
UpdateTimerDisplay();
}
private void Update()
{
if (timerRunning)
{
timeRemaining -= Time.deltaTime;
if (timeRemaining <= 0)
{
timeRemaining = 0;
timerRunning = false;
OnTimerEnd();
}
UpdateTimerDisplay();
}
}
public void StartTimer()
{
if (!timerRunning)
{
timeRemaining = startTime;
timerRunning = true;
}
}
private void UpdateTimerDisplay()
{
int minutes = Mathf.FloorToInt(timeRemaining / 60);
int seconds = Mathf.FloorToInt(timeRemaining % 60);
timerText.text = string.Format("{0:D2}:{1:D2}", minutes, seconds);
}
private void OnTimerEnd()
{
Debug.Log("Timer ended! KeepAway mode has finished.");
GameManager.Instance.GameOver();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 012d92d911650b340a4561288484ac28

View File

@@ -0,0 +1,14 @@
using UnityEngine;
public class HatRespawn : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Platformer Hazard"))
{
transform.position = GameManager.Instance.hatSpawnPosition;
GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
transform.rotation = Quaternion.identity;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1803a208ca94f44a189bf03342859d36

View File

@@ -12,19 +12,20 @@ public class LifeDisplayManager : MonoBehaviour
private void Start()
{
foreach (GameObject player in GameManager.players)
if (GameManager.gameMode == GameManager.GameMode.freeForAll)
{
Transform parent = Instantiate(playerPrefab, players.transform).transform;
List<GameObject> lives = new List<GameObject>();
for (int i = 0; i < player.GetComponent<Damageable>().lives; i++)
foreach (GameObject player in GameManager.players)
{
GameObject life = Instantiate(lifePrefab, parent);
life.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
lives.Add(life);
Transform parent = Instantiate(playerPrefab, players.transform).transform;
List<GameObject> lives = new List<GameObject>();
for (int i = 0; i < player.GetComponent<Damageable>().lives; i++)
{
GameObject life = Instantiate(lifePrefab, parent);
life.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
lives.Add(life);
}
lifeDisplays.Add(player.GetComponent<Damageable>(), lives);
}
lifeDisplays.Add(player.GetComponent<Damageable>(), lives);
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine;
public class ObjectVisibility : MonoBehaviour
{
void Start()
{
UpdateVisibility();
}
void Update()
{
UpdateVisibility();
}
private void UpdateVisibility()
{
if (GameManager.gameMode == GameManager.GameMode.keepAway)
{
gameObject.SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 34a52816a1c62984ea622bbf50849c68

View File

@@ -5,12 +5,24 @@ public class UseItem : MonoBehaviour
[SerializeField] private string itemTag;
private GameObject heldItem;
private bool isHoldingItem = false;
private float holdStartTime;
void Update()
{
if (isHoldingItem)
{
heldItem.transform.position = transform.position + Vector3.up;
if (GameManager.gameMode == GameManager.GameMode.keepAway)
{
if (GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes[gameObject] += Time.deltaTime;
}
else
{
GameManager.playerHoldTimes[gameObject] = Time.time - holdStartTime;
}
}
}
}
@@ -26,9 +38,14 @@ public class UseItem : MonoBehaviour
{
heldItem = item;
isHoldingItem = true;
holdStartTime = Time.time;
item.GetComponent<Collider2D>().enabled = false;
item.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
item.transform.rotation = Quaternion.identity;
if (!GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes[gameObject] = 0f;
}
}
public void DropItem()
@@ -40,6 +57,10 @@ public class UseItem : MonoBehaviour
heldItem.transform.position += Vector3.up * 3f;
heldItem = null;
isHoldingItem = false;
if (GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes.Remove(gameObject);
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class WinScreen : MonoBehaviour
{
public static WinScreen Instance;
public List<TextMeshProUGUI> playerTexts;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this.gameObject);
}
}
public void ShowWinScreen(int player)
{
foreach (TextMeshProUGUI playerText in playerTexts)
{
playerText.text = "Player " + player;
if (playerText.color != Color.black)
{
playerText.color = GameManager.playerColors[player - 1];
}
}
GetComponent<Animator>().SetTrigger("win");
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a15ab2e7ce7b04e19ae259be17276f08