97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Animancer;
|
|
using AYellowpaper.SerializedCollections;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.Movement.MotionBased
|
|
{
|
|
public class MotionBasedBehavior : EntityBehavior
|
|
{
|
|
[SerializeField] private AnimancerComponent animancerComponent;
|
|
[SerializeField] private SerializedDictionary<string, AnimationClip[]> animationClips;
|
|
[SerializeField] private SerializedDictionary<string,int> layerIndex;
|
|
[SerializeField] private SerializedDictionary<int,bool> additiveIndex;
|
|
[SerializeField] private SerializedDictionary<int,AvatarMask> avatarMasks;
|
|
[SerializeField] private bool allowDeathAnimation;
|
|
|
|
[Inject] private IEntityOverride _override;
|
|
[Inject] private IHealth _health;
|
|
|
|
private string lastAnimationName;
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
_health.OnSetAlive += OnSetAlive;
|
|
UnityEntity.AddListener<string>(Constant.Animation.Play,Play);
|
|
|
|
animancerComponent.Layers[2].IsAdditive = true;
|
|
|
|
foreach (var pair in avatarMasks)
|
|
{
|
|
animancerComponent.Layers[pair.Key].SetMask(pair.Value);
|
|
}
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
{
|
|
if (obj)
|
|
{
|
|
//_override.RemoveOverride(-1);
|
|
animancerComponent.Layers[8].Stop();
|
|
}
|
|
else if( allowDeathAnimation)
|
|
{
|
|
Play("Death",false,8);
|
|
}else if (allowDeathAnimation is false)
|
|
{
|
|
if (string.IsNullOrEmpty(lastAnimationName) is false)
|
|
_override.RemoveOverride(lastAnimationName);
|
|
foreach (var x in animancerComponent.Layers)
|
|
{
|
|
x.Stop();
|
|
}
|
|
}
|
|
}
|
|
public void Play(string animationName)=>Play(animationName,false);
|
|
public void Play(string animationName, bool isAdditive,int layer = -1)
|
|
{
|
|
if (animationClips.TryGetValue(animationName, out var clip) is false) return;
|
|
|
|
layer = layer is -1 ? isAdditive ? 2 : 1 : layer;
|
|
if(layerIndex.TryGetValue(animationName,out var index))
|
|
layer = index;
|
|
isAdditive = additiveIndex.TryGetValue(layer, out var value) && value || isAdditive;
|
|
|
|
if (string.IsNullOrEmpty(lastAnimationName) is false)
|
|
{
|
|
_override.RemoveOverride(lastAnimationName);
|
|
}
|
|
|
|
if (isAdditive is false)
|
|
_override.AddOverride(animationName);
|
|
animancerComponent.Layers[layer].Stop();
|
|
|
|
var randomClip = clip.Random();
|
|
|
|
if (randomClip is null) return;
|
|
var state = animancerComponent.Layers[layer].Play(randomClip, 0.2f);
|
|
state.Events.OnEnd += RemoveOverride;
|
|
lastAnimationName = animationName;
|
|
return;
|
|
|
|
void RemoveOverride()
|
|
{
|
|
state.Events.OnEnd -= RemoveOverride;
|
|
animancerComponent.Layers[layer].Stop();
|
|
UnityEntity.Invoke(Constant.Animation.OnPlayEnd,animationName);
|
|
if (isAdditive is false)
|
|
_override.RemoveOverride(animationName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|