83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using System.Collections;
|
|
|
|
public class PirateDance : PirateBehavior
|
|
{
|
|
[Header("Òàéìèíãè")]
|
|
public float danceDuration = 10f;
|
|
|
|
[Header("Àíèìàöèè òàíöà")]
|
|
public int danceCount = 5;
|
|
|
|
[HideInInspector] public Transform targetPoint; // çàäà¸òñÿ èçâíå
|
|
[HideInInspector] public System.Action<Transform> onDanceFinished; // êîëáýê äëÿ îñâîáîæäåíèÿ òî÷êè
|
|
|
|
private bool isDancing = false;
|
|
|
|
|
|
public void Init(Transform point, System.Action<Transform> onFinished)
|
|
{
|
|
targetPoint = point;
|
|
onDanceFinished = onFinished;
|
|
Activate(); // çàïóñêàåì äâèæåíèå/òàíåö
|
|
}
|
|
|
|
public override void Activate()
|
|
{
|
|
if (targetPoint == null || pirate.agent == null) return;
|
|
|
|
enabled = true;
|
|
|
|
pirate.agent.SetDestination(targetPoint.position);
|
|
pirate.agent.isStopped = false;
|
|
|
|
if (Animator != null)
|
|
Animator.SetBool("isWalking", true);
|
|
|
|
StartCoroutine(MoveAndDanceRoutine());
|
|
}
|
|
|
|
public override void Deactivate(bool interrupted = false)
|
|
{
|
|
StopAllCoroutines();
|
|
onDanceFinished?.Invoke(targetPoint);
|
|
enabled = false;
|
|
|
|
if (Animator != null)
|
|
{
|
|
Animator.SetBool("Dance", false);
|
|
Animator.SetBool("isWalking", false);
|
|
}
|
|
|
|
isDancing = false;
|
|
targetPoint = null;
|
|
onDanceFinished = null;
|
|
|
|
pirate.ClearCurrentBehavior(this, interrupted);
|
|
}
|
|
|
|
private IEnumerator MoveAndDanceRoutine()
|
|
{
|
|
// Æäåì ïîêà äîéäåò äî òî÷êè
|
|
while (pirate.agent.pathPending || pirate.agent.remainingDistance > pirate.agent.stoppingDistance)
|
|
yield return null;
|
|
|
|
pirate.agent.isStopped = true;
|
|
|
|
if (Animator != null)
|
|
{
|
|
Animator.SetBool("isWalking", false);
|
|
|
|
float danceValue = Random.Range(0, danceCount) / (float)(danceCount - 1);
|
|
Animator.SetFloat("DanceIndex", danceValue);
|
|
Animator.SetBool("Dance", true);
|
|
}
|
|
|
|
isDancing = true;
|
|
|
|
yield return new WaitForSeconds(danceDuration);
|
|
Deactivate();
|
|
}
|
|
}
|