67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Permissions;
|
|
using AYellowpaper.SerializedCollections;
|
|
using BITFALL.Entities;
|
|
using BITFALL.Entities.Equipment;
|
|
using BITKit.Animations;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Entities.Animation
|
|
{
|
|
public sealed class EntitiesAnimationController : EntityBehavior
|
|
{
|
|
[SerializeField] private UnityAnimator unityAnimator;
|
|
[SerializeField] private SerializedDictionary<string, RuntimeAnimatorController> animatorControllers;
|
|
[Inject]
|
|
private IEntityEquipment _entityEquipment;
|
|
[Inject(true)]
|
|
private IKnockdown _knockdown;
|
|
[Inject]
|
|
private IHealth _health;
|
|
|
|
private RuntimeAnimatorController _initialRuntimeAnimatorController;
|
|
|
|
private readonly DoubleBuffer<RuntimeAnimatorController> _runtimeAnimatorControllerBuffer = new();
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
_entityEquipment.OnEquipAddressable += OnEquip;
|
|
_entityEquipment.OnUnEquipAddressable += UnEquip;
|
|
_initialRuntimeAnimatorController = unityAnimator.animator.runtimeAnimatorController;
|
|
|
|
}
|
|
|
|
private void UnEquip(string obj)
|
|
{
|
|
if (_knockdown is not null && _knockdown.IsKnockdown) return;
|
|
if (_health.IsAlive is false) return;
|
|
if (animatorControllers.Values.Any(x => x == unityAnimator.animator.runtimeAnimatorController))
|
|
_runtimeAnimatorControllerBuffer.Release(_initialRuntimeAnimatorController);
|
|
}
|
|
|
|
public override void OnLateUpdate(float deltaTime)
|
|
{
|
|
if(_runtimeAnimatorControllerBuffer.TryGetRelease(out var controller))
|
|
{
|
|
if (controller is AnimatorOverrideController overrideController)
|
|
{
|
|
controller = overrideController.CopyAndFillMissingContent(_initialRuntimeAnimatorController as AnimatorOverrideController);
|
|
}
|
|
unityAnimator.animator.runtimeAnimatorController = controller;
|
|
unityAnimator.animator.Update(deltaTime);
|
|
}
|
|
}
|
|
|
|
private void OnEquip(string equipName)
|
|
{
|
|
if (_knockdown is not null && _knockdown.IsKnockdown) return;
|
|
_runtimeAnimatorControllerBuffer.Release(animatorControllers.TryGetValue(equipName, out var controller)
|
|
? controller
|
|
: _initialRuntimeAnimatorController);
|
|
}
|
|
}
|
|
|
|
}
|