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 _activeStates = new(); [SerializeField] private AnimancerState _currentState; [SerializeField] private Queue _animationQueue = new Queue(); [SerializeField] private List _animationList = new List(); [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); return; } InternalPlayAnimation(@event.AnimationName); } public void InitPlayerEventDoneHandler() { namedAnimancer = GetComponentInChildren(); if (namedAnimancer == null) { BrewMonster.BMLogger.LogWarning("InitPlayerEventDoneHandler animancer == null"); return; } var player = GetComponentInParent(); if (player == null) { BMLogger.LogWarning("player == null"); return; } _playerInfo = player.GetPlayInfo(); id = _playerInfo.cid; EventBus.SubscribeChannel(_playerInfo.cid, PlayActionEventHandler); EventBus.SubscribeChannelClass(_playerInfo.cid, QueueActionEventHandler); EventBus.SubscribeChannel(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler); } // public void InitElsePlayerEventDoneHandler(INFO playerInfo) // { // namedAnimancer = GetComponentInChildren(); // if (namedAnimancer == null) // { // BrewMonster.BMLogger.LogError("animancer == null"); // return; // } // //var player = GetComponentInParent(); // //if (player == null) // //{ // // BrewMonster.BMLogger.LogError("player == null"); // // return; // //} // _playerInfo = playerInfo;//player.GetPlayInfo(); // EventBus.SubscribeChannel(_playerInfo.cid, PlayActionEventHandler); // EventBus.SubscribeChannelClass(_playerInfo.cid, QueueActionEventHandler); // EventBus.SubscribeChannel(_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(_playerInfo.cid, PlayActionEventHandler); EventBus.UnsubscribeChannelClass(_playerInfo.cid, QueueActionEventHandler); EventBus.UnsubscribeChannel(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler); // EventBus.UnsubscribeAllInChannel(_playerInfo.cid); } public bool IsAnimationExist(string animationName) { return namedAnimancer.States.TryGet("ActionName", out var existingState) ? true : false; } /// /// play an animation with name /// /// /// /// private void InternalPlayAnimation(string animationName, float duration = FadeTime, FadeMode fadeMode = FadeMode) { _currentState = namedAnimancer.TryPlay(animationName, duration, fadeMode); if (_currentState == null) { BMLogger.LogError($"Null animation with name: {animationName}"); } } } }