Pro_Unit

HeroAnimator

Jan 30th, 2021
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using CodeBase.Logic;
  5. using UnityEngine;
  6.  
  7. namespace CodeBase.Hero
  8. {
  9.     public class HeroAnimator : MonoBehaviour, IAnimationStateReader
  10.     {
  11.         private static readonly int MoveHash = Animator.StringToHash("Walking");
  12.         private static readonly int AttackHash = Animator.StringToHash("AttackNormal");
  13.         private static readonly int HitHash = Animator.StringToHash("Hit");
  14.         private static readonly int DieHash = Animator.StringToHash("Die");
  15.  
  16.         private readonly int _idleStateHash = Animator.StringToHash("Idle");
  17.         private readonly int _idleStateFullHash = Animator.StringToHash("Base Layer.Idle");
  18.         private readonly int _attackStateHash = Animator.StringToHash("Attack Normal");
  19.         private readonly int _walkingStateHash = Animator.StringToHash("Run");
  20.         private readonly int _deathStateHash = Animator.StringToHash("Die");
  21.         private Dictionary<int, AnimatorState> _stateForHash = new Dictionary<int, AnimatorState>();
  22.  
  23.         public event Action<AnimatorState> StateEntered;
  24.         public event Action<AnimatorState> StateExited;
  25.  
  26.         public AnimatorState State { get; private set; }
  27.  
  28.         public Animator Animator;
  29.         [SerializeField] private StateHash[] _states;
  30.  
  31.         public bool IsAttacking => State == AnimatorState.Attack;
  32.  
  33.         private void OnValidate()
  34.         {
  35.             foreach (StateHash state in _states) state.OnValidate();
  36.         }
  37.  
  38.         private void Awake()
  39.         {
  40.             _stateForHash = _states.ToDictionary(hash => hash.Hash, hash => hash.State);
  41.         }
  42.  
  43.         public void PlayHit() => Animator.SetTrigger(HitHash);
  44.  
  45.         public void PlayAttack() => Animator.SetTrigger(AttackHash);
  46.  
  47.         public void PlayDeath() => Animator.SetTrigger(DieHash);
  48.  
  49.         public void ResetToIdle() => Animator.Play(_idleStateHash, -1);
  50.  
  51.         public void EnteredState(int stateHash)
  52.         {
  53.             State = StateFor(stateHash);
  54.             StateEntered?.Invoke(State);
  55.         }
  56.  
  57.         public void ExitedState(int stateHash) =>
  58.             StateExited?.Invoke(StateFor(stateHash));
  59.  
  60.         private AnimatorState StateFor(int stateHash)
  61.         {
  62.             // AnimatorState state ;
  63.             // if (stateHash == _idleStateHash)
  64.             //     state = AnimatorState.Idle;
  65.             // else if (stateHash == _attackStateHash)
  66.             //     state = AnimatorState.Attack;
  67.             // else if (stateHash == _walkingStateHash)
  68.             //     state = AnimatorState.Walking;
  69.             // else if (stateHash == _deathStateHash)
  70.             //     state = AnimatorState.Died;
  71.             // else
  72.             //     state = AnimatorState.Unknown;
  73.  
  74.             _stateForHash.TryGetValue(stateHash, out AnimatorState state);
  75.  
  76.             return state;
  77.         }
  78.  
  79.         [Serializable]
  80.         private class StateHash
  81.         {
  82.             [HideInInspector] [SerializeField] private string _name;
  83.  
  84.             public void OnValidate()
  85.             {
  86.                 if (_hash)
  87.                     _name = _hash.name;
  88.             }
  89.  
  90.             [SerializeField] private AnimatorHash _hash;
  91.             [SerializeField] private AnimatorState _state;
  92.  
  93.             public int Hash => _hash;
  94.             public AnimatorState State => _state;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment