2024-07-02 18:27:56 +00:00
|
|
|
using System;
|
2024-06-18 18:16:19 +00:00
|
|
|
using BehaviorDesigner.Runtime;
|
|
|
|
using BlueWater.Enemies;
|
2024-07-02 18:27:56 +00:00
|
|
|
using BlueWater.Interfaces;
|
|
|
|
using BlueWater.Items;
|
2024-06-18 18:16:19 +00:00
|
|
|
using BlueWater.Players;
|
|
|
|
using BlueWater.Players.Tycoons;
|
2024-07-02 18:27:56 +00:00
|
|
|
using BlueWater.Tycoons;
|
|
|
|
using BlueWater.Uis;
|
2024-06-18 18:16:19 +00:00
|
|
|
using Pathfinding;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BlueWater.Npcs.Customers
|
|
|
|
{
|
2024-07-02 18:27:56 +00:00
|
|
|
public class Customer : MonoBehaviour, IPlayerInteraction
|
2024-06-18 18:16:19 +00:00
|
|
|
{
|
|
|
|
// Variables
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#region Variables
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
// Components
|
2024-07-02 18:27:56 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public Transform Transform { get; private set; }
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public Rigidbody Rigidbody { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public CapsuleCollider CharacterCollider { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public BehaviorTree BehaviorTree { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public Transform VisualLook { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public MeshRenderer MeshRenderer { get; private set; }
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public Canvas InteractionCanvas { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public Transform InteractionUi { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public FoodBalloonUi FoodBalloonUi { get; private set; }
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
// Classes
|
|
|
|
[field: SerializeField, Required]
|
|
|
|
public SpineController SpineController { get; private set; }
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
[field: SerializeField, Required]
|
|
|
|
public AiMovement AIMovement { get; private set; }
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
[field: SerializeField]
|
2024-07-02 18:27:56 +00:00
|
|
|
public bool EnableInteraction { get; private set; } = true;
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private IAstarAI _astarAi;
|
|
|
|
public TableSeat TableSeat { get; private set; }
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private bool _isMoving;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
public bool IsMoving
|
|
|
|
{
|
|
|
|
get => _isMoving;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_isMoving == value) return;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
_isMoving = value;
|
|
|
|
SpineController.PlayAnimation(_isMoving
|
|
|
|
? TycoonPlayerSpineAnimation.run.ToString()
|
|
|
|
: TycoonPlayerSpineAnimation.idle.ToString(), true);
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private Vector3 _currentDirection = Vector3.right;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
public Vector3 CurrentDirection
|
|
|
|
{
|
|
|
|
get => _currentDirection;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == Vector3.zero) return;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
_currentDirection = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _isQuitting;
|
2024-07-02 18:27:56 +00:00
|
|
|
public Action OnInteraction;
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#endregion
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
// Unity events
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#region Unity events
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
InitializeComponents();
|
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private void OnApplicationQuit()
|
|
|
|
{
|
|
|
|
_isQuitting = true;
|
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
HandleMovement();
|
|
|
|
FlipVisualLook();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
if (_isQuitting) return;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
|
|
|
TycoonManager.Instance.CustomerManager.UnregisterCustomer(this);
|
2024-06-18 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
// Initialize methods
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#region Initialize methods
|
|
|
|
|
|
|
|
[Button("컴포넌트 초기화")]
|
|
|
|
protected virtual void InitializeComponents()
|
|
|
|
{
|
2024-07-02 18:27:56 +00:00
|
|
|
Transform = transform;
|
2024-06-18 18:16:19 +00:00
|
|
|
Rigidbody = GetComponent<Rigidbody>();
|
|
|
|
CharacterCollider = GetComponent<CapsuleCollider>();
|
|
|
|
BehaviorTree = GetComponent<BehaviorTree>();
|
|
|
|
VisualLook = transform.Find("VisualLook");
|
|
|
|
MeshRenderer = VisualLook.GetComponent<MeshRenderer>();
|
2024-07-02 18:27:56 +00:00
|
|
|
InteractionCanvas = transform.Find("InteractionCanvas").GetComponent<Canvas>();
|
|
|
|
InteractionCanvas.worldCamera = TycoonCameraManager.Instance.UiCamera;
|
|
|
|
InteractionUi = InteractionCanvas.transform.Find("InteractionUi");
|
|
|
|
InteractionUi.localScale = Vector3.one * (1 / transform.localScale.x);
|
|
|
|
FoodBalloonUi = InteractionCanvas.transform.Find("FoodBalloonUi").GetComponent<FoodBalloonUi>();
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
SpineController = GetComponent<SpineController>();
|
|
|
|
AIMovement = GetComponent<AiMovement>();
|
|
|
|
|
|
|
|
_astarAi = GetComponent<IAstarAI>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
{
|
|
|
|
BehaviorTree.EnableBehavior();
|
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Methods
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#region Methods
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
private void HandleMovement()
|
|
|
|
{
|
|
|
|
if (!_astarAi.canMove || _astarAi.isStopped)
|
|
|
|
{
|
|
|
|
IsMoving = false;
|
|
|
|
return;
|
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
CurrentDirection = _astarAi.velocity.normalized;
|
|
|
|
IsMoving = _astarAi.velocity != Vector3.zero || _astarAi.velocity != Vector3.positiveInfinity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FlipVisualLook()
|
|
|
|
{
|
|
|
|
var localScale = VisualLook.localScale;
|
|
|
|
localScale.x = CurrentDirection.x switch
|
|
|
|
{
|
|
|
|
> 0.01f => -Mathf.Abs(localScale.x),
|
|
|
|
< -0.01f => Mathf.Abs(localScale.x),
|
|
|
|
_ => localScale.x
|
|
|
|
};
|
|
|
|
VisualLook.localScale = localScale;
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
public void SetTableSeat(TableSeat tableSeat)
|
|
|
|
{
|
|
|
|
TableSeat = tableSeat;
|
|
|
|
FoodBalloonUi.Initialize(TableSeat);
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
public void SetCurrentDirection(Vector3 normalDirection) => CurrentDirection = normalDirection;
|
|
|
|
|
|
|
|
public void SetTableSeatPositionAndDirection()
|
|
|
|
{
|
|
|
|
transform.position = TableSeat.SeatTransform.position;
|
|
|
|
SetCurrentDirection(TableSeat.TableDirection);
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
public void SetFood(ItemData foodData)
|
|
|
|
{
|
|
|
|
TableSeat.SetFood(foodData.Sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Interaction()
|
|
|
|
{
|
|
|
|
OnInteraction?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ShowInteractionUi()
|
|
|
|
{
|
|
|
|
if (!InteractionUi) return;
|
|
|
|
|
|
|
|
InteractionUi.gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HideInteractionUi()
|
|
|
|
{
|
|
|
|
if (!InteractionUi) return;
|
|
|
|
|
|
|
|
InteractionUi.gameObject.SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RegisterPlayerInteraction()
|
|
|
|
{
|
|
|
|
if (EnableInteraction)
|
|
|
|
{
|
|
|
|
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.RegisterPlayerInteraction(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnregisterPlayerInteraction()
|
|
|
|
{
|
|
|
|
if (EnableInteraction)
|
|
|
|
{
|
|
|
|
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.UnregisterPlayerInteraction(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|