using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class movementsAnimations : MonoBehaviour
{
private bool isGrounded;
[Header("Movements------------------------------------")]
[SerializeField] private float speedForce = 10f;
[SerializeField] private Animator animator;
[Header("RunningStaminaBar------------------------------------")]
[SerializeField] private Slider staminaBar;
[SerializeField] private float runningCost = 0.25f;
[SerializeField] private float staminaRegenerationValue = 0.3f;
private float maxStamina = 1f;
private float actualStamina;
private float staminaRegenerationCooldown;
private bool staminaBarIsUsed;
private float speedValue;
public void Start()
{
actualStamina = maxStamina;
staminaBar.maxValue = maxStamina;
speedValue = speedForce;
}
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
float moveValue = Mathf.Abs(horizontalInput) + Mathf.Abs(verticalInput);
animator.SetBool("isGrounded", isGrounded);
//this set the value of the slider that contains the stamina bar
staminaBar.value = actualStamina;
//idle animation
if (isGrounded)
{
if (moveValue == 0f)
{
animator.SetBool("idle", true);
}
else
{
animator.SetBool("idle", false);
}
}
//stamina animation
if (actualStamina < 1f && !staminaBarIsUsed)
{
staminaRegenerationCooldown += Time.deltaTime;
if (staminaRegenerationCooldown > 1.7f)
{
actualStamina += staminaRegenerationValue * Time.deltaTime;
}
}
//run, walk and stamina animation
if (isGrounded)
{
if (moveValue != 0)
{
if (speedValue == speedForce)
{
animator.SetBool("walk", true);
}
else
{
animator.SetBool("walk", false);
}
if (actualStamina > 0f && Input.GetKey(KeyCode.LeftShift))
{
staminaBarIsUsed = true;
staminaRegenerationCooldown = 0f;
speedValue = speedForce * 2;
animator.SetBool("run", true);
animator.SetBool("walk", false);
actualStamina -= runningCost * Time.deltaTime;
}
else
{
staminaBarIsUsed = false;
animator.SetBool("run", false);
speedValue = speedForce;
}
}
else
{
staminaBarIsUsed = false;
speedValue = speedForce;
animator.SetBool("run", false);
animator.SetBool("walk", false);
}
}
else
{
staminaBarIsUsed = false;
speedValue = speedForce;
animator.SetBool("run", false);
animator.SetBool("walk", false);
}
//this tell the "PlayerMovements" script what value of the speed force he have to use
if (speedValue == speedForce)
{
FindObjectOfType<PlayerMovements>().speedForceOfWalk();
}
if (speedValue == speedForce * 2)
{
FindObjectOfType<PlayerMovements>().speedForceOfRun();
}
}
//these are used by the "PlayerMovemnts" script to tell this script what is the state of the bool "isGrounded"
public void isGroundedOn()
{
isGrounded = true;
}
public void isGroundedOff()
{
isGrounded = false;
}
}