Update hat movement

This commit is contained in:
RochesterX
2025-03-29 15:18:27 -04:00
parent 9cbb29b99f
commit 2343ef2091
11 changed files with 835 additions and 336 deletions

View File

@@ -20,7 +20,7 @@ public class GameManager : MonoBehaviour
public static GameMode gameMode = GameMode.freeForAll; // loads a default gamemode as a safety net
public static string map = "Platformer With Headroom"; // loads a default map as a safety net
public Vector2 spawnPosition;
public Vector2 hatSpawnPosition;
public List<Vector2> hatSpawnPositions = new List<Vector2>();
public Canvas LeaderboardCanvas;
public Canvas TimerCanvas;
public GameObject hatObject;

View File

@@ -6,10 +6,13 @@ public class HatRespawn : MonoBehaviour
private const float respawnTime = 10f;
private bool isDropped;
public static bool canBePickedUp = true; // Flag to check if the hat can be picked up
void Start()
{
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
@@ -42,8 +45,9 @@ public class HatRespawn : MonoBehaviour
private void RespawnHat() // Respawns the hat at the designated spawn position
{
transform.position = GameManager.Instance.hatSpawnPosition;
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

@@ -1,3 +1,4 @@
using System.Collections;
using UnityEngine;
public class UseItem : MonoBehaviour
@@ -9,6 +10,8 @@ public class UseItem : MonoBehaviour
public float holdTime;
private Damageable damageable;
[SerializeField] private Transform head;
private void Start()
{
damageable = GetComponent<Damageable>();
@@ -19,7 +22,7 @@ public class UseItem : MonoBehaviour
if (isHoldingItem)
{
// Keeps hat on the player's head
heldItem.transform.position = transform.position + Vector3.up;
heldItem.transform.localPosition = Vector3.zero;
if (GameManager.gameMode == GameManager.GameMode.keepAway)
{
// Adds time to the player's leaderboard standing
@@ -40,14 +43,17 @@ public class UseItem : MonoBehaviour
private void PickUpItem(GameObject item) // Player picks up hat and starts hold counter
{
if (damageable.dying) return; // Prevent picking up items if the player is dying
if (HatRespawn.canBePickedUp == false) return; // Prevent picking up items if they are not interactable
heldItem = item;
isHoldingItem = true;
holdStartTime = Time.time;
item.GetComponent<Collider2D>().enabled = false;
item.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
item.transform.rotation = Quaternion.identity;
item.GetComponent<HatRespawn>().Interact();
item.transform.parent = head;
item.transform.localRotation = Quaternion.identity;
item.transform.localPosition = Vector3.zero;
if (!GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes[gameObject] = 0f;
@@ -59,9 +65,14 @@ public class UseItem : MonoBehaviour
if (isHoldingItem)
{
heldItem.GetComponent<Collider2D>().enabled = true;
HatRespawn.canBePickedUp = false;
StartCoroutine(WaitForInteractability());
heldItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
heldItem.transform.position += Vector3.up * 3f;
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;
isHoldingItem = false;
if (GameManager.playerHoldTimes.ContainsKey(gameObject))
@@ -71,4 +82,9 @@ public class UseItem : MonoBehaviour
}
}
private IEnumerator WaitForInteractability()
{
yield return new WaitForSeconds(0.1f);
HatRespawn.canBePickedUp = true;
}
}