You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.4 KiB
C#

using Fusion;
using UnityEngine;
using DG.Tweening;
using System.Collections;
using Sirenix.OdinInspector;
[HideMonoScript]
public class AnimationController : MonoBehaviour
{
[Title("ANIMATION CONTROLLER", titleAlignment: TitleAlignments.Centered)]
//===================================================
// FIELDS
//===================================================
[DisableInPlayMode]
[SerializeField] Animator _animator = null;
[DisableInPlayMode]
[SerializeField] int _attackLayerIndex = 2;
[DisableInPlayMode]
[SerializeField] int _attackCount = 3;
[SerializeField] NetworkMecanimAnimator _networkMecanimAnimator = null;
//===================================================
// PRIVATE FIELDS
//===================================================
private float _animationBlend = 0f;
private bool _canAttack = true;
private float _attackWeight = 0f;
private float _attackIdle = 0f;
private int _attackType = 0;
// Animation IDs
private int _animIDIdle = 0;
private int _animIDSpeed = 0;
private int _animIDGrounded = 0;
// private int _animIDJumpStart = 0;
private int _animIDJump = 0;
private int _animIDFreeFall = 0;
private int _animIDAttack = 0;
private int _animIDAttackType = 0;
private int _attackMirror = 0;
private IEnumerator _attackRoutine = null;
//===================================================
// METHODS
//===================================================
private void Awake() => Init();
private void Init()
{
if(_animator == null)
_animator = GetComponentInChildren<Animator>();
_attackRoutine = AttackIdle();
_animIDIdle = Animator.StringToHash("Idle");
_animIDSpeed = Animator.StringToHash("Speed");
_animIDGrounded = Animator.StringToHash("Grounded");
_animIDJump = Animator.StringToHash("Jump");
_animIDFreeFall = Animator.StringToHash("FreeFall");
_animIDAttack = Animator.StringToHash("Attack");
_animIDAttackType = Animator.StringToHash("AttackType");
_attackMirror = Animator.StringToHash("AttackMirror");
_animator.SetBool(_animIDGrounded, true);
}//Init() end
public void Animate(bool grounded, float speed, float deltaTime)
{
if(_animator == null)
return;
_animator.SetLayerWeight(_attackLayerIndex, _attackWeight);
_animator.SetFloat(_animIDIdle, _attackIdle);
_animator.SetBool(_animIDGrounded, grounded);
_animator.SetBool(_animIDFreeFall, !grounded);
_animationBlend = Mathf.Lerp(_animationBlend, speed, deltaTime * 10f);
if (_animationBlend < 0.01f)
_animationBlend = 0f;
if(_animationBlend > 0.01f)
_attackIdle = 0f;
_animator.SetFloat(_animIDSpeed, _animationBlend);
}//Animate() end
public void Jump()
{
if(_networkMecanimAnimator)
_networkMecanimAnimator.SetTrigger(_animIDJump, true);
else
_animator.SetTrigger(_animIDJump);
}//Jump() end
public async void Attack()
{
if (_canAttack is false)
return;
StopCoroutine(_attackRoutine);
DOTween.Kill(_attackWeight);
_attackRoutine = AttackIdle();
StartCoroutine(_attackRoutine);
_canAttack = false;
// _attackType = _attackType >= _attackCount ? 0 : _attackType;
if(_attackType >= _attackCount)
{
_animator.SetBool(_attackMirror, !_animator.GetBool(_attackMirror));
_attackType = 0;
}//if end
_animator.SetInteger(_animIDAttackType, _attackType);
if(_networkMecanimAnimator)
_networkMecanimAnimator.SetTrigger(_animIDAttack, true);
else
_animator.SetTrigger(_animIDAttack);
DOTween.To(()=> _attackWeight, x=> _attackWeight = x, 1f, 0.5f);
if(!await Tasks.Delay(0.8f)) return;
DOTween.To(()=> _attackWeight, x=> _attackWeight = x, 0f, 0.5f);
_attackType++;
_canAttack = true;
}//Attack() end
private IEnumerator AttackIdle()
{
DOTween.Kill(_attackIdle);
DOTween.To(()=> _attackIdle, x=> _attackIdle = x, 1f, 0.5f);
yield return new WaitForSeconds(5.0f);
DOTween.To(()=> _attackIdle, x=> _attackIdle = x, 0f, 0.5f);
}//Coroutine() end
}//class end