49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using AYellowpaper.SerializedCollections;
|
|
using BITFALL.Entities.Equipment;
|
|
using BITKit.Animations;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Entities.Animation
|
|
{
|
|
public sealed class EntitiesAnimationController : EntityComponent
|
|
{
|
|
[SerializeField] private UnityAnimator unityAnimator;
|
|
[SerializeField] private SerializedDictionary<string, RuntimeAnimatorController> animatorControllers;
|
|
[Inject]
|
|
private IEntityEquipment _entityEquipment;
|
|
|
|
private RuntimeAnimatorController _initialRuntimeAnimatorController;
|
|
|
|
private readonly DoubleBuffer<RuntimeAnimatorController> _runtimeAnimatorControllerBuffer = new();
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
_entityEquipment.OnEquip += OnEquip;
|
|
_initialRuntimeAnimatorController = unityAnimator.animator.runtimeAnimatorController;
|
|
}
|
|
|
|
public override void OnLateUpdate(float deltaTime)
|
|
{
|
|
if(_runtimeAnimatorControllerBuffer.TryGetRelease(out var controller))
|
|
{
|
|
unityAnimator.animator.runtimeAnimatorController = controller;
|
|
}
|
|
}
|
|
|
|
private void OnEquip(IBasicItem obj)
|
|
{
|
|
if(animatorControllers.TryGetValue(obj.AddressablePath, out var controller))
|
|
{
|
|
_runtimeAnimatorControllerBuffer.Release(controller);
|
|
}
|
|
else
|
|
{
|
|
_runtimeAnimatorControllerBuffer.Release(_initialRuntimeAnimatorController);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|