Removing barriers, adding trick logic, and making players pick-up-able
This commit is contained in:
@@ -12,6 +12,17 @@ public class TrickController : MonoBehaviour
|
||||
[SerializeField] private float wanderInterval = 1.2f;
|
||||
[SerializeField] private float consumeCooldown = 1.5f;
|
||||
[SerializeField] private TrickController trickPrefab;
|
||||
[SerializeField] private float avoidPlayerRadius = 1.2f;
|
||||
[SerializeField] private float avoidTrickRadius = 0.8f;
|
||||
[SerializeField] private float treatSeekWeight = 1f;
|
||||
[SerializeField] private float avoidPlayerWeight = 0.25f;
|
||||
[SerializeField] private float avoidTrickWeight = 0.2f;
|
||||
[SerializeField] private float wanderWeight = 0.4f;
|
||||
[SerializeField] private float jumpImpulse = 6f;
|
||||
[SerializeField] private float jumpCooldown = 1f;
|
||||
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
||||
[SerializeField] private LayerMask groundMask = ~0;
|
||||
[SerializeField] private float groundCheckDistance = 0.05f;
|
||||
|
||||
private Rigidbody2D body;
|
||||
private Transform target;
|
||||
@@ -20,6 +31,8 @@ public class TrickController : MonoBehaviour
|
||||
private float nextWanderTime;
|
||||
private bool isCarried;
|
||||
private float nextConsumeTime;
|
||||
private float desiredXVelocity;
|
||||
private float nextJumpTime;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -31,6 +44,7 @@ public class TrickController : MonoBehaviour
|
||||
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isCarried)
|
||||
@@ -54,7 +68,7 @@ public class TrickController : MonoBehaviour
|
||||
nextTargetTime = Time.time + targetRefreshInterval;
|
||||
}
|
||||
|
||||
if (target == null && Time.time >= nextWanderTime)
|
||||
if (Time.time >= nextWanderTime)
|
||||
{
|
||||
wanderDirection = Random.insideUnitCircle.normalized;
|
||||
if (wanderDirection.sqrMagnitude < 0.01f)
|
||||
@@ -76,20 +90,43 @@ public class TrickController : MonoBehaviour
|
||||
|
||||
Vector2 desiredVelocity;
|
||||
var stopForTreat = false;
|
||||
if (target != null)
|
||||
var avoidPlayerDirection = GetAvoidPlayerDirection();
|
||||
var avoidTrickDirection = GetAvoidTrickDirection();
|
||||
var hasTarget = target != null;
|
||||
var canConsume = Time.time >= nextConsumeTime;
|
||||
|
||||
var desiredX = 0f;
|
||||
if (hasTarget)
|
||||
{
|
||||
var direction = ((Vector2)target.position - (Vector2)transform.position).normalized;
|
||||
desiredVelocity = new Vector2(direction.x * moveSpeed, 0f);
|
||||
desiredX += direction.x * treatSeekWeight;
|
||||
stopForTreat = Vector2.Distance(transform.position, target.position) <= consumeDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
desiredVelocity = new Vector2(wanderDirection.x * (moveSpeed * 0.6f), 0f);
|
||||
desiredX += wanderDirection.x * wanderWeight;
|
||||
}
|
||||
|
||||
if (!hasTarget || !canConsume)
|
||||
{
|
||||
desiredX += avoidPlayerDirection * avoidPlayerWeight;
|
||||
}
|
||||
desiredX += avoidTrickDirection * avoidTrickWeight;
|
||||
|
||||
if (Mathf.Abs(desiredX) < 0.01f)
|
||||
{
|
||||
desiredX = wanderDirection.x * 0.2f;
|
||||
}
|
||||
|
||||
desiredVelocity = new Vector2(Mathf.Clamp(desiredX, -1f, 1f) * moveSpeed, 0f);
|
||||
|
||||
var velocity = body.linearVelocity;
|
||||
velocity.x = stopForTreat ? 0f : desiredVelocity.x;
|
||||
desiredXVelocity = stopForTreat ? 0f : desiredVelocity.x;
|
||||
velocity.x = Mathf.Lerp(velocity.x, desiredXVelocity, 0.25f);
|
||||
body.linearVelocity = velocity;
|
||||
|
||||
var isMoving = Mathf.Abs(desiredXVelocity) > 0.1f;
|
||||
TryJump(isMoving);
|
||||
}
|
||||
|
||||
private Transform FindNearestTreat()
|
||||
@@ -175,6 +212,126 @@ public class TrickController : MonoBehaviour
|
||||
Instantiate(gameObject, position, Quaternion.identity);
|
||||
}
|
||||
|
||||
private float GetAvoidPlayerDirection()
|
||||
{
|
||||
var players = FindObjectsByType<PlayerPlatformerController>(FindObjectsSortMode.None);
|
||||
if (players == null || players.Length == 0)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
Transform closest = null;
|
||||
var closestDist = float.MaxValue;
|
||||
var origin = transform.position;
|
||||
for (var i = 0; i < players.Length; i++)
|
||||
{
|
||||
var candidate = players[i];
|
||||
if (candidate == null || !candidate.gameObject.activeInHierarchy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var dist = Vector2.Distance(origin, candidate.transform.position);
|
||||
if (dist < closestDist)
|
||||
{
|
||||
closestDist = dist;
|
||||
closest = candidate.transform;
|
||||
}
|
||||
}
|
||||
|
||||
if (closest == null || closestDist > avoidPlayerRadius)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
return Mathf.Sign(origin.x - closest.position.x);
|
||||
}
|
||||
|
||||
private float GetAvoidTrickDirection()
|
||||
{
|
||||
var tricks = FindObjectsByType<TrickController>(FindObjectsSortMode.None);
|
||||
if (tricks == null || tricks.Length <= 1)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
Transform closest = null;
|
||||
var closestDist = float.MaxValue;
|
||||
var origin = transform.position;
|
||||
for (var i = 0; i < tricks.Length; i++)
|
||||
{
|
||||
var candidate = tricks[i];
|
||||
if (candidate == null || candidate == this || !candidate.gameObject.activeInHierarchy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var dist = Vector2.Distance(origin, candidate.transform.position);
|
||||
if (dist < closestDist)
|
||||
{
|
||||
closestDist = dist;
|
||||
closest = candidate.transform;
|
||||
}
|
||||
}
|
||||
|
||||
if (closest == null || closestDist > avoidTrickRadius)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
return Mathf.Sign(origin.x - closest.position.x);
|
||||
}
|
||||
|
||||
private void TryJump(bool isMoving)
|
||||
{
|
||||
if (!isMoving || Time.time < nextJumpTime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsGrounded())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var chance = jumpChancePerSecond * Time.fixedDeltaTime;
|
||||
if (Random.value > chance)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var velocity = body.linearVelocity;
|
||||
velocity.y = 0f;
|
||||
body.linearVelocity = velocity;
|
||||
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
|
||||
nextJumpTime = Time.time + jumpCooldown;
|
||||
}
|
||||
|
||||
private bool IsGrounded()
|
||||
{
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var bounds = collider.bounds;
|
||||
var checkSize = new Vector2(bounds.size.x * 0.9f, groundCheckDistance * 2f);
|
||||
var checkCenter = new Vector2(bounds.center.x, bounds.min.y - groundCheckDistance);
|
||||
var hits = Physics2D.OverlapBoxAll(checkCenter, checkSize, 0f, groundMask);
|
||||
for (var i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var hit = hits[i];
|
||||
if (hit != null && hit != collider)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void SetCarried(bool carried)
|
||||
{
|
||||
isCarried = carried;
|
||||
|
||||
Reference in New Issue
Block a user