208 lines
8.2 KiB
C#
208 lines
8.2 KiB
C#
using System;
|
|
using Animancer;
|
|
using BrewMonster;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public struct AnimationQueue
|
|
{
|
|
public string AnimationName;
|
|
public bool IsForceStopPrevious;
|
|
}
|
|
public class PlayerVisual : MonoBehaviour
|
|
{
|
|
[SerializeField] NamedAnimancerComponent namedAnimancer;
|
|
|
|
[SerializeField] private INFO _playerInfo;
|
|
private Dictionary<string, AnimancerState> _activeStates = new();
|
|
[SerializeField] private AnimancerState _currentState;
|
|
[SerializeField] private Queue<AnimationQueue> _animationQueue = new Queue<AnimationQueue>();
|
|
[SerializeField] private List<string> _animationList = new List<string>();
|
|
[SerializeField] private bool isHit;
|
|
[SerializeField] private int id;
|
|
[SerializeField] private bool isDebug;
|
|
|
|
private const float FadeTime = 100;
|
|
private const FadeMode FadeMode = Animancer.FadeMode.FixedDuration;
|
|
QueueActionEvent queueActionEvent;
|
|
|
|
private void PlayActionEventHandler(PlayActionEvent @event)
|
|
{
|
|
//when this trigger, clear all the animation in the queue which in the same layer of animancer
|
|
if (_animationQueue.Count > 0)
|
|
{
|
|
_animationQueue.Enqueue(new AnimationQueue { AnimationName = @event.AnimationName, IsForceStopPrevious = @event.IsForceStopPrevious });
|
|
_animationList = _animationQueue.Select(q => q.AnimationName).ToList();
|
|
return;
|
|
}
|
|
InternalPlayAnimation(@event.AnimationName, @event.ITransTime);
|
|
}
|
|
public void InitPlayerEventDoneHandler()
|
|
{
|
|
namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
|
|
if (namedAnimancer == null)
|
|
{
|
|
BrewMonster.BMLogger.LogWarning("InitPlayerEventDoneHandler animancer == null");
|
|
return;
|
|
}
|
|
var player = GetComponentInParent<CECPlayer>();
|
|
if (player == null)
|
|
{
|
|
BMLogger.LogWarning("player == null");
|
|
return;
|
|
}
|
|
|
|
_playerInfo = player.GetPlayInfo();
|
|
id = _playerInfo.cid;
|
|
EventBus.SubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
|
|
EventBus.SubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
|
|
EventBus.SubscribeChannel<ClearComActFlagAllRankNodesEvent>(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler);
|
|
}
|
|
|
|
// public void InitElsePlayerEventDoneHandler(INFO playerInfo)
|
|
// {
|
|
// namedAnimancer = GetComponentInChildren<NamedAnimancerComponent>();
|
|
// if (namedAnimancer == null)
|
|
// {
|
|
// BrewMonster.BMLogger.LogError("animancer == null");
|
|
// return;
|
|
// }
|
|
// //var player = GetComponentInParent<CECPlayer>();
|
|
// //if (player == null)
|
|
// //{
|
|
// // BrewMonster.BMLogger.LogError("player == null");
|
|
// // return;
|
|
// //}
|
|
// _playerInfo = playerInfo;//player.GetPlayInfo();
|
|
// EventBus.SubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
|
|
// EventBus.SubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
|
|
// EventBus.SubscribeChannel<CleearComActFlagAllRankNodesEvent>(_playerInfo.cid, CleearComActFlagAllRankNodesEventHandler);
|
|
// }
|
|
|
|
private void ClearComActFlagAllRankNodesEventHandler(ClearComActFlagAllRankNodesEvent @event)
|
|
{
|
|
_animationQueue.Clear();
|
|
_animationList = _animationQueue.Select(q => q.AnimationName).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(new AnimationQueue { AnimationName = @event.AnimationName, IsForceStopPrevious = @event.IsForceStopPrevious });
|
|
_animationList = _animationQueue.Select(q => q.AnimationName).ToList();
|
|
|
|
if (!isHit)
|
|
{
|
|
queueActionEvent = @event;
|
|
isHit = @event.IsHitAnim;
|
|
}
|
|
return true;
|
|
}
|
|
private void PlayNext()
|
|
{
|
|
if (_animationQueue.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// if (_currentState == null)
|
|
// {
|
|
// _animationQueue.Dequeue();
|
|
// return;
|
|
// }
|
|
//peek next if IsForceStopPrevious is true, force end
|
|
if (_animationQueue.Peek().IsForceStopPrevious)
|
|
{
|
|
_currentState.Stop();
|
|
_currentState = null;
|
|
}
|
|
if (_currentState != null && _currentState.NormalizedTime < 1f) return;
|
|
if (isHit)// have it relative to check _currentState == null?
|
|
{
|
|
ApplyDamage();
|
|
}
|
|
var animationQueue = _animationQueue.Dequeue();
|
|
_animationList = _animationQueue.Select(q => q.AnimationName).ToList();
|
|
InternalPlayAnimation(animationQueue.AnimationName);
|
|
}
|
|
void ApplyDamage()
|
|
{
|
|
if (queueActionEvent == null) return;
|
|
isHit = false;
|
|
queueActionEvent.SetFlag(true, queueActionEvent.AttackEvent);
|
|
queueActionEvent = null;
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
EventBus.UnsubscribeChannel<PlayActionEvent>(_playerInfo.cid, PlayActionEventHandler);
|
|
EventBus.UnsubscribeChannelClass<QueueActionEvent>(_playerInfo.cid, QueueActionEventHandler);
|
|
EventBus.UnsubscribeChannel<ClearComActFlagAllRankNodesEvent>(_playerInfo.cid, ClearComActFlagAllRankNodesEventHandler);
|
|
// EventBus.UnsubscribeAllInChannel(_playerInfo.cid);
|
|
}
|
|
public bool IsAnimationExist(string animationName)
|
|
{
|
|
return namedAnimancer.States.TryGet("ActionName", out var existingState) ? true : false;
|
|
}
|
|
|
|
private string _currentAnimationName;
|
|
/// <summary>
|
|
/// play an animation with name
|
|
/// </summary>
|
|
/// <param name="animationName"></param>
|
|
/// <param name="duration"></param>
|
|
/// <param name="fadeMode"></param>
|
|
private void InternalPlayAnimation(string animationName, float duration = FadeTime, FadeMode fadeMode = FadeMode)
|
|
{
|
|
|
|
string fullName = animationName;
|
|
string removeShapeName = animationName;
|
|
if(animationName.Contains("_"))
|
|
{
|
|
int underscoreIndex = animationName.IndexOf('_');
|
|
|
|
removeShapeName = animationName.Substring(underscoreIndex + 1);
|
|
}
|
|
if (isDebug)
|
|
{
|
|
BMLogger.LogError($"InternalPlayAnimation: original={animationName}, removeShapeName={removeShapeName}");
|
|
}
|
|
|
|
|
|
bool isState = namedAnimancer.States.TryGet(removeShapeName, out var existingState) ? true : false;
|
|
if (isState)
|
|
{
|
|
_currentState = namedAnimancer.TryPlay(removeShapeName, duration / 1000, fadeMode);
|
|
_currentAnimationName = removeShapeName;
|
|
return;
|
|
}
|
|
bool isState2 = namedAnimancer.States.TryGet(fullName, out var existingState2) ? true : false;
|
|
if (isState2)
|
|
{
|
|
_currentState = namedAnimancer.TryPlay(fullName, duration / 1000, fadeMode);
|
|
_currentAnimationName = fullName;
|
|
return;
|
|
}
|
|
BMLogger.LogError($"Null name animation: {fullName}");
|
|
}
|
|
}
|
|
} |