using System; using System.Collections; using System.Collections.Generic; using DDD.Audios; using DDD.Interfaces; using DDD.Npcs.Crews; using DDD.Npcs.Crews.Server; using DDD.Npcs.Customers; using DDD.ScriptableObjects; using DDD.Utility; using UnityEngine; namespace DDD.Tycoons { public class ServingTable : InteractionFurniture, ICrewInteraction { [SerializeField] private SpriteRenderer _itemSpriteRenderer; [SerializeField] private List _outlineRenderers = new(); [SerializeField] private string _putDownSfxName = "PutDownCocktail"; private CraftRecipeData _currentCraftRecipeData; public CraftRecipeData CurrentCraftRecipeData { get => _currentCraftRecipeData; private set { _currentCraftRecipeData = value; if (_currentCraftRecipeData != null) { Utils.StartUniqueCoroutine(this, ref _findCustomerMatchingItemInstance, FindCustomerMatchingItem()); } else { if (_findCustomerMatchingItemInstance != null) { StopCoroutine(_findCustomerMatchingItemInstance); _findCustomerMatchingItemInstance = null; } if (_findServerCrewInstance != null) { StopCoroutine(_findServerCrewInstance); _findServerCrewInstance = null; } } } } private Customer _orderedCustomer; private List _originalMaterials; private Coroutine _findCustomerMatchingItemInstance; private Coroutine _findServerCrewInstance; public event Action OnInteractionCompleted; protected override void Awake() { base.Awake(); _itemSpriteRenderer.sprite = null; _itemSpriteRenderer.enabled = false; _originalMaterials = new List(_outlineRenderers.Count); for (int i = 0; i < _outlineRenderers.Count; i++) { _originalMaterials.Add(_outlineRenderers[i].material); } } public override void Interaction() { // 테이블의 칵테일을 가져가는 경우 if (CurrentCraftRecipeData != null) { CurrentTycoonPlayer.PickupCook(CurrentCraftRecipeData); _itemSpriteRenderer.sprite = null; _itemSpriteRenderer.enabled = false; CurrentCraftRecipeData = null; } // 테이블에 칵테일을 놓는 경우 else { AudioManager.Instance.PlaySfx(_putDownSfxName); CurrentCraftRecipeData = CurrentTycoonPlayer.TycoonPickupHandler.CurrentCraftRecipeData; _itemSpriteRenderer.sprite = CurrentCraftRecipeData.Sprite; _itemSpriteRenderer.enabled = true; CurrentTycoonPlayer.ServedCook(true); } } public override bool CanInteraction() { // 1. 테이블에 음식이 있고, 플레이어가 음식을 들고 있지 않은 경우 // 2. 테이블에 음식이 없고, 플레이어가 음식을 들고 있는 경우 return (CurrentCraftRecipeData != null && !CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpCook) || (CurrentCraftRecipeData == null && CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpCook); } public override void ShowInteractionUi() { if (CurrentCraftRecipeData != null) { InteractionMessage = Utils.GetLocalizedString("Action002"); } else { InteractionMessage = Utils.GetLocalizedString("Action003"); } base.ShowInteractionUi(); for (int i = 0; i < _outlineRenderers.Count; i++) { _outlineRenderers[i].material = OutlineMaterial; } } public override void HideInteractionUi() { base.HideInteractionUi(); for (int i = 0; i < _outlineRenderers.Count; i++) { _outlineRenderers[i].material = _originalMaterials[i]; } } public void InteractionCrew(Crew crew) { // 종업원이 테이블의 칵테일을 가져가는 경우 if (CurrentCraftRecipeData != null) { var serverCrew = (ServerCrew)crew; serverCrew.TakeFromServingTable(CurrentCraftRecipeData, _orderedCustomer); _itemSpriteRenderer.enabled = false; // InteractionCanvas.BalloonUi.DiscardItem(); CurrentCraftRecipeData = null; _orderedCustomer = null; } // 종업원이 테이블에 칵테일을 놓는 경우 else { var serverCrew = (ServerCrew)crew; CurrentCraftRecipeData = serverCrew.CurrentPickupItem; _itemSpriteRenderer.sprite = CurrentCraftRecipeData.Sprite; _itemSpriteRenderer.enabled = true; // InteractionCanvas.BalloonUi.SetItemImage(CurrentPickupItem); serverCrew.ResetMission(); } } public void CancelInteractionCrew() { throw new NotImplementedException(); } public bool CanInteractionCrew(Crew crew = null) { var servingCrew = (ServerCrew)crew; if (!servingCrew) { throw new Exception("상호작용 오브젝트 오류"); } return (servingCrew.CurrentActionType == ActionType.TakeCocktail && CurrentCraftRecipeData != null && _orderedCustomer) || servingCrew.CurrentActionType == ActionType.PlaceOnServingTable && CurrentCraftRecipeData == null; } private IEnumerator FindCustomerMatchingItem() { var waitTime = new WaitForSeconds(2f); while (true) { _orderedCustomer = TycoonManager.Instance.CustomerController.FindCustomerMatchingItem(CurrentCraftRecipeData); if (_orderedCustomer && _orderedCustomer.CanInteractionCrew()) { var crewController = TycoonManager.Instance.CrewController; Utils.StartUniqueCoroutine(this, ref _findServerCrewInstance, crewController.FindClosestCrewCoroutine(CenterTransform.position, crewController.ServerCrews, crew => crew.OnMission(this, _orderedCustomer, ActionType.TakeCocktail))); } yield return waitTime; } } } }