using Animancer; using BrewMonster; using System; using System.Collections.Generic; using UnityEngine; public class PlayerVisual : MonoBehaviour { [SerializeField] NamedAnimancerComponent namedAnimancer; [SerializeField] private INFO _playerInfo; [SerializeField] private AnimancerState _currentState; private readonly Queue _animationQueue = new Queue(); private void PlayActionEventHandler(PlayActionEvent @event) { _currentState = namedAnimancer.TryPlay(@event.AnimationName); if(_currentState == null) { BMLogger.LogError("HoangDev: PlayActionEventHandler Failed "); } } public void InitHostPlayerEventDoneHandler() { namedAnimancer = GetComponentInChildren(); if(namedAnimancer == null) { BrewMonster.BMLogger.LogError("animancer == null"); return; } var player = GetComponentInParent(); if(player == null) { BrewMonster.BMLogger.LogError("player == null"); return; } _playerInfo = player.GetPlayInfo(); EventBus.SubscribeChannel(_playerInfo.cid, PlayActionEventHandler); EventBus.SubscribeChannel(_playerInfo.cid, QueueActionEventHandler); } private void QueueActionEventHandler(QueueActionEvent @event) { if(!EnqueueAnimation(@event.AnimationName)) { BMLogger.LogError("HoangDev : EnqueueAnimation Failed"); } } public bool EnqueueAnimation(string animName) { if (namedAnimancer == null) return false; _animationQueue.Enqueue(animName); if (!namedAnimancer.IsPlaying()) PlayNext(); else { if (_currentState == null) return false; _currentState.Events.OnEnd = PlayNext; } return true; } private void PlayNext() { if (_animationQueue.Count == 0) { return; } string animName = _animationQueue.Dequeue(); var state = namedAnimancer.TryPlay(animName); // Khi clip kết thúc thì gọi tiếp cái kế tiếp state.Events.OnEnd = PlayNext; } private void OnDestroy() { EventBus.UnsubscribeAllInChannel(_playerInfo.cid); } public bool IsAnimationExist(string animationName) { return namedAnimancer.States.TryGet("ActionName", out var existingState) ? true : false; } }