89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using BlueWaterProject.Type;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
namespace BlueWaterProject
|
|
{
|
|
public class MotionSickState : INpcState
|
|
{
|
|
public event Action OnEnterAction;
|
|
private NavMeshAgent agent;
|
|
private InShipMapInfo inShipMapInfo;
|
|
private bool isMovingToToilet;
|
|
private Toilet targetToilet;
|
|
|
|
public MotionSickState(NavMeshAgent agent, InShipMapInfo inShipMapInfo)
|
|
{
|
|
this.agent = agent;
|
|
this.inShipMapInfo = inShipMapInfo;
|
|
}
|
|
|
|
public void OnEnter(NpcStateMachine npcStateMachine)
|
|
{
|
|
var toilets = inShipMapInfo.Toilets;
|
|
|
|
// 화장실을 NPC와의 거리에 따라 정렬
|
|
var sortedToilets = toilets.OrderBy(toilet => Vector3.Distance(agent.transform.position, toilet.transform.position)).ToList();
|
|
|
|
Toilet availableToilet = null;
|
|
|
|
foreach (var toilet in sortedToilets)
|
|
{
|
|
// 사용 가능한 화장실을 찾음
|
|
if (!toilet.IsUsed)
|
|
{
|
|
availableToilet = toilet;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 사용 가능한 화장실이 있는 경우
|
|
if (availableToilet != null)
|
|
{
|
|
isMovingToToilet = true;
|
|
targetToilet = availableToilet;
|
|
agent.SetDestination(availableToilet.transform.position);
|
|
agent.stoppingDistance = 0;
|
|
}
|
|
else
|
|
{
|
|
npcStateMachine.InstantiateObject(DataManager.Inst.vomit, npcStateMachine.transform.position);
|
|
OnEnterAction?.Invoke();
|
|
npcStateMachine.RestorePreviousState();
|
|
}
|
|
}
|
|
|
|
public void OnUpdate(NpcStateMachine npcStateMachine)
|
|
{
|
|
// 화장실로 이동 중이라면
|
|
if (isMovingToToilet)
|
|
{
|
|
// 도착 여부 확인
|
|
if (agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
|
|
{
|
|
// 이동 상태 업데이트
|
|
isMovingToToilet = false;
|
|
|
|
// 도착했다면 화장실 사용
|
|
targetToilet.UseToilet();
|
|
OnEnterAction?.Invoke();
|
|
npcStateMachine.RestorePreviousState();
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnExit(NpcStateMachine npcStateMachine)
|
|
{
|
|
|
|
}
|
|
|
|
public INpcState Clone()
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
} |