Files
ft/Client/Assets/Scripts/EventPartnerGather/Mining/OreIncreaseController.cs
2026-06-29 21:18:33 +08:00

109 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
// 石头增长
namespace EventPartnerGather.Mining
{
public class OreIncreaseController : MonoBehaviour
{
// 周期
[SerializeField]
private float delayBetweenObjects = 0.1f; // 每个对象动画开始的间隔时间
[SerializeField]
private float popOutDuration = 0.05f; // 放大缩小动画的总时长
[SerializeField]
private float popOutScale = 1.2f; // 放大的目标倍数
// 设置Ore 列表
private readonly List<Transform> _ores = new ();
//
private void Awake()
{
foreach (Transform child in transform)
{
_ores.Add(child);
}
}
//
private void OnDestroy()
{
_ores.Clear();
}
// [0,1.0] float
public void JumpTo(float fillAmount)
{
var endValue = Mathf.CeilToInt(_ores.Count * fillAmount);
endValue = Mathf.Clamp(endValue, 0, _ores.Count - 1);
for (var i = 0; i < endValue; ++i)
{
_ores[i].gameObject.SetActive(true);
}
for (var i = endValue; i < _ores.Count; ++i)
{
_ores[i].gameObject.SetActive(false);
}
}
public async Task AsyncPlayFromTo(int startFrame, int endFrame)
{
// Debug.Log("AsyncPlayFromToStart");
var tcs = new TaskCompletionSource<bool>();
var sequence = DOTween.Sequence(); // 创建主序列
var startValue = Mathf.CeilToInt(startFrame * _ores.Count * 0.01F);
var endValue = Mathf.CeilToInt(endFrame * _ores.Count * 0.01F);
var count = endValue - startValue;
// Debug.Log($"AsyncPlayFromToCount count={count}");
for (var i = startValue; i < endValue; ++i)
{
var child = _ores[i];
sequence.Join(CreatePopOutSequence(child, i - startValue));
}
sequence.SetAutoKill(true);
sequence.OnComplete(() =>
{
// Debug.Log("sequence Finished");
tcs.TrySetResult(true);
});
sequence.Play(); // 播放整个序列
await tcs.Task;
}
private Sequence CreatePopOutSequence(Transform trans,int index )
{
var popSequence = DOTween.Sequence();
// 记录原始缩放值通常应该是Vector3.one
// delayBetweenObjects = 0.1F;
var originalScale = trans.localScale;
popSequence.AppendInterval(index * delayBetweenObjects);
// 设置激活
popSequence.AppendCallback(() => trans.gameObject.SetActive(true));
// 第一阶段:放大
popSequence.Append(trans.DOScale(originalScale * popOutScale, popOutDuration * 0.75f).SetEase(Ease.OutQuad));
// 第二阶段:缩小回原始大小
popSequence.Append(trans.DOScale(originalScale, popOutDuration * 0.25f).SetEase(Ease.OutQuad));
// 为这个放大缩小序列设置缓动类型,让动画更生动
popSequence.SetEase(Ease.Linear);
popSequence.OnComplete(() =>
{
// Debug.Log("CreatePopOutSequence Finished");
});
return popSequence;
}
public void Reset()
{
foreach (var ore in _ores)
{
ore.gameObject.SetActive(false);
}
}
}
}