148 lines
5.3 KiB
C#
148 lines
5.3 KiB
C#
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<string> _animationQueue = new Queue<string>();
|
|
[SerializeField] private AnimancerState _currentState;
|
|
private const float fadeTime = .2f;
|
|
private const FadeMode FadeMode = Animancer.FadeMode.FixedDuration;
|
|
|
|
public CECNPC.INFO GetNPCINFO => m_NPCInfo;
|
|
public bool TryPlayAction(string animationName, CECAttackEvent cECAttackEvent, bool isHit = false, bool bRestart = true)
|
|
{
|
|
BMLogger.LogMono(this, "HoangDev: TryPlayAction: " + animationName);
|
|
|
|
if (namedAnimancer == null) return false;
|
|
BMLogger.LogMono(this, "HoangDev: namedAnimancer == null: " + animationName);
|
|
|
|
if (namedAnimancer.IsPlaying(animationName)) return false;
|
|
BMLogger.LogMono(this, "HoangDev: namedAnimancerIsPlaying == null1: " + animationName);
|
|
_currentState = namedAnimancer.TryPlay(animationName, fadeTime);
|
|
if (isHit)
|
|
{
|
|
_currentState.Events.OnEnd = () => SetHitOnEnd(cECAttackEvent);
|
|
}
|
|
if (_currentState != null)
|
|
BMLogger.LogMono(this, "HoangDev: _currentState != null1: " + _currentState.Clip.name);
|
|
return _currentState != null;
|
|
}
|
|
private void SetHitOnEnd(CECAttackEvent cECAttackEvent)
|
|
{
|
|
cECAttackEvent.m_bSignaled = true;
|
|
}
|
|
public void InitNPCEventDoneHandler(CECNPC.INFO iNFO)
|
|
{
|
|
m_NPCInfo = iNFO;
|
|
namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
|
|
if (namedAnimancer == null)
|
|
{
|
|
BMLogger.LogWarning("animancer == null");
|
|
return;
|
|
}
|
|
EventBus.SubscribeChannel<QueueNPCActionEvent>(m_NPCInfo.nid, OnQueueAction);
|
|
EventBus.SubscribeChannel<ClearComActFlagEvent>(m_NPCInfo.nid, OnClearComActFlagEvent);
|
|
}
|
|
private void OnClearComActFlagEvent(ClearComActFlagEvent @event)
|
|
{
|
|
if (_currentState != null)
|
|
BMLogger.LogError("HoangDev: OnClearComActFlagEvent _currentState:" + _currentState.Clip.name);
|
|
|
|
foreach (var state in _animationQueue)
|
|
{
|
|
BMLogger.LogMono(this,"HoangDev: OnClearComActFlagEvent state:" + state);
|
|
}
|
|
BMLogger.LogMono(this,"HoangDev: OnClearComActFlagEvent");
|
|
_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);
|
|
|
|
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();
|
|
BMLogger.LogMono(this,"HoangDev: PlayNext: " + animName);
|
|
_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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refresh the namedAnimancer reference when the model changes (e.g., shape change)
|
|
/// 当模型更改时(例如形状更改)刷新namedAnimancer引用
|
|
/// </summary>
|
|
/// <param name="modelRoot">The root GameObject of the model to search for NamedAnimancerComponent / 要搜索NamedAnimancerComponent的模型根GameObject</param>
|
|
public void RefreshNamedAnimancer(GameObject modelRoot = null)
|
|
{
|
|
if (modelRoot != null)
|
|
{
|
|
// Search specifically within the model GameObject's hierarchy
|
|
// 在模型GameObject的层次结构中搜索
|
|
namedAnimancer = modelRoot.GetComponentInChildren<NamedAnimancerComponent>();
|
|
}
|
|
else
|
|
{
|
|
// Fallback to searching from this component's hierarchy
|
|
// 回退到从此组件的层次结构搜索
|
|
namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
|
|
}
|
|
|
|
if (namedAnimancer == null)
|
|
{
|
|
BMLogger.LogWarning($"NPCVisual: RefreshNamedAnimancer - namedAnimancer == null after refresh (modelRoot: {modelRoot?.name ?? "null"})");
|
|
}
|
|
else
|
|
{
|
|
BMLogger.LogMono(this, $"NPCVisual: RefreshNamedAnimancer - Successfully refreshed namedAnimancer from model: {modelRoot?.name ?? "default"}");
|
|
}
|
|
}
|
|
}
|