SamsonGame/Assets/Scripts/Game/UI/Elements/BuffPresenter.cs

265 lines
7.9 KiB
C#

using System;
using System.Collections;
using Coffee.UIEffects;
using game;
using I2.Loc;
using RND.SDK;
using UnityEngine;
using UnityEngine.UI;
namespace RND
{
public class BuffPresenter : MonoBehaviour
{
[Header("Field")] [SerializeField] private EnumBuffType _buffType;
[Header("Field")] [SerializeField] private Image _icon;
[SerializeField] private Image _buffIcon;
[SerializeField] private Text _title;
[SerializeField] private Text _level;
[SerializeField] private Text _price;
[SerializeField] private Text _message;
[Header("ADS View")] [SerializeField] private Sprite _adsBackground;
[SerializeField] private Sprite _adsIcon;
[Header("Default View")] [SerializeField] private Sprite _defaultBackground;
[SerializeField] private Sprite _defaultIcon;
[Header("VFX")] [SerializeField] private ParticleSystem _buyVFXPrefab;
private enum EnumBuffBuying { COINS, ADS, ALL }
private Buff Buff { get; set; }
private Action BuyCallback { get; set; }
private EnumBuffBuying BuyingMode { get; }
private Button _button;
private ParticleSystem _buyVFX;
private Image _background;
private Coroutine _buyRoutine;
private UIShiny _shine;
private Animator _animator;
private void Awake()
{
_button = GetComponent<Button>();
_button.onClick.AddListener(TryBuy);
_background = GetComponent<Image>();
_shine = GetComponent<UIShiny>();
_animator = GetComponent<Animator>();
}
private void Start()
{
_title.text = Buff.Title;
UpdateView();
}
private void OnEnable()
{
Buff = GetBuff();
}
private void Update()
{
UpdateView();
}
private Buff GetBuff()
{
if (!GameSettings.Buffs.TryGet(_buffType, out Buff targetBuff))
throw new NullReferenceException($"Cant get buff by type {_buffType}");
return targetBuff;
}
private void TryBuy()
{
if (_buyRoutine != null)
return;
_buyRoutine = StartCoroutine(Buy());
}
private IEnumerator Buy()
{
if (!Buff.CanUpgrade())
yield return null;
int currentCoins = (int) Save.Inventory.GetConsumableItemAmount(EnumConsumable.COIN);
int nextLevelNumber = Buff.GetCurrentLevel() + 1;
bool enoughMoney = currentCoins >= Buff.GetLevelInfo(nextLevelNumber).price;
if (enoughMoney && CanBuyForCoins())
{
BuyBuffLevel();
}
else if (Ads.HasRewardVideo() && CanBuyForAds())
{
ShowRewardedVideo();
}
yield return new WaitForSecondsRealtime(0.1f);
_buyRoutine = null;
}
private void WasteMoney(uint value)
{
Save.Inventory.RemoveConsumableItem(EnumConsumable.COIN, value);
UI.GetOrCreate<CoinsCounterWindow>().SyncCoinsCounter();
}
private void PlayBuyVFX()
{
if (_buyVFX == null && _buyVFXPrefab != null)
_buyVFX = Instantiate(_buyVFXPrefab, transform);
if (_buyVFX != null)
_buyVFX.Play();
//Sound.Play("Purchase");
}
private void ShowRewardedVideo()
{
Ads.ShowReward(
"buff",
BuyBuffLevel,
() => { });
}
private void BuyBuffLevel()
{
BuyCallback?.Invoke();
int nextLevelNumber = Buff.GetCurrentLevel() + 1;
uint price = Buff.GetLevelInfo(nextLevelNumber).price;
WasteMoney(price);
Save.Buffs.UpgradeLevel(Buff.Type);
ProductStatistics.ResourcesEvent(EnumProductResourcesFlow.SINK, "gold", price, "buff",
Buff.Type.ToString());
UpdateView();
PlayBuyVFX();
}
private void UpdateView()
{
if (!Buff.CanUpgrade())
{
MaxView(LocalizationManager.GetTranslation("Max"));
return;
}
int currentCoins = (int) Save.Inventory.GetConsumableItemAmount(EnumConsumable.COIN);
bool hasGoldForBuff = currentCoins >= Buff.GetLevelInfo(Buff.GetCurrentLevel() + 1).price;
if (hasGoldForBuff && CanBuyForCoins())
DefaultView();
else if (Ads.HasRewardVideo() && CanBuyForAds())
AdsView(GetFreeText());
else
NotEnoughView();
}
private bool CanBuyForCoins()
{
return BuyingMode == EnumBuffBuying.ALL || BuyingMode == EnumBuffBuying.COINS;
}
private bool CanBuyForAds()
{
return BuyingMode == EnumBuffBuying.ALL || BuyingMode == EnumBuffBuying.ADS;
}
private void DefaultView()
{
int nextLevel = Buff.GetCurrentLevel() + 1;
uint nextLevelPrice = Buff.GetLevelInfo(nextLevel).price;
_background.sprite = _defaultBackground;
_icon.sprite = _defaultIcon;
_buffIcon.sprite = Buff.Icon;
_level.text = $"{nextLevel}";
_price.text = $"{nextLevelPrice}";
_price.color = Color.white;
if (_shine.effectPlayer.play == false)
_shine.Play(reset: true);
_price.gameObject.SetActive(true);
_icon.gameObject.SetActive(true);
_buffIcon.gameObject.SetActive(_buffIcon.sprite != null);
_button.interactable = true;
}
private void AdsView(string message)
{
int nextLevel = Buff.GetCurrentLevel() + 1;
_background.sprite = _adsBackground;
_icon.sprite = _adsIcon;
_buffIcon.sprite = Buff.Icon;
_level.text = $"{nextLevel}";
_price.text = $"{message}";
_price.color = Color.white;
_shine.Stop(reset: true);
_price.gameObject.SetActive(true);
_icon.gameObject.SetActive(true);
_buffIcon.gameObject.SetActive(_buffIcon.sprite != null);
_button.interactable = true;
}
private void MaxView(string message)
{
_background.sprite = _defaultBackground;
_buffIcon.sprite = Buff.Icon;
_level.text = $"{Buff.GetCurrentLevel()}";
_message.text = message;
_shine.Stop(reset: true);
_price.gameObject.SetActive(false);
_icon.gameObject.SetActive(false);
_buffIcon.gameObject.SetActive(_buffIcon.sprite != null);
_button.interactable = false;
}
private void NotEnoughView()
{
int nextLevel = Buff.GetCurrentLevel() + 1;
uint nextLevelPrice = Buff.GetLevelInfo(nextLevel).price;
_background.sprite = _defaultBackground;
_icon.sprite = _defaultIcon;
_buffIcon.sprite = Buff.Icon;
_level.text = $"{nextLevel}";
_price.text = $"{nextLevelPrice}";
_price.color = Color.red;
_shine.Stop(reset: true);
_price.gameObject.SetActive(true);
_icon.gameObject.SetActive(true);
_buffIcon.gameObject.SetActive(_buffIcon.sprite != null);
_button.interactable = true;
}
private void SetDefaultAnimation()
{
_animator.SetBool("Pulsing", false);
_shine.Stop(false);
}
private void SetSpecialAnimation()
{
_animator.SetBool("Pulsing", true);
_shine.Play(false);
}
private string GetFreeText()
{
return LocalizationManager.GetTranslation("Free");
}
}
}