CapersProject/Assets/02.Scripts/Character/Npc/Customer.cs

320 lines
9.3 KiB
C#
Raw Normal View History

using System;
using BehaviorDesigner.Runtime;
using BlueWater.Enemies;
using BlueWater.Interfaces;
using BlueWater.Items;
using BlueWater.Players;
using BlueWater.Tycoons;
using BlueWater.Uis;
using Pathfinding;
using PixelCrushers.DialogueSystem;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BlueWater.Npcs.Customers
{
public static class CustomerSpineAnimation
{
public const string Idle = "Idle";
public const string Walk = "Run";
}
public class Customer : MonoBehaviour, IPlayerInteraction
{
// Variables
#region Variables
// Components
[field: SerializeField]
public Transform CenterTransform { get; private set; }
[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; }
[field: SerializeField]
public BarkTrigger BarkTrigger { get; private set; }
[field: SerializeField]
2024-09-26 11:50:39 +00:00
public InteractionCanvas InteractionCanvas { get; private set; }
[field: SerializeField]
public ParticleSystem PayMoneyParticle { get; private set; }
[field: SerializeField]
2024-09-10 10:25:05 +00:00
public BalloonUi BalloonUi { get; private set; }
[field: SerializeField]
public PayMoneyUi PayMoneyUi { get; private set; }
// Classes
[field: SerializeField, Required]
public SpineController SpineController { get; private set; }
[field: SerializeField, Required]
public AiMovement AIMovement { get; private set; }
[field: SerializeField]
public bool EnableInteraction { get; private set; } = true;
[field: SerializeField]
public float InteractionRadius { get; private set; } = 2f;
2024-09-24 10:35:49 +00:00
public string InteractionMessage { get; private set; }
private IAstarAI _astarAi;
public LevelData LevelData { get; private set; }
public TableSeat TableSeat { get; private set; }
2024-09-30 12:05:19 +00:00
public CocktailData CocktailData { get; private set; }
// private int _happyPoint;
// public int HappyPoint
// {
// get => _happyPoint;
// private set
// {
// var newHappyPoint = Mathf.Max(0, value);
// _happyPoint = newHappyPoint;
// }
// }
private bool _isMoving;
public bool IsMoving
{
get => _isMoving;
set
{
if (_isMoving == value) return;
_isMoving = value;
SpineController.PlayAnimation(_isMoving
? CustomerSpineAnimation.Walk.ToString()
: CustomerSpineAnimation.Idle.ToString(), true);
}
}
private Vector3 _currentDirection = Vector3.right;
public Vector3 CurrentDirection
{
get => _currentDirection;
set
{
if (value == Vector3.zero) return;
_currentDirection = value;
}
}
private int _paidAmount;
private int _foodPrice;
private int _tipAmount;
private bool _isQuitting;
public Action OnInteraction;
#endregion
// Unity events
#region Unity events
private void Awake()
{
InitializeComponents();
}
private void OnApplicationQuit()
{
_isQuitting = true;
}
private void Update()
{
HandleMovement();
FlipVisualLook();
}
private void OnDestroy()
{
if (_isQuitting) return;
CustomerManager.Instance.UnregisterCustomer(this);
}
#endregion
// Initialize methods
#region Initialize methods
[Button("컴포넌트 초기화")]
protected virtual void InitializeComponents()
{
CenterTransform = transform;
Rigidbody = GetComponent<Rigidbody>();
CharacterCollider = GetComponent<CapsuleCollider>();
BehaviorTree = GetComponent<BehaviorTree>();
VisualLook = transform.Find("VisualLook");
MeshRenderer = VisualLook.GetComponent<MeshRenderer>();
BarkTrigger = transform.Find("DialogueSystem").GetComponent<BarkTrigger>();
PayMoneyParticle = transform.Find("PayMoneyParticle").GetComponent<ParticleSystem>();
2024-09-26 11:50:39 +00:00
InteractionCanvas = transform.GetComponentInChildren<InteractionCanvas>();
2024-09-10 10:25:05 +00:00
BalloonUi = InteractionCanvas.transform.Find("FoodBalloonUi").GetComponent<BalloonUi>();
PayMoneyUi = InteractionCanvas.transform.Find("PayMoneyUi").GetComponent<PayMoneyUi>();
SpineController = GetComponent<SpineController>();
AIMovement = GetComponent<AiMovement>();
_astarAi = GetComponent<IAstarAI>();
}
public void Initialize(LevelData levelData)
{
LevelData = levelData;
//CustomerData = CustomerManager.Instance.GetRandomCustomerData();
//AIMovement.SetMoveSpeed(CustomerData.MoveSpeed);
//HappyPoint = CustomerData.BaseHappyPoint;
BehaviorTree.EnableBehavior();
}
#endregion
// Methods
#region Methods
private void HandleMovement()
{
if (!_astarAi.canMove || _astarAi.isStopped)
{
IsMoving = false;
return;
}
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;
}
public void SetTableSeat(TableSeat tableSeat)
{
TableSeat = tableSeat;
2024-09-10 10:25:05 +00:00
BalloonUi.Initialize(TableSeat);
}
public void SetCurrentDirection(Vector3 normalDirection) => CurrentDirection = normalDirection;
public void SetTableSeatPositionAndDirection()
{
transform.position = TableSeat.SeatTransform.position;
SetCurrentDirection(TableSeat.TableDirection);
}
2024-09-30 12:05:19 +00:00
public void ServedItem(CocktailData cocktailData)
{
2024-09-30 12:05:19 +00:00
CocktailData = cocktailData;
TableSeat.SetFood(CocktailData.Sprite);
}
public void Interaction()
{
OnInteraction?.Invoke();
}
2024-09-09 12:27:15 +00:00
public virtual void CancelInteraction() { }
public bool CanInteraction() => true;
2024-09-26 11:50:39 +00:00
public virtual void ShowInteractionUi()
{
SpineController.EnableCustomMaterial();
2024-09-26 11:50:39 +00:00
EventManager.OnShowInteractionUi?.Invoke(InteractionMessage);
}
2024-09-26 11:50:39 +00:00
public virtual void HideInteractionUi()
{
SpineController.DisableCustomMaterial();
2024-09-26 11:50:39 +00:00
EventManager.OnHideInteractionUi?.Invoke();
}
public void RegisterPlayerInteraction()
{
if (EnableInteraction)
{
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.RegisterPlayerInteraction(this);
}
}
public void UnregisterPlayerInteraction()
{
if (EnableInteraction)
{
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.UnregisterPlayerInteraction(this);
}
}
//public void AddHappyPoint(int value) => HappyPoint += value;
public void Bark(string conversation, BarkOrder barkOrder = BarkOrder.Random)
{
if (string.IsNullOrEmpty(conversation)) return;
BarkTrigger.barkOrder = barkOrder;
BarkTrigger.conversation = conversation;
BarkTrigger.OnUse();
}
2024-09-30 12:05:19 +00:00
public void PayMoney()
{
PayMoneyParticle.gameObject.SetActive(true);
PayMoneyParticle.Play();
2024-09-30 12:05:19 +00:00
PayMoneyUi.PayMoney(LevelData.Gold);
}
2024-09-30 12:05:19 +00:00
// public void PayMoney(int foodPrice, int tipAmount)
// {
// _foodPrice = foodPrice;
// _tipAmount = tipAmount;
// _paidAmount = _foodPrice + _tipAmount;
//
// PayMoneyParticle.gameObject.SetActive(true);
// PayMoneyParticle.Play();
// PayMoneyUi.PayMoney(_paidAmount);
// }
public void CheckOut()
{
//var customerVisitInfo = new CustomerVisitInfo(HappyPoint, _foodPrice, _tipAmount, _paidAmount);
//TycoonManager.Instance.TycoonStageController.RegisterCustomerVisitInfo(customerVisitInfo);
Destroy(gameObject);
}
#endregion
}
}