using Animancer; using BrewMonster; using System; using System.Collections.Generic; using UnityEngine; using static CECModel; public class NPCVisual : MonoBehaviour { [SerializeField] NamedAnimancerComponent namedAnimancer; protected CECNPC.INFO m_NPCInfo; [SerializeField] private Queue _animationQueue = new Queue(); [SerializeField] private AnimancerState _currentState; public CECNPC.INFO GetNPCINFO => m_NPCInfo; #if UNITY_EDITOR [SerializeField] bool isDebug; public void DEBUG(string text) { if (!isDebug) return; BMLogger.LogError(text); } #endif public bool TryPlayAction(string animationName, CECAttackEvent cECAttackEvent, bool isHit = false, bool bRestart = true) { #if UNITY_EDITOR DEBUG("HoangDev: TryPlayAction: " + animationName); #endif if (namedAnimancer == null) return false; #if UNITY_EDITOR DEBUG("HoangDev: namedAnimancer == null: " + animationName); #endif if (namedAnimancer.IsPlaying(animationName)) return false; #if UNITY_EDITOR DEBUG("HoangDev: namedAnimancerIsPlaying == null1: " + animationName); #endif _currentState = namedAnimancer.TryPlay(animationName); if (isHit) { _currentState.Events.OnEnd = ()=>SetHitOnEnd(cECAttackEvent); } #if UNITY_EDITOR if (_currentState != null) DEBUG("HoangDev: _currentState != null1: " + _currentState.Clip.name); #endif return _currentState != null; } private void SetHitOnEnd(CECAttackEvent cECAttackEvent) { cECAttackEvent.m_bSignaled = true; } public void InitNPCEventDoneHandler(CECNPC.INFO iNFO) { m_NPCInfo = iNFO; namedAnimancer = GetComponentInChildren(); if (namedAnimancer == null) { BrewMonster.BMLogger.LogError("animancer == null"); return; } EventBus.SubscribeChannel(m_NPCInfo.nid, OnQueueAction); EventBus.SubscribeChannel(m_NPCInfo.nid, OnClearComActFlagEvent); } private void OnClearComActFlagEvent(ClearComActFlagEvent @event) { #if UNITY_EDITOR if (_currentState != null) DEBUG("HoangDev: OnClearComActFlagEvent _currentState:" + _currentState.Clip.name); #endif #if UNITY_EDITOR foreach (var state in _animationQueue) { DEBUG("HoangDev: OnClearComActFlagEvent state:" + state); } DEBUG("HoangDev: OnClearComActFlagEvent"); #endif _animationQueue.Clear(); } private void Update() { PlayNext(); } private void OnQueueAction(QueueNPCActionEvent @event) { if (!EnqueueAnimation(@event)) { BMLogger.LogError("HoangDev : EnqueueAnimation Failed"); } } public bool EnqueueAnimation(QueueNPCActionEvent @event) { if (namedAnimancer == null) return false; _animationQueue.Enqueue(@event.AnimationName); BMLogger.LogError("HoangDev: EnqueueAnimation: " + _animationQueue.Peek()); return true; } public string animName1; private void PlayNext() { if (_animationQueue.Count == 0) { return; } if (_currentState == null) return; animName1 = _animationQueue.Peek(); if (_currentState.NormalizedTime < 1f) return; string animName = _animationQueue.Dequeue(); #if UNITY_EDITOR DEBUG("HoangDev: PlayNext: " + animName); #endif _currentState = namedAnimancer.TryPlay(animName); } private void OnDestroy() { EventBus.UnsubscribeAllInChannel(m_NPCInfo.nid); } public bool IsAnimationExist(string animationName) { if (namedAnimancer == null) return false; return namedAnimancer.States.TryGet(animationName, out var existingState) ? true : false; } public bool IsPlayAnimation() { if (namedAnimancer == null) return false; return namedAnimancer.IsPlaying(); } public bool IsPlayAnimation(string animationName) { if (namedAnimancer == null) return false; return namedAnimancer.IsPlaying(animationName); } }