Trick is now pickup-able and is not scored by baskets

This commit is contained in:
hz
2026-01-31 18:55:26 -06:00
parent c24846d568
commit 7d1b387d19
6 changed files with 86 additions and 6 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ GameObject:
- component: {fileID: 700000005} - component: {fileID: 700000005}
m_Layer: 0 m_Layer: 0
m_Name: Trick m_Name: Trick
m_TagString: Untagged m_TagString: Interactable
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
+2 -1
View File
@@ -2621,8 +2621,9 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: Assembly-CSharp::Spawner m_EditorClassIdentifier: Assembly-CSharp::Spawner
objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3} objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3}
spawnInterval: 6 spawnInterval: 3
maxAlive: 1 maxAlive: 1
spawnOnStart: 1
--- !u!1660057539 &9223372036854775807 --- !u!1660057539 &9223372036854775807
SceneRoots: SceneRoots:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
+5
View File
@@ -10,6 +10,11 @@ public class BasketController : MonoBehaviour
if (collision.gameObject.CompareTag("Interactable")) if (collision.gameObject.CompareTag("Interactable"))
{ {
if (collision.gameObject.GetComponent<TrickController>() != null)
{
return;
}
scoreManager.AddPoints(1); scoreManager.AddPoints(1);
GameObject.Destroy(collision.gameObject); GameObject.Destroy(collision.gameObject);
+9
View File
@@ -92,6 +92,11 @@ public class PlayerPickupController : MonoBehaviour
rb.mass = 0; rb.mass = 0;
} }
if (heldObject.TryGetComponent<TrickController>(out var trick))
{
trick.SetCarried(true);
}
if (col != null) col.enabled = false; if (col != null) col.enabled = false;
break; break;
} }
@@ -106,6 +111,10 @@ public class PlayerPickupController : MonoBehaviour
rb.bodyType = RigidbodyType2D.Dynamic; rb.bodyType = RigidbodyType2D.Dynamic;
rb.gravityScale = 1; rb.gravityScale = 1;
} }
if (heldObject.TryGetComponent<TrickController>(out var trick))
{
trick.SetCarried(false);
}
Collider2D col = heldObject.GetComponent<Collider2D>(); Collider2D col = heldObject.GetComponent<Collider2D>();
if (col != null) col.enabled = true; if (col != null) col.enabled = true;
+26 -3
View File
@@ -9,9 +9,18 @@ public class Spawner : MonoBehaviour
// Time interval between spawns // Time interval between spawns
public float spawnInterval = 1f; public float spawnInterval = 1f;
[SerializeField] private int maxAlive = 0; [SerializeField] private int maxAlive = 0;
[SerializeField] private bool spawnOnStart = true;
private readonly List<GameObject> spawned = new List<GameObject>(); private readonly List<GameObject> spawned = new List<GameObject>();
private void OnEnable()
{
if (spawnOnStart)
{
SpawnOnce();
}
}
void Start() void Start()
{ {
// Start the spawning process using a Coroutine // Start the spawning process using a Coroutine
@@ -35,12 +44,26 @@ public class Spawner : MonoBehaviour
continue; continue;
} }
// Spawn the object at the spawner's position and rotation SpawnOnce();
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
spawned.Add(instance);
} }
} }
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() private int CountAlive()
{ {
for (var i = spawned.Count - 1; i >= 0; i--) for (var i = spawned.Count - 1; i >= 0; i--)
+43 -1
View File
@@ -16,6 +16,7 @@ public class TrickController : MonoBehaviour
private float nextTargetTime; private float nextTargetTime;
private Vector2 wanderDirection; private Vector2 wanderDirection;
private float nextWanderTime; private float nextWanderTime;
private bool isCarried;
private void Awake() private void Awake()
{ {
@@ -29,9 +30,24 @@ public class TrickController : MonoBehaviour
private void Update() private void Update()
{ {
if (isCarried)
{
return;
}
if (Time.time >= nextTargetTime) if (Time.time >= nextTargetTime)
{ {
target = FindNearestTreat(); 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; nextTargetTime = Time.time + targetRefreshInterval;
} }
@@ -50,6 +66,11 @@ public class TrickController : MonoBehaviour
private void FixedUpdate() private void FixedUpdate()
{ {
if (isCarried)
{
return;
}
Vector2 desiredVelocity; Vector2 desiredVelocity;
var stopForTreat = false; var stopForTreat = false;
if (target != null) if (target != null)
@@ -82,6 +103,10 @@ public class TrickController : MonoBehaviour
for (var i = 0; i < treats.Length; i++) for (var i = 0; i < treats.Length; i++)
{ {
var candidate = treats[i]; var candidate = treats[i];
if (candidate == gameObject)
{
continue;
}
if (!candidate.activeInHierarchy) if (!candidate.activeInHierarchy)
{ {
continue; continue;
@@ -105,11 +130,16 @@ public class TrickController : MonoBehaviour
private void TryConsumeTreat() private void TryConsumeTreat()
{ {
if (isCarried)
{
return;
}
var hits = Physics2D.OverlapCircleAll(transform.position, consumeDistance); var hits = Physics2D.OverlapCircleAll(transform.position, consumeDistance);
for (var i = 0; i < hits.Length; i++) for (var i = 0; i < hits.Length; i++)
{ {
var hit = hits[i]; var hit = hits[i];
if (hit == null || !hit.CompareTag(treatTag)) if (hit == null || hit.gameObject == gameObject || !hit.CompareTag(treatTag))
{ {
continue; continue;
} }
@@ -122,4 +152,16 @@ public class TrickController : MonoBehaviour
break; break;
} }
} }
public void SetCarried(bool carried)
{
isCarried = carried;
if (carried)
{
target = null;
var velocity = body.linearVelocity;
velocity.x = 0f;
body.linearVelocity = velocity;
}
}
} }