Hat remains on winner

This commit is contained in:
djkellerman
2025-04-04 12:09:40 -04:00
parent dff10176c7
commit 2976bed24c
3 changed files with 35 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
@@ -112,9 +113,16 @@ public class GameManager : MonoBehaviour
{
UseItem useItem = player.GetComponent<UseItem>(); // Drop the item the player is holding
if (useItem != null)
{
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<PlayerCameraMovement>().WinScene(winner);
WinScreen.Instance.ShowWinScreen(players.IndexOf(winner) + 1);
FindFirstObjectByType<LifeDisplayManager>().HideLifeDisplay();
StartCoroutine(MoveHatToWinner(winner));
hatObject.SetActive(true);
hatObject.GetComponent<Collider2D>().enabled = true;
hatObject.GetComponent<Rigidbody2D>().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<UseItem>().IsHoldingItem())
{
hatObject.transform.position = winner.transform.position + Vector3.up * 3/2;
yield return null;
}
}
public List<GameObject> AlivePlayers() // Returns a list of all players that are alive
{
List<GameObject> alivePlayers = new();

View File

@@ -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<UseItem>();
if (useItem != null)

View File

@@ -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<Collider2D>().enabled = true;
@@ -70,7 +73,6 @@ public class UseItem : MonoBehaviour
heldItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
heldItem.GetComponent<Rigidbody2D>().AddForce(Vector2.up * Random.Range(10f, 30f) + Vector2.right * Random.Range(-10, 10), ForceMode2D.Impulse);
heldItem.GetComponent<Rigidbody2D>().AddTorque(Random.Range(-5, 5), ForceMode2D.Impulse);
//heldItem.transform.position += Vector3.up * 3f;
heldItem.GetComponent<HatRespawn>().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;
}
}