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

49 lines
1.9 KiB
C#

using Player.Movement;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class UIEvents : MonoBehaviour
{
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 Awake()
{
_document = GetComponent<UIDocument>();
_stamina = _document.rootVisualElement.Q<ProgressBar>("Stamina");
_stamina.value = CurrentStamina; // set starting value of stamina
}
private void Update()//petla update
{
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
}
}
}
_stamina.value = CurrentStamina;//set stamina value to current stamina
}
}