Files
MobileShooter/Assets/Darkmatter/Code/Presentation/Animation/PlayerAnimController.cs
2025-12-30 17:51:20 -08:00

68 lines
2.0 KiB
C#

using Darkmatter.Core;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Animations.Rigging;
using VContainer;
namespace Darkmatter.Presentation
{
public class PlayerAnimController : HumonoidAnim, IPlayerAnim
{
private readonly int shootHash = Animator.StringToHash("IsShooting");
private readonly int reloadHash = Animator.StringToHash("Reload");
public TwoBoneIKConstraint HandOnGunIK; //for gunHand Ik
private Coroutine reloadCoroutine;
public void PlayReloadAnim(IWeapon currentWeapon)
{
if (reloadCoroutine == null)
{
reloadCoroutine = StartCoroutine(ReloadRoutine(currentWeapon));
}
}
IEnumerator ReloadRoutine(IWeapon currentWeapon)
{
yield return BlendLayerWeight(1, 1, 0.2f);
//animator.SetLayerWeight(1,1);
HandOnGunIK.weight = 0f;
animator.SetTrigger(reloadHash);
yield return new WaitForSeconds(3f); //gave the length of the animation very bad practice
// animator.SetLayerWeight(1, 0);
yield return BlendLayerWeight(1, 0, 0.2f);
HandOnGunIK.weight = 1f;
currentWeapon.Reload();
reloadCoroutine = null;
}
IEnumerator BlendLayerWeight(int layerIndex, float targetWeight, float blendTime)
{
float startWeight = animator.GetLayerWeight(layerIndex);
float time = 0f;
while (time < blendTime)
{
time += Time.deltaTime;
float t = time / blendTime;
float weight = Mathf.Lerp(startWeight, targetWeight, t);
animator.SetLayerWeight(layerIndex, weight);
yield return null;
}
animator.SetLayerWeight(layerIndex, targetWeight);
}
public void PlayShootAnim()
{
Debug.Log("player Shoot");
}
}
}