using UnityEngine; using UnityEngine.AI; namespace BlueWaterProject { public class FindTableState : INpcState { private NavMeshAgent agent; private TycoonMapInfo tycoonMapInfo; private Table[] tables; private bool doSeat; private Seat assignedSeat; public FindTableState(NavMeshAgent agent, TycoonMapInfo tycoonMapInfo) { this.agent = agent; this.tycoonMapInfo = tycoonMapInfo; tables = tycoonMapInfo.Tables.GetComponentsInChildren(); } public void OnEnter(NpcStateMachine npcStateMachine) { CheckAndAssignSeat(); } public void OnUpdate(NpcStateMachine npcStateMachine) { var currentTables = tycoonMapInfo.Tables.GetComponentsInChildren
(); if (currentTables.Length != tables.Length) { tables = currentTables; CheckAndAssignSeat(); } // NPC가 목적지에 도달했는지 확인 if (doSeat && !agent.pathPending && agent.remainingDistance <= 0.5f) { // 다음 상태로 전환 //npcStateMachine.ChangeState(new NextState(agent, /* 다른 필요한 매개변수 */)); } } private void CheckAndAssignSeat() { if (!doSeat) { foreach(var table in tables) { foreach(var seat in table.SeatPoints) { if (seat.IsUsing) continue; agent.SetDestination(seat.transform.position); if (assignedSeat != null) { assignedSeat.IsUsing = false; assignedSeat.IsDirty = true; } assignedSeat = seat; assignedSeat.IsUsing = true; doSeat = true; return; } } } Debug.Log("All tables are occupied. No place to sit"); } public void OnExit(NpcStateMachine npcStateMachine) { if (assignedSeat == null) return; assignedSeat.IsUsing = false; assignedSeat.IsDirty = true; doSeat = false; } } }