116 lines
2.9 KiB
C#
116 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using OHM.UnityToolkit;
|
|
using OHM.UnityToolkit.Pooling;
|
|
using OHM.UnityToolkit.UI;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class UIInventoryCount : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
protected UIIncrementableCounter counter;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("poppingPrefab")]
|
|
private UIMoveToTarget _poppingPrefab;
|
|
|
|
[SerializeField]
|
|
private RectTransform TargetPos;
|
|
|
|
[SerializeField]
|
|
private Transform poppingCont;
|
|
|
|
[SerializeField]
|
|
private float minPoppinDT;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("maxPoppingGroupDuration")]
|
|
private float _maxPoppingGroupDuration;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("maxPoppingGroupDelta")]
|
|
private Vector3 _maxPoppingGroupDelta;
|
|
|
|
[SerializeField]
|
|
private int maxCount = int.MaxValue;
|
|
[SerializeField]
|
|
[FormerlySerializedAs("collectSFX")]
|
|
private SFX _collectSFX;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("collectDoneSFX")]
|
|
private SFX _collectDoneSFX;
|
|
|
|
protected AppController appController;
|
|
|
|
private float _nextPoppingCoinMinTime;
|
|
|
|
public virtual void Setup(AppController appController)
|
|
{
|
|
this.appController = appController;
|
|
}
|
|
|
|
protected void OnCollectAlign(int count, Transform from)
|
|
{
|
|
OnCollect(count, from, align: true);
|
|
}
|
|
|
|
protected void OnCollect(int count, Transform from, bool align = true)
|
|
{
|
|
if (!base.gameObject.activeInHierarchy)
|
|
{
|
|
return;
|
|
}
|
|
int popCount = Mathf.Min(count, maxCount);
|
|
float groupDT = 0f;
|
|
if (popCount >= 2)
|
|
{
|
|
groupDT = Mathf.Clamp(_maxPoppingGroupDuration / popCount, 0f, minPoppinDT);
|
|
}
|
|
Vector3 delta = Vector3.zero;
|
|
for (int i = 0; i < popCount; i++)
|
|
{
|
|
float delay = Mathf.Max(0f, _nextPoppingCoinMinTime - Time.time);
|
|
StartCoroutine(PopCoin(delay, from, delta, i == 0, align));
|
|
|
|
delta = new Vector3(
|
|
UnityEngine.Random.Range(0f - _maxPoppingGroupDelta.x, _maxPoppingGroupDelta.x),
|
|
UnityEngine.Random.Range(0f - _maxPoppingGroupDelta.y, _maxPoppingGroupDelta.y),
|
|
UnityEngine.Random.Range(0f - _maxPoppingGroupDelta.z, _maxPoppingGroupDelta.z));
|
|
_nextPoppingCoinMinTime = Time.time + delay + groupDT;
|
|
}
|
|
if (_collectSFX != null)
|
|
{
|
|
_collectSFX.Play(base.transform);
|
|
}
|
|
}
|
|
|
|
protected IEnumerator PopCoin(float delay, Transform from, Vector3 delta, bool doneSfx, bool align = true)
|
|
{
|
|
Vector3 fromPos = from.position;
|
|
yield return new WaitForSeconds(delay);
|
|
UIMoveToTarget popping = PoolsManager.InstantiatePrefab(_poppingPrefab, poppingCont,
|
|
poppingCont.position, poppingCont.rotation);
|
|
if (popping == null)
|
|
{
|
|
yield break;
|
|
}
|
|
popping.target = TargetPos;
|
|
|
|
fromPos += delta;
|
|
if (align)
|
|
{
|
|
UIUtils.AlignToWorldPosition(fromPos, popping.GetComponent<RectTransform>());
|
|
}
|
|
popping.gameObject.SetActive(true);
|
|
if (doneSfx && _collectDoneSFX != null)
|
|
{
|
|
popping.onMoveDone.AddListener(delegate
|
|
{
|
|
_collectDoneSFX.Play(base.transform);
|
|
});
|
|
}
|
|
}
|
|
}
|