153 lines
4.3 KiB
C#
153 lines
4.3 KiB
C#
using UnityEngine;
|
|
using Pixelplacement;
|
|
|
|
public class LawnMowerChaseBehaviour : MonoBehaviour
|
|
{
|
|
public Spline ChaseSpline;
|
|
public Transform LawnMowerObject;
|
|
public float chaseDuration;
|
|
public float chaseDelay;
|
|
[Range(0, 1)] public float startPercentage; // Exposed in Inspector
|
|
|
|
private const float AnchorThreshold = 0.05f;
|
|
private bool _wasAtStart = false;
|
|
private bool _wasAtEnd = false;
|
|
|
|
// For initial tween tracking
|
|
private bool _initialTweenActive = true;
|
|
private float _initialTargetAnchor = 1f;
|
|
|
|
//Reference to the gardener's gameobject
|
|
public GameObject gardenerRef = null;
|
|
public Animator gardenerAnimator = null;
|
|
public bool gardenerChasing = true;
|
|
|
|
void Start()
|
|
{
|
|
LawnMowerObject.position = ChaseSpline.GetPosition(startPercentage);
|
|
|
|
float distanceToStart = Mathf.Abs(startPercentage - 0f);
|
|
float distanceToEnd = Mathf.Abs(startPercentage - 1f);
|
|
|
|
if (distanceToStart < distanceToEnd)
|
|
{
|
|
// Tween from startPercentage to 1
|
|
_initialTargetAnchor = 1f;
|
|
Tween.Spline(
|
|
ChaseSpline,
|
|
LawnMowerObject,
|
|
startPercentage,
|
|
1,
|
|
false,
|
|
chaseDuration * (1 - startPercentage),
|
|
chaseDelay,
|
|
Tween.EaseInOut,
|
|
Tween.LoopType.None
|
|
);
|
|
}
|
|
else
|
|
{
|
|
// Tween from startPercentage to 0
|
|
_initialTargetAnchor = 0f;
|
|
Tween.Spline(
|
|
ChaseSpline,
|
|
LawnMowerObject,
|
|
startPercentage,
|
|
0,
|
|
false,
|
|
chaseDuration * startPercentage,
|
|
chaseDelay,
|
|
Tween.EaseInOut,
|
|
Tween.LoopType.None
|
|
);
|
|
}
|
|
_initialTweenActive = true;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float percentage = ChaseSpline.ClosestPoint(LawnMowerObject.position);
|
|
|
|
// Handle initial tween completion
|
|
if (_initialTweenActive)
|
|
{
|
|
if (Mathf.Abs(percentage - _initialTargetAnchor) <= AnchorThreshold)
|
|
{
|
|
// Start ping-pong tween between extremes
|
|
StartPingPongTween(_initialTargetAnchor, 1f - _initialTargetAnchor);
|
|
_initialTweenActive = false;
|
|
}
|
|
return; // Don't process flip logic until ping-pong starts
|
|
}
|
|
|
|
// Detect start anchor
|
|
if (percentage <= AnchorThreshold)
|
|
{
|
|
if (!_wasAtStart)
|
|
{
|
|
flipSprite();
|
|
_wasAtStart = true;
|
|
_wasAtEnd = false;
|
|
}
|
|
}
|
|
// Detect end anchor
|
|
else if (percentage >= 1f - AnchorThreshold)
|
|
{
|
|
if (!_wasAtEnd)
|
|
{
|
|
flipSprite();
|
|
_wasAtEnd = true;
|
|
_wasAtStart = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_wasAtStart = false;
|
|
_wasAtEnd = false;
|
|
}
|
|
}
|
|
|
|
private void StartPingPongTween(float from, float to)
|
|
{
|
|
Tween.Spline(
|
|
ChaseSpline,
|
|
LawnMowerObject,
|
|
from,
|
|
to,
|
|
false,
|
|
chaseDuration,
|
|
0,
|
|
Tween.EaseInOut,
|
|
Tween.LoopType.PingPong
|
|
);
|
|
}
|
|
|
|
private void flipSprite()
|
|
{
|
|
if (gardenerRef == null)
|
|
{
|
|
gardenerRef = GameObject.Find("GardenerRunningSprite");
|
|
gardenerAnimator = gardenerRef.GetComponent<Animator>();
|
|
}
|
|
|
|
Vector3 scale = LawnMowerObject.transform.localScale;
|
|
Vector3 rotation = LawnMowerObject.transform.eulerAngles;
|
|
scale.x *= -1;
|
|
rotation.z *= -1;
|
|
LawnMowerObject.transform.localScale = scale;
|
|
LawnMowerObject.transform.eulerAngles = rotation;
|
|
if (gardenerChasing == true)
|
|
{
|
|
gardenerRef.transform.localPosition = new Vector3(-6.3f, -2.9f, gardenerRef.transform.localPosition.z);
|
|
gardenerAnimator.SetBool("IsScared?", true);
|
|
gardenerChasing = false;
|
|
}
|
|
else
|
|
{
|
|
gardenerRef.transform.localPosition = new Vector3(8.3f, 3.9f, gardenerRef.transform.localPosition.z);
|
|
gardenerAnimator.SetBool("IsScared?", false);
|
|
gardenerChasing = true;
|
|
}
|
|
}
|
|
}
|