timer and leaderboard logic complete

This commit is contained in:
djkellerman
2025-03-25 22:21:21 -04:00
parent 867376c7d7
commit ad50ee6d7c
6 changed files with 1124 additions and 24 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -168,11 +168,16 @@ public class GameManager : MonoBehaviour
return alivePlayers; return alivePlayers;
} }
public void UpdateLeaderboard() public void UpdatePlayerHoldTime(GameObject player, float holdTime)
{ {
List<KeyValuePair<GameObject, float>> sortedList = if (playerHoldTimes.ContainsKey(player))
new List<KeyValuePair<GameObject, float>>(playerHoldTimes); {
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); playerHoldTimes[player] = holdTime;
}
else
{
playerHoldTimes.Add(player, holdTime);
}
LeaderboardManager.Instance.UpdateLeaderboard();
} }
} }

View File

@@ -14,7 +14,7 @@ public class GameTimer : MonoBehaviour
private void Start() private void Start()
{ {
timeRemaining = startTime; timeRemaining = startTime;
timer.text = "0:00.00"; timer.text = "3:00.00";
UpdateTimerDisplay(); UpdateTimerDisplay();
} }
@@ -48,12 +48,11 @@ public class GameTimer : MonoBehaviour
{ {
int minutes = Mathf.FloorToInt(timeRemaining / 60); int minutes = Mathf.FloorToInt(timeRemaining / 60);
int seconds = Mathf.FloorToInt(timeRemaining % 60); int seconds = Mathf.FloorToInt(timeRemaining % 60);
timerText.text = string.Format("{0:D2}:{1:D2}", minutes, seconds); timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
} }
private void OnTimerEnd() private void OnTimerEnd()
{ {
Debug.Log("Timer ended! KeepAway mode has finished.");
GameManager.Instance.GameOver(); GameManager.Instance.GameOver();
} }
} }

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LeaderboardManager : MonoBehaviour
{
public static LeaderboardManager Instance { get; private set; }
[SerializeField] private GameObject playersParent; // The parent GameObject that holds the player icons
[SerializeField] private GameObject playerPrefab; // The prefab for the player icon
private Dictionary<GameObject, GameObject> playerIcons = new Dictionary<GameObject, GameObject>();
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
InitializeLeaderboard();
}
private void InitializeLeaderboard()
{
foreach (GameObject player in GameManager.players)
{
GameObject playerIcon = Instantiate(playerPrefab, playersParent.transform);
playerIcon.GetComponentInChildren<Text>().text = player.name;
playerIcon.GetComponentInChildren<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
playerIcons[player] = playerIcon;
}
}
public void UpdateLeaderboard()
{
List<KeyValuePair<GameObject, float>> sortedList =
new List<KeyValuePair<GameObject, float>>(GameManager.playerHoldTimes);
sortedList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
foreach (var player in sortedList)
{
playerIcons[player.Key].transform.SetSiblingIndex(sortedList.IndexOf(player));
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 94998096868b2f543ae7fcd946cc4f14

View File

@@ -14,14 +14,8 @@ public class UseItem : MonoBehaviour
heldItem.transform.position = transform.position + Vector3.up; heldItem.transform.position = transform.position + Vector3.up;
if (GameManager.gameMode == GameManager.GameMode.keepAway) if (GameManager.gameMode == GameManager.GameMode.keepAway)
{ {
if (GameManager.playerHoldTimes.ContainsKey(gameObject)) float holdTime = Time.time - holdStartTime;
{ GameManager.Instance.UpdatePlayerHoldTime(gameObject, holdTime);
GameManager.playerHoldTimes[gameObject] += Time.deltaTime;
}
else
{
GameManager.playerHoldTimes[gameObject] = Time.time - holdStartTime;
}
} }
} }
} }
@@ -73,12 +67,12 @@ public class UseItem : MonoBehaviour
{ {
//Punch.OnPlayerPunched -= HandlePlayerPunched; //Punch.OnPlayerPunched -= HandlePlayerPunched;
} }
/* /*
private void HandlePlayerPunched(GameObject punchedPlayer) private void HandlePlayerPunched(GameObject punchedPlayer)
{
if (punchedPlayer == gameObject)
{ {
DropItem(); if (punchedPlayer == gameObject)
} {
}*/ DropItem();
}
}*/
} }