breadwinner/scenes/player/stamina_component.gd
Hellie-xd 900f5eb6c3 kinda working options menu, but needs work
also audio lags on my pc, probably the cpu cant keep up with the audio
2026-08-12 01:47:15 +02:00

52 lines
1.2 KiB
GDScript

class_name StaminaComponent extends Node
var stamina_recharge: bool = true
var stamina: float = 100.0:
set(new_value):
stamina = new_value
stamina_changed.emit(new_value)
var running: bool = false
## how many seconds to wait for stamina recharge
@export var stamina_recharge_cooldown: float = 3.0
## emits the current stamina value
signal stamina_changed(cur_stamina:float)
var _timer: Timer = Timer.new()
func restart_timer():
stamina_recharge = false
_timer.start()
func _process(delta: float) -> void:
if stamina_recharge and stamina <= 100:
add_stamina(10 * delta)
if running and stamina > 0:
remove_stamina(PlayerConsts.SPRINT_STAMINA_COST * delta)
func remove_stamina(by:float) -> void:
stamina = move_toward(stamina, 0, by)
func add_stamina(by:float) -> void:
stamina = move_toward(stamina, 100, by)
func jump() -> void:
remove_stamina(PlayerConsts.JUMP_STAMINA_COST)
restart_timer()
func run(is_running:bool) -> void:
if not is_running and running:
restart_timer()
running = is_running
stamina_recharge = false
func _ready() -> void:
_timer.wait_time = stamina_recharge_cooldown
_timer.autostart = false
_timer.timeout.connect(func (): stamina_recharge = true)
add_child(_timer)