Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd0c6fea0c | ||
|
|
cb176962a9 | ||
|
|
2d38838efd |
@@ -1,8 +1,27 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class BasketController : MonoBehaviour
|
public class BasketController : MonoBehaviour
|
||||||
{
|
{
|
||||||
public ScoreManager scoreManager;
|
public ScoreManager scoreManager;
|
||||||
|
[SerializeField] private Transform basketHoldPoint;
|
||||||
|
[SerializeField] private LayerMask trickDetectMask = ~0;
|
||||||
|
[SerializeField] private float basketSpawnInterval = 3f;
|
||||||
|
private readonly Collider2D[] overlapHits = new Collider2D[8];
|
||||||
|
private BoxCollider2D basketCollider;
|
||||||
|
private TrickController currentTrick;
|
||||||
|
private float nextBasketSpawnTime;
|
||||||
|
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
basketCollider = GetComponent<BoxCollider2D>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
RefreshTricksInBasket();
|
||||||
|
TryBasketSpawn();
|
||||||
|
}
|
||||||
|
|
||||||
private void OnCollisionEnter2D(Collision2D collision)
|
private void OnCollisionEnter2D(Collision2D collision)
|
||||||
{
|
{
|
||||||
@@ -12,12 +31,193 @@ public class BasketController : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if (collision.gameObject.GetComponent<TrickController>() != null)
|
if (collision.gameObject.GetComponent<TrickController>() != null)
|
||||||
{
|
{
|
||||||
|
RegisterTrick(collision.gameObject);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RefreshTricksInBasket();
|
||||||
|
if (currentTrick != null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
scoreManager.AddPoints(1);
|
scoreManager.AddPoints(1);
|
||||||
|
|
||||||
GameObject.Destroy(collision.gameObject);
|
GameObject.Destroy(collision.gameObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnCollisionExit2D(Collision2D collision)
|
||||||
|
{
|
||||||
|
UnregisterTrick(collision.gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnTriggerEnter2D(Collider2D other)
|
||||||
|
{
|
||||||
|
if (!other.CompareTag("Interactable"))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.GetComponent<TrickController>() != null)
|
||||||
|
{
|
||||||
|
RegisterTrick(other.gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnTriggerExit2D(Collider2D other)
|
||||||
|
{
|
||||||
|
UnregisterTrick(other.gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterTrick(GameObject obj)
|
||||||
|
{
|
||||||
|
if (obj.TryGetComponent<TrickController>(out var trick))
|
||||||
|
{
|
||||||
|
if (currentTrick != null && currentTrick != trick)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTrick = trick;
|
||||||
|
trick.SetBasketed(true, GetBasketAnchorPosition(trick));
|
||||||
|
nextBasketSpawnTime = Time.time + basketSpawnInterval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UnregisterTrick(GameObject obj)
|
||||||
|
{
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.TryGetComponent<TrickController>(out var trick))
|
||||||
|
{
|
||||||
|
if (currentTrick == trick)
|
||||||
|
{
|
||||||
|
currentTrick = null;
|
||||||
|
trick.SetBasketed(false);
|
||||||
|
nextBasketSpawnTime = 0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RefreshTricksInBasket()
|
||||||
|
{
|
||||||
|
if (basketCollider == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var filter = new ContactFilter2D();
|
||||||
|
filter.SetLayerMask(trickDetectMask);
|
||||||
|
filter.useTriggers = true;
|
||||||
|
|
||||||
|
var hitCount = basketCollider.Overlap(filter, overlapHits);
|
||||||
|
for (var i = 0; i < hitCount; i++)
|
||||||
|
{
|
||||||
|
var hit = overlapHits[i];
|
||||||
|
if (hit == null || !hit.CompareTag("Interactable"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hit.TryGetComponent<TrickController>(out var trick))
|
||||||
|
{
|
||||||
|
if (currentTrick == null)
|
||||||
|
{
|
||||||
|
currentTrick = trick;
|
||||||
|
trick.SetBasketed(true, GetBasketAnchorPosition(trick));
|
||||||
|
nextBasketSpawnTime = Time.time + basketSpawnInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentTrick == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentTrick.IsCarried)
|
||||||
|
{
|
||||||
|
currentTrick.SetBasketed(false);
|
||||||
|
currentTrick = null;
|
||||||
|
nextBasketSpawnTime = 0f;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var stillInside = false;
|
||||||
|
for (var i = 0; i < hitCount; i++)
|
||||||
|
{
|
||||||
|
var hit = overlapHits[i];
|
||||||
|
if (hit == null || !hit.CompareTag("Interactable"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hit.TryGetComponent<TrickController>(out var trick) && trick == currentTrick)
|
||||||
|
{
|
||||||
|
stillInside = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stillInside)
|
||||||
|
{
|
||||||
|
currentTrick.SetBasketed(false);
|
||||||
|
currentTrick = null;
|
||||||
|
nextBasketSpawnTime = 0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TryBasketSpawn()
|
||||||
|
{
|
||||||
|
if (currentTrick == null || basketSpawnInterval <= 0f)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Time.time < nextBasketSpawnTime)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TrickController.TrySpawnAt(currentTrick.transform.position);
|
||||||
|
nextBasketSpawnTime = Time.time + basketSpawnInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector2? GetBasketAnchorPosition(TrickController trick)
|
||||||
|
{
|
||||||
|
if (basketHoldPoint != null)
|
||||||
|
{
|
||||||
|
return OffsetForTrickHeight(basketHoldPoint.position, trick);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (basketCollider != null)
|
||||||
|
{
|
||||||
|
return OffsetForTrickHeight(basketCollider.bounds.center, trick);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector2 OffsetForTrickHeight(Vector2 anchor, TrickController trick)
|
||||||
|
{
|
||||||
|
if (trick == null)
|
||||||
|
{
|
||||||
|
return anchor;
|
||||||
|
}
|
||||||
|
|
||||||
|
var renderer = trick.GetComponent<SpriteRenderer>();
|
||||||
|
if (renderer == null)
|
||||||
|
{
|
||||||
|
return anchor;
|
||||||
|
}
|
||||||
|
|
||||||
|
anchor.y += renderer.bounds.size.y * 0.5f;
|
||||||
|
return anchor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
[SerializeField] private float throwUpwardForce = 0f;
|
[SerializeField] private float throwUpwardForce = 0f;
|
||||||
[SerializeField] private float throwTriggerThreshold = 0.6f;
|
[SerializeField] private float throwTriggerThreshold = 0.6f;
|
||||||
[SerializeField] private float dropClearance = 0.2f;
|
[SerializeField] private float dropClearance = 0.2f;
|
||||||
|
[SerializeField] private float holdToSpawnTime = 3f;
|
||||||
|
|
||||||
[Header("Setup")]
|
[Header("Setup")]
|
||||||
public Transform holdPoint; // Empty GameObject above head
|
public Transform holdPoint; // Empty GameObject above head
|
||||||
@@ -33,6 +34,9 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
private PlayerStressController playerStressController;
|
private PlayerStressController playerStressController;
|
||||||
private bool isChargingThrow;
|
private bool isChargingThrow;
|
||||||
private float throwChargeStartTime;
|
private float throwChargeStartTime;
|
||||||
|
private float holdStartTime;
|
||||||
|
private bool spawnedDuringHold;
|
||||||
|
private float nextHoldSpawnTime;
|
||||||
|
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
{
|
{
|
||||||
@@ -113,6 +117,21 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Time.time >= nextHoldSpawnTime)
|
||||||
|
{
|
||||||
|
if (heldObject == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!TrickController.TrySpawnFromPrefabAt(heldObject.transform.position))
|
||||||
|
{
|
||||||
|
TrickController.TrySpawnAt(heldObject.transform.position);
|
||||||
|
}
|
||||||
|
spawnedDuringHold = true;
|
||||||
|
nextHoldSpawnTime = Time.time + holdToSpawnTime;
|
||||||
|
}
|
||||||
|
|
||||||
var gamepad = GetGamepad();
|
var gamepad = GetGamepad();
|
||||||
if (gamepad == null)
|
if (gamepad == null)
|
||||||
{
|
{
|
||||||
@@ -159,12 +178,20 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
heldObject = col.gameObject;
|
heldObject = col.gameObject;
|
||||||
|
holdStartTime = Time.time;
|
||||||
|
spawnedDuringHold = false;
|
||||||
|
nextHoldSpawnTime = holdStartTime + holdToSpawnTime;
|
||||||
|
|
||||||
// Parent the object to the HoldPoint and reset its local position
|
// Parent the object to the HoldPoint and reset its local position
|
||||||
heldObject.transform.SetParent(holdPoint);
|
heldObject.transform.SetParent(holdPoint);
|
||||||
heldObject.transform.localPosition = Vector3.zero;
|
heldObject.transform.localPosition = Vector3.zero;
|
||||||
heldObject.transform.localRotation = Quaternion.identity;
|
heldObject.transform.localRotation = Quaternion.identity;
|
||||||
|
|
||||||
|
if (trick != null)
|
||||||
|
{
|
||||||
|
trick.SetCarried(true);
|
||||||
|
}
|
||||||
|
|
||||||
// Disable physics so it doesn't fall or collide with the player
|
// Disable physics so it doesn't fall or collide with the player
|
||||||
if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
|
if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
|
||||||
{
|
{
|
||||||
@@ -181,11 +208,6 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
rb.angularVelocity = 0f;
|
rb.angularVelocity = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trick != null)
|
|
||||||
{
|
|
||||||
trick.SetCarried(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (playerController != null)
|
if (playerController != null)
|
||||||
{
|
{
|
||||||
playerController.enabled = false;
|
playerController.enabled = false;
|
||||||
@@ -260,6 +282,8 @@ public class PlayerPickupController : MonoBehaviour
|
|||||||
heldObject = null;
|
heldObject = null;
|
||||||
heldBody = null;
|
heldBody = null;
|
||||||
isChargingThrow = false;
|
isChargingThrow = false;
|
||||||
|
spawnedDuringHold = false;
|
||||||
|
nextHoldSpawnTime = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Gamepad GetGamepad()
|
private Gamepad GetGamepad()
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ public class TrickController : MonoBehaviour
|
|||||||
private static readonly List<TrickController> ActiveTricks = new List<TrickController>();
|
private static readonly List<TrickController> ActiveTricks = new List<TrickController>();
|
||||||
private static PlayerPlatformerController[] cachedPlayers = Array.Empty<PlayerPlatformerController>();
|
private static PlayerPlatformerController[] cachedPlayers = Array.Empty<PlayerPlatformerController>();
|
||||||
private static float nextPlayerCacheTime;
|
private static float nextPlayerCacheTime;
|
||||||
|
private static bool hasCanonicalScale;
|
||||||
|
private static Vector3 canonicalScale;
|
||||||
public static int ActiveCount => ActiveTricks.Count;
|
public static int ActiveCount => ActiveTricks.Count;
|
||||||
|
|
||||||
[SerializeField] private string treatTag = "Interactable";
|
[SerializeField] private string treatTag = "Interactable";
|
||||||
@@ -30,6 +32,12 @@ public class TrickController : MonoBehaviour
|
|||||||
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
||||||
[SerializeField] private float jumpArcChance = 0.5f;
|
[SerializeField] private float jumpArcChance = 0.5f;
|
||||||
[SerializeField] private float jumpArcHorizontalImpulse = 2f;
|
[SerializeField] private float jumpArcHorizontalImpulse = 2f;
|
||||||
|
[SerializeField] private float basketSeekRadius = 8f;
|
||||||
|
[SerializeField] private float basketSeekWeight = 0.9f;
|
||||||
|
[SerializeField] private float basketMinHeight = 0.5f;
|
||||||
|
[SerializeField] private float basketRefreshInterval = 0.6f;
|
||||||
|
[SerializeField] private float basketJumpChance = 0.35f;
|
||||||
|
[SerializeField] private float basketJumpHorizontalRange = 0.8f;
|
||||||
[SerializeField] private LayerMask groundMask = ~0;
|
[SerializeField] private LayerMask groundMask = ~0;
|
||||||
[SerializeField] private float groundCheckDistance = 0.05f;
|
[SerializeField] private float groundCheckDistance = 0.05f;
|
||||||
[SerializeField] private float despawnMinX = -12f;
|
[SerializeField] private float despawnMinX = -12f;
|
||||||
@@ -39,13 +47,17 @@ public class TrickController : MonoBehaviour
|
|||||||
[SerializeField] private float throwControlLockTime = 0.35f;
|
[SerializeField] private float throwControlLockTime = 0.35f;
|
||||||
[SerializeField] private float wallCheckDistance = 0.1f;
|
[SerializeField] private float wallCheckDistance = 0.1f;
|
||||||
[SerializeField] private float wallAvoidDuration = 0.6f;
|
[SerializeField] private float wallAvoidDuration = 0.6f;
|
||||||
|
[SerializeField] private float wallAvoidCooldown = 0.2f;
|
||||||
|
|
||||||
private Rigidbody2D body;
|
private Rigidbody2D body;
|
||||||
private Transform target;
|
private Transform target;
|
||||||
private float nextTargetTime;
|
private float nextTargetTime;
|
||||||
|
private Transform basketTarget;
|
||||||
|
private float nextBasketTime;
|
||||||
private Vector2 wanderDirection;
|
private Vector2 wanderDirection;
|
||||||
private float nextWanderTime;
|
private float nextWanderTime;
|
||||||
private bool isCarried;
|
private bool isCarried;
|
||||||
|
private bool isBasketed;
|
||||||
private float nextConsumeTime;
|
private float nextConsumeTime;
|
||||||
private float desiredXVelocity;
|
private float desiredXVelocity;
|
||||||
private float nextJumpTime;
|
private float nextJumpTime;
|
||||||
@@ -54,6 +66,21 @@ public class TrickController : MonoBehaviour
|
|||||||
private readonly RaycastHit2D[] wallHits = new RaycastHit2D[4];
|
private readonly RaycastHit2D[] wallHits = new RaycastHit2D[4];
|
||||||
private float wallAvoidUntil;
|
private float wallAvoidUntil;
|
||||||
private float wallAvoidDirection;
|
private float wallAvoidDirection;
|
||||||
|
private float nextWallAvoidTime;
|
||||||
|
private RigidbodyType2D basketBodyType;
|
||||||
|
private float basketGravityScale;
|
||||||
|
private bool basketSimulated;
|
||||||
|
private bool hasBasketAnchor;
|
||||||
|
private Vector2 basketAnchorPosition;
|
||||||
|
[SerializeField] private string basketSortingLayer = "";
|
||||||
|
[SerializeField] private int basketSortingOrder = -10;
|
||||||
|
private SpriteRenderer cachedRenderer;
|
||||||
|
private string originalSortingLayer;
|
||||||
|
private int originalSortingOrder;
|
||||||
|
private Vector3 originalLocalScale;
|
||||||
|
|
||||||
|
public bool IsCarried => isCarried;
|
||||||
|
public bool IsBasketed => isBasketed;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
@@ -63,6 +90,13 @@ public class TrickController : MonoBehaviour
|
|||||||
body.freezeRotation = true;
|
body.freezeRotation = true;
|
||||||
body.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
body.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
||||||
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
||||||
|
cachedRenderer = GetComponent<SpriteRenderer>();
|
||||||
|
originalLocalScale = transform.localScale;
|
||||||
|
if (!hasCanonicalScale)
|
||||||
|
{
|
||||||
|
canonicalScale = transform.localScale;
|
||||||
|
hasCanonicalScale = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
@@ -88,6 +122,11 @@ public class TrickController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isBasketed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var position = transform.position;
|
var position = transform.position;
|
||||||
if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY || position.y > despawnMaxY)
|
if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY || position.y > despawnMaxY)
|
||||||
{
|
{
|
||||||
@@ -111,6 +150,12 @@ public class TrickController : MonoBehaviour
|
|||||||
nextTargetTime = Time.time + targetRefreshInterval;
|
nextTargetTime = Time.time + targetRefreshInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Time.time >= nextBasketTime)
|
||||||
|
{
|
||||||
|
basketTarget = FindNearestBasketAbove();
|
||||||
|
nextBasketTime = Time.time + basketRefreshInterval;
|
||||||
|
}
|
||||||
|
|
||||||
if (Time.time >= nextWanderTime)
|
if (Time.time >= nextWanderTime)
|
||||||
{
|
{
|
||||||
wanderDirection = Random.insideUnitCircle.normalized;
|
wanderDirection = Random.insideUnitCircle.normalized;
|
||||||
@@ -131,6 +176,19 @@ public class TrickController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isBasketed)
|
||||||
|
{
|
||||||
|
if (hasBasketAnchor)
|
||||||
|
{
|
||||||
|
transform.position = basketAnchorPosition;
|
||||||
|
}
|
||||||
|
var held = body.linearVelocity;
|
||||||
|
held.x = 0f;
|
||||||
|
held.y = 0f;
|
||||||
|
body.linearVelocity = held;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (Time.time < movementLockUntil)
|
if (Time.time < movementLockUntil)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -156,6 +214,15 @@ public class TrickController : MonoBehaviour
|
|||||||
desiredX += wanderDirection.x * wanderWeight;
|
desiredX += wanderDirection.x * wanderWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!hasTarget && basketTarget != null)
|
||||||
|
{
|
||||||
|
var basketDelta = (Vector2)basketTarget.position - (Vector2)transform.position;
|
||||||
|
if (basketDelta.y >= basketMinHeight && basketDelta.magnitude <= basketSeekRadius)
|
||||||
|
{
|
||||||
|
desiredX += Mathf.Sign(basketDelta.x) * basketSeekWeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// No trick-avoidance bias.
|
// No trick-avoidance bias.
|
||||||
|
|
||||||
if (Mathf.Abs(desiredX) < 0.01f)
|
if (Mathf.Abs(desiredX) < 0.01f)
|
||||||
@@ -163,11 +230,15 @@ public class TrickController : MonoBehaviour
|
|||||||
desiredX = wanderDirection.x * 0.2f;
|
desiredX = wanderDirection.x * 0.2f;
|
||||||
}
|
}
|
||||||
|
|
||||||
var wallDirection = GetWallAvoidDirection(desiredX);
|
if (Time.time >= nextWallAvoidTime)
|
||||||
if (wallDirection != 0f)
|
|
||||||
{
|
{
|
||||||
wallAvoidDirection = wallDirection;
|
var wallDirection = GetWallAvoidDirection(desiredX);
|
||||||
wallAvoidUntil = Time.time + wallAvoidDuration;
|
if (wallDirection != 0f)
|
||||||
|
{
|
||||||
|
wallAvoidDirection = wallDirection;
|
||||||
|
wallAvoidUntil = Time.time + wallAvoidDuration;
|
||||||
|
nextWallAvoidTime = Time.time + wallAvoidDuration + wallAvoidCooldown;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Time.time < wallAvoidUntil && Mathf.Abs(wallAvoidDirection) > 0.01f)
|
if (Time.time < wallAvoidUntil && Mathf.Abs(wallAvoidDirection) > 0.01f)
|
||||||
@@ -208,6 +279,14 @@ public class TrickController : MonoBehaviour
|
|||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (candidate.GetComponent<TrickController>() != null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (candidate.GetComponent<PlayerPlatformerController>() != null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!candidate.TryGetComponent<Collider2D>(out var collider) || !collider.enabled)
|
if (!candidate.TryGetComponent<Collider2D>(out var collider) || !collider.enabled)
|
||||||
{
|
{
|
||||||
@@ -225,6 +304,48 @@ public class TrickController : MonoBehaviour
|
|||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Transform FindNearestBasketAbove()
|
||||||
|
{
|
||||||
|
var baskets = FindObjectsByType<BasketController>(FindObjectsSortMode.None);
|
||||||
|
if (baskets == null || baskets.Length == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Transform best = null;
|
||||||
|
var bestDistance = float.MaxValue;
|
||||||
|
var origin = transform.position;
|
||||||
|
for (var i = 0; i < baskets.Length; i++)
|
||||||
|
{
|
||||||
|
var basket = baskets[i];
|
||||||
|
if (basket == null || !basket.gameObject.activeInHierarchy)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var basketPos = basket.transform.position;
|
||||||
|
var delta = (Vector2)basketPos - (Vector2)origin;
|
||||||
|
if (delta.y < basketMinHeight)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var distance = delta.magnitude;
|
||||||
|
if (distance > basketSeekRadius)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (distance < bestDistance)
|
||||||
|
{
|
||||||
|
bestDistance = distance;
|
||||||
|
best = basket.transform;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
private void TryConsumeTreat()
|
private void TryConsumeTreat()
|
||||||
{
|
{
|
||||||
if (isCarried)
|
if (isCarried)
|
||||||
@@ -260,22 +381,130 @@ public class TrickController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SpawnTrick(Vector3 position)
|
private bool SpawnTrick(Vector3 position)
|
||||||
{
|
{
|
||||||
if (maxTricks > 0 && ActiveTricks.Count >= maxTricks)
|
if (maxTricks > 0 && ActiveTricks.Count >= maxTricks)
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trickPrefab != null)
|
if (trickPrefab != null)
|
||||||
{
|
{
|
||||||
Instantiate(trickPrefab, position, Quaternion.identity);
|
var instance = Instantiate(trickPrefab, position, Quaternion.identity);
|
||||||
return;
|
if (instance != null)
|
||||||
|
{
|
||||||
|
instance.ResetRuntimeState(canonicalScale);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Instantiate(gameObject, position, Quaternion.identity);
|
var clone = Instantiate(gameObject, position, Quaternion.identity);
|
||||||
|
if (clone.TryGetComponent<TrickController>(out var spawned))
|
||||||
|
{
|
||||||
|
spawned.ResetRuntimeState(canonicalScale);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ResetRuntimeState(Vector3? forcedScale = null)
|
||||||
|
{
|
||||||
|
isCarried = false;
|
||||||
|
isBasketed = false;
|
||||||
|
hasBasketAnchor = false;
|
||||||
|
movementLockUntil = 0f;
|
||||||
|
wallAvoidUntil = 0f;
|
||||||
|
wallAvoidDirection = 0f;
|
||||||
|
if (forcedScale.HasValue)
|
||||||
|
{
|
||||||
|
transform.localScale = forcedScale.Value;
|
||||||
|
}
|
||||||
|
if (cachedRenderer != null)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(basketSortingLayer))
|
||||||
|
{
|
||||||
|
cachedRenderer.sortingLayerName = originalSortingLayer;
|
||||||
|
}
|
||||||
|
cachedRenderer.sortingOrder = originalSortingOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.simulated = true;
|
||||||
|
body.bodyType = RigidbodyType2D.Dynamic;
|
||||||
|
body.gravityScale = 1f;
|
||||||
|
body.linearVelocity = Vector2.zero;
|
||||||
|
body.angularVelocity = 0f;
|
||||||
|
if (TryGetComponent<Collider2D>(out var collider))
|
||||||
|
{
|
||||||
|
collider.enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TrySpawnAt(Vector3 position)
|
||||||
|
{
|
||||||
|
if (ActiveTricks.Count == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TrickController candidate = null;
|
||||||
|
TrickController fallback = null;
|
||||||
|
for (var i = 0; i < ActiveTricks.Count; i++)
|
||||||
|
{
|
||||||
|
var trick = ActiveTricks[i];
|
||||||
|
if (trick == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fallback == null)
|
||||||
|
{
|
||||||
|
fallback = trick;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!trick.isCarried && !trick.isBasketed)
|
||||||
|
{
|
||||||
|
if (trick.TryGetComponent<Collider2D>(out var collider) && collider.enabled)
|
||||||
|
{
|
||||||
|
candidate = trick;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (candidate == null)
|
||||||
|
{
|
||||||
|
candidate = fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
return candidate != null && candidate.SpawnTrick(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TrySpawnFromPrefabAt(Vector3 position)
|
||||||
|
{
|
||||||
|
if (ActiveTricks.Count == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < ActiveTricks.Count; i++)
|
||||||
|
{
|
||||||
|
var trick = ActiveTricks[i];
|
||||||
|
if (trick == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trick.trickPrefab == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return trick.SpawnTrick(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private float GetAvoidPlayerDirection()
|
private float GetAvoidPlayerDirection()
|
||||||
{
|
{
|
||||||
if (Time.time >= nextPlayerCacheTime)
|
if (Time.time >= nextPlayerCacheTime)
|
||||||
@@ -320,7 +549,7 @@ public class TrickController : MonoBehaviour
|
|||||||
|
|
||||||
private void TryJump(bool isMoving, float jumpDirection)
|
private void TryJump(bool isMoving, float jumpDirection)
|
||||||
{
|
{
|
||||||
if (!isMoving || Time.time < nextJumpTime)
|
if (Time.time < nextJumpTime)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -330,20 +559,43 @@ public class TrickController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (body.linearVelocity.y > 0.01f)
|
var wantsBasketJump = false;
|
||||||
|
var basketDir = 0f;
|
||||||
|
if (basketTarget != null)
|
||||||
{
|
{
|
||||||
return;
|
var delta = (Vector2)basketTarget.position - (Vector2)transform.position;
|
||||||
|
if (delta.y >= basketMinHeight && Mathf.Abs(delta.x) <= basketJumpHorizontalRange)
|
||||||
|
{
|
||||||
|
if (Random.value < basketJumpChance)
|
||||||
|
{
|
||||||
|
wantsBasketJump = true;
|
||||||
|
basketDir = Mathf.Sign(delta.x);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var chance = jumpChancePerSecond * Time.fixedDeltaTime;
|
if (!wantsBasketJump)
|
||||||
if (Random.value > chance)
|
{
|
||||||
|
if (!isMoving)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var chance = jumpChancePerSecond * Time.fixedDeltaTime;
|
||||||
|
if (Random.value > chance)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.linearVelocity.y > 0.01f)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var velocity = body.linearVelocity;
|
var velocity = body.linearVelocity;
|
||||||
velocity.y = 0f;
|
velocity.y = 0f;
|
||||||
var jumpDir = Mathf.Sign(jumpDirection);
|
var jumpDir = wantsBasketJump ? basketDir : Mathf.Sign(jumpDirection);
|
||||||
if (jumpDir == 0f)
|
if (jumpDir == 0f)
|
||||||
{
|
{
|
||||||
jumpDir = Random.value < 0.5f ? -1f : 1f;
|
jumpDir = Random.value < 0.5f ? -1f : 1f;
|
||||||
@@ -448,6 +700,10 @@ public class TrickController : MonoBehaviour
|
|||||||
public void SetCarried(bool carried)
|
public void SetCarried(bool carried)
|
||||||
{
|
{
|
||||||
isCarried = carried;
|
isCarried = carried;
|
||||||
|
if (carried && isBasketed)
|
||||||
|
{
|
||||||
|
SetBasketed(false);
|
||||||
|
}
|
||||||
if (carried)
|
if (carried)
|
||||||
{
|
{
|
||||||
target = null;
|
target = null;
|
||||||
@@ -475,6 +731,62 @@ public class TrickController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetBasketed(bool basketed, Vector2? anchorPosition = null)
|
||||||
|
{
|
||||||
|
if (isBasketed == basketed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isBasketed = basketed;
|
||||||
|
if (basketed)
|
||||||
|
{
|
||||||
|
if (cachedRenderer != null)
|
||||||
|
{
|
||||||
|
originalSortingLayer = cachedRenderer.sortingLayerName;
|
||||||
|
originalSortingOrder = cachedRenderer.sortingOrder;
|
||||||
|
if (!string.IsNullOrWhiteSpace(basketSortingLayer))
|
||||||
|
{
|
||||||
|
cachedRenderer.sortingLayerName = basketSortingLayer;
|
||||||
|
}
|
||||||
|
cachedRenderer.sortingOrder = basketSortingOrder;
|
||||||
|
}
|
||||||
|
if (anchorPosition.HasValue)
|
||||||
|
{
|
||||||
|
hasBasketAnchor = true;
|
||||||
|
basketAnchorPosition = anchorPosition.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hasBasketAnchor = false;
|
||||||
|
}
|
||||||
|
basketBodyType = body.bodyType;
|
||||||
|
basketGravityScale = body.gravityScale;
|
||||||
|
basketSimulated = body.simulated;
|
||||||
|
body.simulated = true;
|
||||||
|
body.bodyType = RigidbodyType2D.Kinematic;
|
||||||
|
body.gravityScale = 0f;
|
||||||
|
body.linearVelocity = Vector2.zero;
|
||||||
|
body.angularVelocity = 0f;
|
||||||
|
nextJumpTime = Time.time + 0.1f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (cachedRenderer != null)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(basketSortingLayer))
|
||||||
|
{
|
||||||
|
cachedRenderer.sortingLayerName = originalSortingLayer;
|
||||||
|
}
|
||||||
|
cachedRenderer.sortingOrder = originalSortingOrder;
|
||||||
|
}
|
||||||
|
hasBasketAnchor = false;
|
||||||
|
body.simulated = basketSimulated;
|
||||||
|
body.bodyType = basketBodyType;
|
||||||
|
body.gravityScale = basketGravityScale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void OnThrown()
|
public void OnThrown()
|
||||||
{
|
{
|
||||||
movementLockUntil = Time.time + throwControlLockTime;
|
movementLockUntil = Time.time + throwControlLockTime;
|
||||||
|
|||||||
Reference in New Issue
Block a user