2025-12-09 01:04:14 +08:00

112 lines
3.3 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 TH1_Renderer.UnitAtomAnim;
using UnityEngine;
namespace TH1_Anim.UnitAtomAnim
{
public class UnitAnimManager
{
// Start is called before the first frame update
private Queue<IUnitAtomAnim> _animQueue = new Queue<IUnitAtomAnim>();
private bool _isBusy;
private IUnitAtomAnim _currentAnim;
public UnitAnimManager()
{
}
public void Init()
{
// 在这里可以进行资源加载、UI逻辑类的实例化等操作
// 例如:
// _wonderCompletedUI = new WonderCompletedAnnouncementUI(...);
//Debug.Log("PresentationManager 已初始化。");
}
public void EnqueueAnim(UnitAtomAnimType type,IUnitAtomAnimData data)
{
var anim = UnitAtomAnimFactory.Create(type,data);
if (anim == null)
{
Debug.LogError("试图添加一个空的动画!");
return;
}
_animQueue.Enqueue(anim);
// 尝试执行下一个任务
TryProcessNext();
}
private void TryProcessNext()
{
// 如果当前正在忙,
if (_isBusy)return;
//队列为空,则不执行
if (_animQueue.Count == 0)
{
_currentAnim = null;
return;
}
// 标记为忙碌状态
_isBusy = true;
// 从队列中取出下一个任务
_currentAnim = _animQueue.Dequeue();
}
private void OnAnimCompleted()
{
// 解除忙碌状态
_isBusy = false;
// 立即尝试处理队列中的下一个任务
TryProcessNext();
}
public void Update(UnitMono unitMono)
{
//Step #1 如果没有动画要播放return
if (_currentAnim == null) return;
switch (_currentAnim.State)
{
case UnitAtomAnimState.Wrong:
_isBusy = false;
TryProcessNext();
break;
case UnitAtomAnimState.Finished:
OnAnimCompleted();
break;
case UnitAtomAnimState.Prepare:
if (!_currentAnim.OnStart(unitMono))
{
_currentAnim.State = UnitAtomAnimState.Wrong;
break;
}
//Debug.Log($"Prepare -> Playing {Time.time}");
_currentAnim.StartTime = Time.time;
_currentAnim.State = UnitAtomAnimState.Playing;
break;
case UnitAtomAnimState.Playing:
_currentAnim.OnUpdate(unitMono,Time.time - _currentAnim.StartTime);
if (Time.time >= _currentAnim.StartTime + _currentAnim.Duration)
{
_currentAnim.OnFinished(unitMono);
_currentAnim.State = UnitAtomAnimState.Finished;
}
break;
}
}
}
}