Files
test/Assets/Scripts/PlayerVisual.cs
T
2025-12-17 14:18:10 +07:00

171 lines
6.5 KiB
C#

using System;
using Animancer;
using BrewMonster;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace BrewMonster
{
public class PlayerVisual : MonoBehaviour
{
[SerializeField] NamedAnimancerComponent namedAnimancer;
[SerializeField] private INFO _playerInfo;
private Dictionary<string, AnimancerState> _activeStates = new();
[SerializeField] private AnimancerState _currentState;
[SerializeField] private Queue<string> _animationQueue = new Queue<string>();
[SerializeField] private List<string> _animationList = new List<string>();
[SerializeField] private bool isHit;
[SerializeField] private int id;
private const float FadeTime = 0.1f;
private const FadeMode FadeMode = Animancer.FadeMode.FixedDuration;
QueueActionEvent queueActionEvent;
private void PlayActionEventHandler(PlayActionEvent @event)
{
if (_animationQueue.Count > 0)
{
_animationQueue.Enqueue(@event.AnimationName);
_animationList = _animationQueue.ToList();
return;
}
InternalPlayAnimation(@event.AnimationName);
}
public void InitPlayerEventDoneHandler()
{
namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
if (namedAnimancer == null)
{
BrewMonster.BMLogger.LogWarning("InitPlayerEventDoneHandler animancer == null");
return;
}
var player = GetComponentInParent<CECPlayer>();
if (player == null)
{
BMLogger.LogWarning("player == null");
return;
}
_playerInfo = player.GetPlayInfo();
id = _playerInfo.cid;
EventBus.SubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
EventBus.SubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
EventBus.SubscribeChannel<ClearComActFlagAllRankNodesEvent>(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler);
}
// public void InitElsePlayerEventDoneHandler(INFO playerInfo)
// {
// namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
// if (namedAnimancer == null)
// {
// BrewMonster.BMLogger.LogError("animancer == null");
// return;
// }
// //var player = GetComponentInParent<CECPlayer>();
// //if (player == null)
// //{
// // BrewMonster.BMLogger.LogError("player == null");
// // return;
// //}
// _playerInfo = playerInfo;//player.GetPlayInfo();
// EventBus.SubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
// EventBus.SubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
// EventBus.SubscribeChannel<CleearComActFlagAllRankNodesEvent>(_playerInfo.cid, CleearComActFlagAllRankNodesEventHandler);
// }
private void ClearComActFlagAllRankNodesEventHandler(ClearComActFlagAllRankNodesEvent @event)
{
_animationQueue.Clear();
_animationList = _animationQueue.ToList();
if (isHit)
{
ApplyDamage();
}
//todo: this is dummy to force change to idle state
// EventBus.PublishChannel(_playerInfo.cid, new PlayActionEvent("站立_通用"));
}
private void QueueActionEventHandler(QueueActionEvent @event)
{
if (!EnqueueAnimation(@event))
{
BMLogger.LogError("HoangDev : EnqueueAnimation Failed");
}
}
private void Update()
{
PlayNext();
}
public bool EnqueueAnimation(QueueActionEvent @event)
{
if (namedAnimancer == null) return false;
_animationQueue.Enqueue(@event.AnimationName);
_animationList = _animationQueue.ToList();
if (!isHit)
{
queueActionEvent = @event;
isHit = @event.IsHitAnim;
}
return true;
}
private void PlayNext()
{
if (_animationQueue.Count == 0)
{
return;
}
// if (_currentState == null)
// {
// _animationQueue.Dequeue();
// return;
// }
if (_currentState != null && _currentState.NormalizedTime < 1f) return;
if (isHit)// have it relative to check _currentState == null?
{
ApplyDamage();
}
string animName = _animationQueue.Dequeue();
_animationList = _animationQueue.ToList();
InternalPlayAnimation(animName);
}
void ApplyDamage()
{
if (queueActionEvent == null) return;
isHit = false;
queueActionEvent.SetFlag(true, queueActionEvent.AttackEvent);
queueActionEvent = null;
}
private void OnDestroy()
{
EventBus.UnsubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
EventBus.UnsubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
EventBus.UnsubscribeChannel<ClearComActFlagAllRankNodesEvent>(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler);
// EventBus.UnsubscribeAllInChannel(_playerInfo.cid);
}
public bool IsAnimationExist(string animationName)
{
return namedAnimancer.States.TryGet("ActionName", out var existingState) ? true : false;
}
private string _currentAnimationName;
/// <summary>
/// play an animation with name
/// </summary>
/// <param name="animationName"></param>
/// <param name="duration"></param>
/// <param name="fadeMode"></param>
private void InternalPlayAnimation(string animationName, float duration = FadeTime, FadeMode fadeMode = FadeMode)
{
_currentState = namedAnimancer.TryPlay(animationName, duration, fadeMode);
_currentAnimationName = animationName;
if (_currentState == null)
{
BMLogger.LogError($"Null animation with name: {animationName}");
}
}
}
}