UnityBiz/Assets/scripts/Movement/PlayerMovementController.Sprint.cs
adamwi1000 b854205dcb test
2024-08-02 06:41:41 +02:00

70 lines
2.2 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace Player.Movement
{
public partial class PlayerMovementController
{
private float _originalSpeed;
private UIDocument _document;
private ProgressBar _stamina;
private float CurrentStamina = 100f;
private float timer = 0f;
[SerializeField] private PlayerMovementController _playerMovementController;
[SerializeField] private float _staminaDepletionMultiplier;
[SerializeField] private float _staminaRecoveryMultiplier;
public float delay = 3;
private void SprintStart(InputAction.CallbackContext obj)
{
StartSprint();
}
private void SprintEnd(InputAction.CallbackContext obj)
{
StopSprint();
}
public void StartSprint()
{
_originalSpeed = maxMovementSpeed;
maxMovementSpeed = sprintSpeed;
}
public void StopSprint()
{
maxMovementSpeed = _originalSpeed;
}
public void StaminaSystem()
{
if (Input.GetKey(KeyCode.LeftShift))//if shift is pressed
{
timer = 0f;//timer is set to 0
CurrentStamina = Mathf.Max(0, CurrentStamina - Time.deltaTime * _staminaDepletionMultiplier);//stamina began to deplet
if (CurrentStamina <= 0)//if current stamina is lower or equal to 0
{
_playerMovementController.StopSprint();//use function from another class
}
}
else if (CurrentStamina < 100)//and also when current stamina is lower than 100
{
timer += Time.deltaTime;//timer starts working
if (timer > delay)//when timer is greater than delay time
{
CurrentStamina = Mathf.Min(100f, CurrentStamina + Time.deltaTime * _staminaRecoveryMultiplier);//stamina began to recover
if (CurrentStamina >= 100f)//on top of that if timer is greater or equal 100
{
timer = 0f;//set timer to 0
}
}
}
}
}
}