Hat animation

This commit is contained in:
RochesterX
2025-04-17 21:39:27 -04:00
parent e7bec0c5e3
commit 7f4556853f
13 changed files with 816 additions and 157 deletions

View File

@@ -1,63 +1,78 @@
using UnityEngine; using Game; using Music; using Player;
using UnityEngine;
using Game;
using Music;
using Player;
using System.Collections;
namespace Game
{
public class HatRespawn : MonoBehaviour
{
private float lastInteractionTime;
public const float respawnTime = 10f;
private bool isDropped;
public Vector2 initialSubhatPosition;
public static bool canBePickedUp = true; // Flag to check if the hat can be picked up
void Start()
public class HatRespawn : MonoBehaviour
{
initialSubhatPosition = transform.GetChild(0).transform.localPosition;
lastInteractionTime = Time.time;
isDropped = false;
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
}
private float lastInteractionTime;
public const float respawnTime = 10f;
private bool isDropped;
void Update() // Checks if the hat has been inactive for too long
{
if (GameManager.Instance.gameOver) GetComponent<BoxCollider2D>().enabled = false;
if (isDropped && Time.time - lastInteractionTime > respawnTime)
public Vector2 initialSubhatPosition;
public static bool canBePickedUp = true; // Flag to check if the hat can be picked up
public Vector2 initialScale;
private void Awake()
{
RespawnHat();
initialScale = transform.lossyScale;
}
void Start()
{
//initialSubhatPosition = transform.GetChild(0).transform.localPosition;
lastInteractionTime = Time.time;
isDropped = false;
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
}
void Update() // Checks if the hat has been inactive for too long
{
if (GameManager.Instance.gameOver) GetComponent<BoxCollider2D>().enabled = false;
if (isDropped && Time.time - lastInteractionTime > respawnTime)
{
RespawnHat();
}
}
void OnTriggerEnter2D(Collider2D collision) // Respawns the hat if it falls out of bounds
{
if (collision.gameObject.CompareTag("Platformer Hazard"))
{
StartCoroutine(RespawnHat());
}
}
public void Interact() // Updates the player interaction time
{
lastInteractionTime = Time.time;
isDropped = false;
}
public void OnHatDropped() // Resets the timer when the hat is dropped
{
lastInteractionTime = Time.time;
isDropped = true;
}
private IEnumerator RespawnHat() // Respawns the hat at the designated spawn position
{
GetComponentInChildren<Animator>().SetTrigger("respawn");
yield return new WaitForSeconds(1f / 3f / 2f);
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
GetComponent<Rigidbody2D>().angularVelocity = 0f;
transform.rotation = Quaternion.identity;
lastInteractionTime = Time.time; // Reset the timer after respawning
isDropped = false;
}
}
void OnTriggerEnter2D(Collider2D collision) // Respawns the hat if it falls out of bounds
{
if (collision.gameObject.CompareTag("Platformer Hazard"))
{
RespawnHat();
}
}
public void Interact() // Updates the player interaction time
{
lastInteractionTime = Time.time;
isDropped = false;
}
public void OnHatDropped() // Resets the timer when the hat is dropped
{
lastInteractionTime = Time.time;
isDropped = true;
}
private void RespawnHat() // Respawns the hat at the designated spawn position
{
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
GetComponent<Rigidbody2D>().angularVelocity = 0f;
transform.rotation = Quaternion.identity;
lastInteractionTime = Time.time; // Reset the timer after respawning
isDropped = false;
}
}
}

View File

@@ -59,7 +59,7 @@ namespace Player
item.transform.parent = head;
item.transform.localRotation = Quaternion.identity;
item.transform.localPosition = Vector3.zero;
item.transform.GetChild(0).transform.localPosition = item.GetComponent<HatRespawn>().initialSubhatPosition;
//item.transform.GetChild(0).transform.localPosition = item.GetComponent<HatRespawn>().initialSubhatPosition;
if (!GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes[gameObject] = 0f;
@@ -79,7 +79,7 @@ namespace Player
StartCoroutine(WaitForInteractability());
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.GetComponent<Rigidbody2D>().AddTorque(Random.Range(-1, 1), ForceMode2D.Impulse);
heldItem.GetComponent<HatRespawn>().OnHatDropped();
heldItem.transform.parent = GameManager.Instance.transform;
heldItem = null;