class_name StaminaComponent
extends Node

export var max_stamina: int = 0
## Recovery Rate in units per second
export var recovery_rate: int = 0
onready var stamina :int = max_stamina


signal stamina_depleted()

var recovery_counter: float = 0


func _process(delta):
	recovery_counter += delta 
	if recovery_rate != 0:
		var _rate :float = 1.0/float(recovery_rate)
		if recovery_counter > _rate: ## Divide by zero warning
			recover(1)
			recovery_counter = 0

func reduce_stamina(_amount :int):
	if _amount < 0:
		push_error("ERROR: Only positive numbers in stamina component functions.")
	stamina -= _amount
	
	if stamina <= 0:
		emit_signal("stamina_depleted")
		stamina = 0
		
func recover(recover_amount :int):
	if recover_amount < 0:
		push_error("ERROR: Only positive numbers in stamina component functions.")
		return
	stamina += recover_amount
	
	if stamina > max_stamina:
		stamina = max_stamina
