100 lines
3.1 KiB
C#
100 lines
3.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
namespace BlueWaterProject
|
|
{
|
|
public class UsuallyPointState : INpcState
|
|
{
|
|
private NavMeshAgent agent;
|
|
private Transform[] usuallyPoints;
|
|
private int destPoint;
|
|
private Vector3 lastPosition;
|
|
private Vector3 localScale;
|
|
private float waitTime = 5f;
|
|
private bool isWaiting = false;
|
|
private Transform visualLook;
|
|
|
|
public UsuallyPointState(NavMeshAgent agent, Transform[] usuallyPoints, Transform visualLook)
|
|
{
|
|
this.agent = agent;
|
|
this.usuallyPoints = usuallyPoints;
|
|
this.visualLook = visualLook;
|
|
}
|
|
|
|
public event Action OnUnityEvent;
|
|
|
|
public void OnEnter(NpcStateMachine npcStateMachine)
|
|
{
|
|
agent.updateRotation = false;
|
|
//localScale = agent.transform.localScale;
|
|
localScale = visualLook.localScale;
|
|
lastPosition = agent.transform.position;
|
|
GoToNextPoint();
|
|
}
|
|
|
|
public void OnUpdate(NpcStateMachine npcStateMachine)
|
|
{
|
|
var moveDirection = agent.transform.position - lastPosition;
|
|
if (moveDirection.x < 0)
|
|
{
|
|
localScale.x = Mathf.Abs(localScale.x) * -1;
|
|
}
|
|
else if (moveDirection.x > 0)
|
|
{
|
|
localScale.x = Mathf.Abs(localScale.x);
|
|
}
|
|
|
|
visualLook.localScale = localScale;
|
|
lastPosition = agent.transform.position;
|
|
|
|
if (!agent.pathPending && agent.remainingDistance < 1f)
|
|
{
|
|
if (!isWaiting)
|
|
{
|
|
// 랜덤한 대기 시간 설정
|
|
waitTime = UnityEngine.Random.Range(3f, 10f);
|
|
isWaiting = true;
|
|
|
|
// NpcStateMachine 또는 Npc 클래스에서 Coroutine을 시작
|
|
npcStateMachine.StartWaitCoroutine(waitTime, this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void EndWait()
|
|
{
|
|
isWaiting = false;
|
|
GoToNextPoint();
|
|
}
|
|
|
|
public void OnExit(NpcStateMachine npcStateMachine)
|
|
{
|
|
// 필요한 경우, 상태를 빠져나올 때 수행할 로직을 여기에 작성
|
|
}
|
|
|
|
public INpcState Clone()
|
|
{
|
|
var newState = new UsuallyPointState(agent, usuallyPoints, visualLook);
|
|
newState.destPoint = this.destPoint; // 이전 목적지를 복제
|
|
return newState;
|
|
}
|
|
|
|
private void GoToNextPoint()
|
|
{
|
|
if (usuallyPoints.Length == 0) return;
|
|
destPoint = Random.Range(0, usuallyPoints.Length);
|
|
agent.destination = usuallyPoints[destPoint].position;
|
|
}
|
|
|
|
private IEnumerator WaitAndGoToNextPoint()
|
|
{
|
|
var waitTime = Random.Range(3f, 10f);
|
|
yield return new WaitForSeconds(waitTime);
|
|
GoToNextPoint();
|
|
}
|
|
}
|
|
} |