CapersProject/Assets/02.Scripts/Prop/Tycoon/RewardBox.cs

75 lines
2.2 KiB
C#
Raw Normal View History

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