From 7d1b387d197cb3b8872f6f07ad7b3ce2228f0763 Mon Sep 17 00:00:00 2001 From: hzmt Date: Sat, 31 Jan 2026 18:55:26 -0600 Subject: [PATCH] Trick is now pickup-able and is not scored by baskets --- Assets/Prefabs/Trick.prefab | 2 +- Assets/Scenes/Experiment.unity | 3 +- Assets/Scripts/BasketController.cs | 5 +++ Assets/Scripts/PlayerPickupController.cs | 9 +++++ Assets/Scripts/Spawner.cs | 29 ++++++++++++++-- Assets/Scripts/TrickController.cs | 44 +++++++++++++++++++++++- 6 files changed, 86 insertions(+), 6 deletions(-) diff --git a/Assets/Prefabs/Trick.prefab b/Assets/Prefabs/Trick.prefab index a58d44f..c9050d7 100644 --- a/Assets/Prefabs/Trick.prefab +++ b/Assets/Prefabs/Trick.prefab @@ -15,7 +15,7 @@ GameObject: - component: {fileID: 700000005} m_Layer: 0 m_Name: Trick - m_TagString: Untagged + m_TagString: Interactable m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 diff --git a/Assets/Scenes/Experiment.unity b/Assets/Scenes/Experiment.unity index a99ce47..779c1c6 100644 --- a/Assets/Scenes/Experiment.unity +++ b/Assets/Scenes/Experiment.unity @@ -2621,8 +2621,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Assembly-CSharp::Spawner objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3} - spawnInterval: 6 + spawnInterval: 3 maxAlive: 1 + spawnOnStart: 1 --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/BasketController.cs b/Assets/Scripts/BasketController.cs index 4d67462..feb727c 100644 --- a/Assets/Scripts/BasketController.cs +++ b/Assets/Scripts/BasketController.cs @@ -10,6 +10,11 @@ public class BasketController : MonoBehaviour if (collision.gameObject.CompareTag("Interactable")) { + if (collision.gameObject.GetComponent() != null) + { + return; + } + scoreManager.AddPoints(1); GameObject.Destroy(collision.gameObject); diff --git a/Assets/Scripts/PlayerPickupController.cs b/Assets/Scripts/PlayerPickupController.cs index b37da49..87d4546 100644 --- a/Assets/Scripts/PlayerPickupController.cs +++ b/Assets/Scripts/PlayerPickupController.cs @@ -92,6 +92,11 @@ public class PlayerPickupController : MonoBehaviour rb.mass = 0; } + if (heldObject.TryGetComponent(out var trick)) + { + trick.SetCarried(true); + } + if (col != null) col.enabled = false; break; } @@ -106,6 +111,10 @@ public class PlayerPickupController : MonoBehaviour rb.bodyType = RigidbodyType2D.Dynamic; rb.gravityScale = 1; } + if (heldObject.TryGetComponent(out var trick)) + { + trick.SetCarried(false); + } Collider2D col = heldObject.GetComponent(); if (col != null) col.enabled = true; diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs index 5a501ae..2559848 100644 --- a/Assets/Scripts/Spawner.cs +++ b/Assets/Scripts/Spawner.cs @@ -9,9 +9,18 @@ public class Spawner : MonoBehaviour // Time interval between spawns public float spawnInterval = 1f; [SerializeField] private int maxAlive = 0; + [SerializeField] private bool spawnOnStart = true; private readonly List spawned = new List(); + private void OnEnable() + { + if (spawnOnStart) + { + SpawnOnce(); + } + } + void Start() { // Start the spawning process using a Coroutine @@ -35,12 +44,26 @@ public class Spawner : MonoBehaviour continue; } - // Spawn the object at the spawner's position and rotation - var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity); - spawned.Add(instance); + SpawnOnce(); } } + private void SpawnOnce() + { + if (objectToSpawn == null) + { + return; + } + + if (maxAlive > 0 && CountAlive() >= maxAlive) + { + return; + } + + var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity); + spawned.Add(instance); + } + private int CountAlive() { for (var i = spawned.Count - 1; i >= 0; i--) diff --git a/Assets/Scripts/TrickController.cs b/Assets/Scripts/TrickController.cs index a25d4b0..55e56f3 100644 --- a/Assets/Scripts/TrickController.cs +++ b/Assets/Scripts/TrickController.cs @@ -16,6 +16,7 @@ public class TrickController : MonoBehaviour private float nextTargetTime; private Vector2 wanderDirection; private float nextWanderTime; + private bool isCarried; private void Awake() { @@ -29,9 +30,24 @@ public class TrickController : MonoBehaviour private void Update() { + if (isCarried) + { + return; + } + if (Time.time >= nextTargetTime) { target = FindNearestTreat(); + if (target == null) + { + // If we can't find treats while we're tagged Interactable, keep moving. + wanderDirection = Random.insideUnitCircle.normalized; + if (wanderDirection.sqrMagnitude < 0.01f) + { + wanderDirection = Vector2.right; + } + nextWanderTime = Time.time + wanderInterval; + } nextTargetTime = Time.time + targetRefreshInterval; } @@ -50,6 +66,11 @@ public class TrickController : MonoBehaviour private void FixedUpdate() { + if (isCarried) + { + return; + } + Vector2 desiredVelocity; var stopForTreat = false; if (target != null) @@ -82,6 +103,10 @@ public class TrickController : MonoBehaviour for (var i = 0; i < treats.Length; i++) { var candidate = treats[i]; + if (candidate == gameObject) + { + continue; + } if (!candidate.activeInHierarchy) { continue; @@ -105,11 +130,16 @@ public class TrickController : MonoBehaviour private void TryConsumeTreat() { + if (isCarried) + { + return; + } + var hits = Physics2D.OverlapCircleAll(transform.position, consumeDistance); for (var i = 0; i < hits.Length; i++) { var hit = hits[i]; - if (hit == null || !hit.CompareTag(treatTag)) + if (hit == null || hit.gameObject == gameObject || !hit.CompareTag(treatTag)) { continue; } @@ -122,4 +152,16 @@ public class TrickController : MonoBehaviour break; } } + + public void SetCarried(bool carried) + { + isCarried = carried; + if (carried) + { + target = null; + var velocity = body.linearVelocity; + velocity.x = 0f; + body.linearVelocity = velocity; + } + } }