2026-01-09 08:45:22 +03:00

122 lines
2.6 KiB
C#

using UnityEngine;
using UnityEngine.AI;
public class Pirate : MonoBehaviour
{
[Header("Îáùèå êîìïîíåíòû")]
public Animator animator;
public NavMeshAgent agent;
[Header("Ïîâåäåíèÿ")]
public PirateBehavior[] behaviors;
[Header("Îðóæèå / ýêèïèðîâêà")]
[SerializeField] private GameObject musket;
public GameObject Musket => musket;
public Vector3 HomePosition { get; private set; }
private PirateBehavior currentBehavior;
// Ïóáëè÷íîå ñâîéñòâî äëÿ ÷òåíèÿ òåêóùåãî ïîâåäåíèÿ
public PirateBehavior CurrentBehavior => currentBehavior;
public void ShowMusket(bool show)
{
if (musket != null)
musket.SetActive(show);
}
public bool IsBusy<T>() where T : PirateBehavior
{
return currentBehavior is T;
}
public bool IsBusy(System.Type type)
{
return currentBehavior != null && type.IsInstanceOfType(currentBehavior);
}
private void Awake()
{
HomePosition = transform.position;
}
private void Start()
{
behaviors = GetComponents<PirateBehavior>();
foreach (var b in behaviors)
b?.Deactivate(true);
}
public void ClearCurrentBehavior(PirateBehavior behavior, bool interrupted)
{
if (currentBehavior == behavior)
{
currentBehavior = null;
if (!interrupted)
{
SetBehavior<PirateIdle>();
if (currentBehavior != null)
currentBehavior.Activate();
}
}
}
public void SetBehavior<T>() where T : PirateBehavior
{
// Âûêëþ÷àåì ïðåäûäóùåå
if (currentBehavior != null)
currentBehavior.Deactivate(true);
// Âêëþ÷àåì íîâîå
foreach (var b in behaviors)
{
if (b is T tBehavior)
{
//tBehavior.Activate();
currentBehavior = tBehavior;
break;
}
}
}
public void ResetByIdleHome()
{
if (agent != null)
{
agent.ResetPath();
agent.isStopped = true;
if (NavMesh.SamplePosition(HomePosition, out var hit, 1f, NavMesh.AllAreas))
agent.Warp(hit.position);
else
transform.position = HomePosition;
}
else
{
transform.position = HomePosition;
}
if (CurrentBehavior != null)
CurrentBehavior.Deactivate(false);
else
{
SetBehavior<PirateIdle>();
CurrentBehavior?.Activate();
}
}
}