146 lines
4.2 KiB
C#
146 lines
4.2 KiB
C#
using System.Collections;
|
|
using game;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public enum SyncCoinsTextType { ALL, SESSION, SAVE }
|
|
|
|
public class CoinsCounterWindow : ParameterlessWindow
|
|
{
|
|
public Transform CoinFlyTarget => _coinCounter;
|
|
|
|
[Header("Prefabs")] [SerializeField] private Camera _coinCameraPrefab;
|
|
[SerializeField] private Text _coinsCount;
|
|
[SerializeField] private RectTransform _coinCounter;
|
|
|
|
private ScreenCoins _screenCoins;
|
|
private CoinCamera _coinView;
|
|
private Camera _coinCamera;
|
|
private Camera _gameCamera;
|
|
|
|
public override bool NeedShowBackground => false;
|
|
public override int ZOrder => -4;
|
|
|
|
private Coroutine _textEffect;
|
|
|
|
protected override void OnInit()
|
|
{
|
|
_gameCamera = CameraManager.Main;
|
|
_screenCoins = gameObject.AddComponent<ScreenCoins>();
|
|
}
|
|
|
|
protected override void OnShowed()
|
|
{
|
|
_coinCamera = Instantiate(_coinCameraPrefab, Vector3.left * 1000f, Quaternion.identity);
|
|
_coinView = _coinCamera.GetComponent<CoinCamera>();
|
|
_coinCounter.GetComponent<RawImage>().texture = _coinCamera.targetTexture;
|
|
|
|
StopTextScaleEffect();
|
|
}
|
|
|
|
protected override void OnHidden()
|
|
{
|
|
if (_coinCamera == null)
|
|
return;
|
|
|
|
Destroy(_coinCamera.gameObject);
|
|
}
|
|
|
|
|
|
public void SyncCoinsCounter(SyncCoinsTextType syncType = SyncCoinsTextType.ALL, bool skipAnimation = true)
|
|
{
|
|
int coinsCount = GetCoinsCount(syncType);
|
|
|
|
if (skipAnimation == false && gameObject.activeInHierarchy)
|
|
SetCoinsTextWithEffects(coinsCount);
|
|
else
|
|
SetCoinsText(coinsCount);
|
|
}
|
|
|
|
private int GetCoinsCount(SyncCoinsTextType syncType)
|
|
{
|
|
switch (syncType)
|
|
{
|
|
case SyncCoinsTextType.ALL:
|
|
return (int)Save.Inventory.GetConsumableItemAmount(EnumConsumable.COIN) + G.Instance.Session.EarnedCoins;
|
|
case SyncCoinsTextType.SESSION:
|
|
return G.Instance.Session.EarnedCoins;
|
|
case SyncCoinsTextType.SAVE:
|
|
return (int)Save.Inventory.GetConsumableItemAmount(EnumConsumable.COIN);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private void SetCoinsTextWithEffects(int coinsCount)
|
|
{
|
|
PlayTextScaleEffect();
|
|
SetTextWithEffect(coinsCount);
|
|
}
|
|
|
|
private void SetCoinsText(int value)
|
|
{
|
|
_coinsCount.text = $"{value}";
|
|
}
|
|
|
|
private void SetTextWithEffect(int value)
|
|
{
|
|
int startValue = int.Parse(_coinsCount.text);
|
|
StartCoroutine(Tween.DoTween(0.5f, t =>
|
|
{
|
|
var desiredValue = (int) Mathf.Lerp(startValue, value, t);
|
|
SetCoinsText(desiredValue);
|
|
}));
|
|
|
|
_coinView.RotateView();
|
|
}
|
|
|
|
private void PlayTextScaleEffect()
|
|
{
|
|
StopTextScaleEffect();
|
|
StartCoroutine(TextScaleEffect());
|
|
}
|
|
|
|
private void StopTextScaleEffect()
|
|
{
|
|
if (_textEffect != null)
|
|
StopCoroutine(_textEffect);
|
|
|
|
_textEffect = null;
|
|
}
|
|
|
|
private IEnumerator TextScaleEffect()
|
|
{
|
|
yield return Tween.DoTween(0.1f, t => { _coinsCount.fontSize += (int) (1.5f * t); });
|
|
yield return Tween.DoTween(0.1f, t => { _coinsCount.fontSize -= (int) (1.5f * t); });
|
|
}
|
|
|
|
// public Vector3 WorldToViewportPoint(Vector3 original)
|
|
// {
|
|
// return _gameCamera.WorldToViewportPoint(original);
|
|
// }
|
|
//
|
|
// public Vector3 ViewportToWorldPoint(Vector3 original)
|
|
// {
|
|
// return _gameCamera.ViewportToWorldPoint(original);
|
|
// }
|
|
//
|
|
// public Vector3 GetCounterViewportPosition(Vector3 original)
|
|
// {
|
|
// Vector3 viewPortPosition = _gameCamera.WorldToViewportPoint(_coinCounter.position);
|
|
// float targetZ = _gameCamera.transform.InverseTransformPoint(original).z;
|
|
//
|
|
// return _gameCamera.ViewportToWorldPoint(new Vector3(viewPortPosition.x, viewPortPosition.y, targetZ));
|
|
// }
|
|
|
|
public Coroutine ShowScreenCoins() => StartCoroutine(ShowScreenCoinsRoutine());
|
|
|
|
private IEnumerator ShowScreenCoinsRoutine()
|
|
{
|
|
_screenCoins.Init(15);
|
|
yield return new WaitForSeconds(1f);
|
|
StartCoroutine(_screenCoins.MoveCoins());
|
|
yield return new WaitForSeconds(2f);
|
|
}
|
|
}
|