using System; using System.Collections; using System.Linq; using BlueWater.Interfaces; using BlueWater.Npcs.Crews; using BlueWater.Utility; using UnityEngine; namespace BlueWater.Tycoons { [Serializable] public class Vomiting : InteractionFurniture, ICrewInteraction { [SerializeField] private float _interactionHoldingTime = 3f; private Sprite vomitingImage; private Coroutine _findCleanerCrewInstance; private bool _isPlayerInteracting; private bool _isCrewInteracting; private float _playerElapsedTime; private float _crewElapsedTime; public event Action OnInteractionCompleted; protected override void OnEnable() { base.OnEnable(); Initialize(); } private void Update() { if (InteractionCanvas.BalloonUi.IsWaitTimeOver()) { var damageable = GameManager.Instance.CurrentTycoonPlayer.GetComponent(); damageable?.TakeDamage(1); Destroy(); } if (_isPlayerInteracting) { var clamp = Mathf.Clamp(_playerElapsedTime / _interactionHoldingTime, 0f, 1f); EventManager.OnInteracting?.Invoke(clamp); if (_playerElapsedTime > _interactionHoldingTime) { Destroy(); } _playerElapsedTime += Time.deltaTime; } if (_isCrewInteracting) { if (_crewElapsedTime > _interactionHoldingTime) { OnInteractionCompleted?.Invoke(); Destroy(); } _crewElapsedTime += Time.deltaTime; } } public void Initialize() { InteractionMessage = "치우기"; vomitingImage = VisualLook.GetComponent().sprite; InteractionCanvas.BalloonUi.OrderItem(vomitingImage, 0, TycoonManager.Instance.TycoonStageController.StageDataSo.VomitingWaitTime); Utils.StartUniqueCoroutine(this, ref _findCleanerCrewInstance, FindCleanerCrewCoroutine()); } private IEnumerator FindCleanerCrewCoroutine() { var waitTime = new WaitForSeconds(2f); while (true) { var cleaners = TycoonManager.Instance.CrewController.CleanerCrews; if (cleaners == null || cleaners.Count == 0) { yield return waitTime; continue; } var closestCleaners = cleaners.OrderBy(element => Vector3.Distance(element.transform.position, transform.position)).ToList(); var isFound = false; foreach (var element in closestCleaners.Where(element => !element.IsOnMission)) { element.OnMission(this); isFound = true; break; } if (isFound) { break; } yield return waitTime; } } public override void Interaction() { _playerElapsedTime = 0f; EventManager.OnInteracting?.Invoke(_playerElapsedTime); GameManager.Instance.CurrentTycoonPlayer.IsCleaningFloor = true; _isPlayerInteracting = true; } public override void CancelInteraction() { _playerElapsedTime = 0f; EventManager.OnInteracting?.Invoke(_playerElapsedTime); GameManager.Instance.CurrentTycoonPlayer.IsCleaningFloor = false; _isPlayerInteracting = false; } public override bool CanInteraction() { return !GameManager.Instance.CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpCocktail(); } private void Destroy() { GameManager.Instance.CurrentTycoonPlayer.IsCleaningFloor = false; Destroy(gameObject); } public void InteractionCrew(Crew crew) { _crewElapsedTime = 0f; _isCrewInteracting = true; crew.IsCleaningFloor = true; } public void CancelInteractionCrew() { _crewElapsedTime = 0f; _isCrewInteracting = false; } public bool CanInteractionCrew() { return !_isPlayerInteracting; } } }