105 lines
3.0 KiB
C#
105 lines
3.0 KiB
C#
using Animancer;
|
|
using BrewMonster;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class NPCVisual : MonoBehaviour
|
|
{
|
|
[SerializeField] NamedAnimancerComponent namedAnimancer;
|
|
protected CECNPC.INFO m_NPCInfo;
|
|
[SerializeField] private Queue<string> _animationQueue = new Queue<string>();
|
|
[SerializeField] private bool pActFlag;
|
|
[SerializeField] private bool isHit;
|
|
[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)
|
|
{
|
|
DEBUG("HoangDev: TryPlayAction: " + animationName);
|
|
if (namedAnimancer == null) return false;
|
|
if (namedAnimancer.IsPlaying(animationName)) return false;
|
|
_currentState = namedAnimancer.TryPlay(animationName);
|
|
return _currentState == null;
|
|
}
|
|
|
|
public void InitNPCEventDoneHandler(CECNPC.INFO iNFO)
|
|
{
|
|
m_NPCInfo = iNFO;
|
|
namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
|
|
if (namedAnimancer == null)
|
|
{
|
|
BrewMonster.BMLogger.LogError("animancer == null");
|
|
return;
|
|
}
|
|
EventBus.SubscribeChannel<QueueActionEvent>(m_NPCInfo.nid, OnQueueAction);
|
|
}
|
|
private void Update()
|
|
{
|
|
PlayNext();
|
|
}
|
|
private void OnQueueAction(QueueActionEvent @event)
|
|
{
|
|
if (!EnqueueAnimation(@event))
|
|
{
|
|
BMLogger.LogError("HoangDev : EnqueueAnimation Failed");
|
|
}
|
|
}
|
|
public bool EnqueueAnimation(QueueActionEvent @event)
|
|
{
|
|
if (namedAnimancer == null) return false;
|
|
|
|
_animationQueue.Enqueue(@event.AnimationName);
|
|
pActFlag = @event.PActFlag;
|
|
isHit = @event.IsHitAnim;
|
|
return true;
|
|
}
|
|
private void PlayNext()
|
|
{
|
|
if (_animationQueue.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
if (_currentState == null) return;
|
|
if (_currentState.IsPlaying) return;
|
|
if (isHit)
|
|
{
|
|
SetpActFlagTrue();
|
|
isHit = false;
|
|
return;
|
|
}
|
|
string animName = _animationQueue.Dequeue();
|
|
_currentState = namedAnimancer.TryPlay(animName);
|
|
}
|
|
private void SetpActFlagTrue()
|
|
{
|
|
pActFlag = true;
|
|
}
|
|
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);
|
|
}
|
|
}
|