From 2976bed24cab251b85770765959c544299d7e360 Mon Sep 17 00:00:00 2001 From: djkellerman Date: Fri, 4 Apr 2025 12:09:40 -0400 Subject: [PATCH] Hat remains on winner --- Assets/Scripts/Game/GameManager.cs | 26 +++++++++++++++++++++++--- Assets/Scripts/Player/Damageable.cs | 4 +++- Assets/Scripts/Player/UseItem.cs | 11 +++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 10a0b69..cad3c96 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -1,3 +1,4 @@ +using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; @@ -113,7 +114,14 @@ public class GameManager : MonoBehaviour UseItem useItem = player.GetComponent(); // Drop the item the player is holding if (useItem != null) { - useItem.DropItem(); + if (gameOver == false) + { + useItem.DropItem(); + } + else + { + return; // Prevents winner from dropping the item if the game is over + } } if (gameMode == GameMode.freeForAll) // Respawns player if they have lives left @@ -159,7 +167,6 @@ public class GameManager : MonoBehaviour EndGameEvent?.Invoke(); LeaderboardCanvas.gameObject.SetActive(false); TimerCanvas.gameObject.SetActive(false); - hatObject.SetActive(false); if (gameMode == GameMode.freeForAll) // Last player alive wins { @@ -187,9 +194,13 @@ public class GameManager : MonoBehaviour FindFirstObjectByType().WinScene(winner); WinScreen.Instance.ShowWinScreen(players.IndexOf(winner) + 1); FindFirstObjectByType().HideLifeDisplay(); + StartCoroutine(MoveHatToWinner(winner)); + hatObject.SetActive(true); + hatObject.GetComponent().enabled = true; + hatObject.GetComponent().bodyType = RigidbodyType2D.Dynamic; } } - if (gameMode == GameMode.obstacleCourse) + if (gameMode == GameMode.obstacleCourse) // Player who reached the end first wins { GameObject winner = ObstacleCourse.playerWon; @@ -200,6 +211,15 @@ public class GameManager : MonoBehaviour } } + private IEnumerator MoveHatToWinner(GameObject winner) + { + while (!winner.GetComponent().IsHoldingItem()) + { + hatObject.transform.position = winner.transform.position + Vector3.up * 3/2; + yield return null; + } + } + public List AlivePlayers() // Returns a list of all players that are alive { List alivePlayers = new(); diff --git a/Assets/Scripts/Player/Damageable.cs b/Assets/Scripts/Player/Damageable.cs index fc69fb0..9ef9d02 100644 --- a/Assets/Scripts/Player/Damageable.cs +++ b/Assets/Scripts/Player/Damageable.cs @@ -74,6 +74,8 @@ public class Damageable : MonoBehaviour public void Damage(float damage) // Adds damage to player when hit { + if (GameManager.Instance.gameOver) return; // Prevent damage after game is over + this.damage += damage; if (damage >= maxDamage) { @@ -94,7 +96,7 @@ public class Damageable : MonoBehaviour private void Die() // Triggers death animation and sets player to dying state { - if (GameManager.Instance != null) + if (GameManager.Instance != null && !GameManager.Instance.gameOver) // Prevent death after game is over { UseItem useItem = GetComponent(); if (useItem != null) diff --git a/Assets/Scripts/Player/UseItem.cs b/Assets/Scripts/Player/UseItem.cs index bd1ef73..5a6c217 100644 --- a/Assets/Scripts/Player/UseItem.cs +++ b/Assets/Scripts/Player/UseItem.cs @@ -10,7 +10,7 @@ public class UseItem : MonoBehaviour public float holdTime; private Damageable damageable; - [SerializeField] private Transform head; + [SerializeField] public Transform head; private void Start() { @@ -58,10 +58,13 @@ public class UseItem : MonoBehaviour { GameManager.playerHoldTimes[gameObject] = 0f; } + GameManager.Instance.StopCoroutine("MoveHatToWinner"); } public void DropItem() // Player drops hat when hit { + if (GameManager.Instance.gameOver) return; // Prevent dropping items if the game is over + if (isHoldingItem) { heldItem.GetComponent().enabled = true; @@ -70,7 +73,6 @@ public class UseItem : MonoBehaviour heldItem.GetComponent().bodyType = RigidbodyType2D.Dynamic; heldItem.GetComponent().AddForce(Vector2.up * Random.Range(10f, 30f) + Vector2.right * Random.Range(-10, 10), ForceMode2D.Impulse); heldItem.GetComponent().AddTorque(Random.Range(-5, 5), ForceMode2D.Impulse); - //heldItem.transform.position += Vector3.up * 3f; heldItem.GetComponent().OnHatDropped(); heldItem.transform.parent = GameManager.Instance.transform; heldItem = null; @@ -87,4 +89,9 @@ public class UseItem : MonoBehaviour yield return new WaitForSeconds(0.1f); HatRespawn.canBePickedUp = true; } + + public bool IsHoldingItem() + { + return isHoldingItem; + } }