using System; using System.Collections; using UnityEngine; namespace BlueWater.Tycoons { [Serializable] public class RewardBox : InteractionFurniture { [SerializeField] private AnimationController _animationController; private RewardBoxType _rewardBoxType; private int _requiredGold; private bool _isInteracting; public void Initialize(LevelData levelData) { _rewardBoxType = levelData.RewardBoxType; _requiredGold = levelData.RewardBoxPrice; InteractionCanvas.UnlockUi.Initialize(DataManager.Instance.SpriteDataSo.Gold02, _requiredGold); } public override void Interaction() { if (_isInteracting) return; StartCoroutine(nameof(OpenRewardBoxCoroutine)); } public override void CancelInteraction() { } public override bool CanInteraction() { return TycoonManager.Instance.TycoonStatus.CurrentGold >= _requiredGold; } private IEnumerator OpenRewardBoxCoroutine() { _isInteracting = true; _animationController.SetAnimationParameter("isOpened", true); var animationStarted = false; yield return StartCoroutine(_animationController.WaitForAnimationToRun("Open", success => animationStarted = success)); if (!animationStarted) { Destroy(gameObject); yield break; } yield return new WaitUntil(() => _animationController.GetCurrentAnimationNormalizedTime() >= 1f); EventManager.InvokeAddedGold(-_requiredGold, false); switch (_rewardBoxType) { case RewardBoxType.Normal: EventManager.InvokeOpenedNormalRewardBox(); break; case RewardBoxType.Rare: EventManager.InvokeOpenedRareRewardBox(); break; default: throw new ArgumentOutOfRangeException(); } Destroy(gameObject); } } }