2023-10-30 15:45:05 +00:00
|
|
|
using System;
|
2023-10-18 02:36:56 +00:00
|
|
|
using System.Collections;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
2023-10-30 15:45:05 +00:00
|
|
|
using Random = UnityEngine.Random;
|
2023-10-18 02:36:56 +00:00
|
|
|
|
|
|
|
// 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;
|
2023-10-23 03:12:13 +00:00
|
|
|
private Transform visualLook;
|
2023-11-01 09:25:09 +00:00
|
|
|
private bool isIdle = true;
|
2023-10-18 02:36:56 +00:00
|
|
|
|
2023-10-23 03:12:13 +00:00
|
|
|
public UsuallyPointState(NavMeshAgent agent, Transform[] usuallyPoints, Transform visualLook)
|
2023-10-18 02:36:56 +00:00
|
|
|
{
|
|
|
|
this.agent = agent;
|
|
|
|
this.usuallyPoints = usuallyPoints;
|
2023-10-23 03:12:13 +00:00
|
|
|
this.visualLook = visualLook;
|
2023-10-18 02:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void OnEnter(NpcStateMachine npcStateMachine)
|
|
|
|
{
|
2023-11-01 09:25:09 +00:00
|
|
|
isIdle = true;
|
2023-10-18 02:36:56 +00:00
|
|
|
agent.updateRotation = false;
|
2023-10-23 03:12:13 +00:00
|
|
|
//localScale = agent.transform.localScale;
|
|
|
|
localScale = visualLook.localScale;
|
2023-10-18 02:36:56 +00:00
|
|
|
lastPosition = agent.transform.position;
|
|
|
|
GoToNextPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnUpdate(NpcStateMachine npcStateMachine)
|
|
|
|
{
|
2023-11-01 09:25:09 +00:00
|
|
|
if(isIdle == false) return;
|
2023-10-18 02:36:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-10-23 03:12:13 +00:00
|
|
|
visualLook.localScale = localScale;
|
2023-10-18 02:36:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2023-11-01 09:25:09 +00:00
|
|
|
isIdle = false;
|
2023-10-18 02:36:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 08:52:06 +00:00
|
|
|
public INpcState Clone()
|
|
|
|
{
|
|
|
|
var newState = new UsuallyPointState(agent, usuallyPoints, visualLook);
|
|
|
|
newState.destPoint = this.destPoint; // 이전 목적지를 복제
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2023-10-18 02:36:56 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|