KeepAway updates + GameTimer
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
56
Assets/Scripts/GameTimer.cs
Normal file
56
Assets/Scripts/GameTimer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/GameTimer.cs.meta
Normal file
2
Assets/Scripts/GameTimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 012d92d911650b340a4561288484ac28
|
||||
14
Assets/Scripts/HatRespawn.cs
Normal file
14
Assets/Scripts/HatRespawn.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/HatRespawn.cs.meta
Normal file
2
Assets/Scripts/HatRespawn.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1803a208ca94f44a189bf03342859d36
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
Assets/Scripts/ObjectVisibility.cs
Normal file
26
Assets/Scripts/ObjectVisibility.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/ObjectVisibility.cs.meta
Normal file
2
Assets/Scripts/ObjectVisibility.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34a52816a1c62984ea622bbf50849c68
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
35
Assets/Scripts/WinScreen.cs
Normal file
35
Assets/Scripts/WinScreen.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/WinScreen.cs.meta
Normal file
2
Assets/Scripts/WinScreen.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a15ab2e7ce7b04e19ae259be17276f08
|
||||
Reference in New Issue
Block a user