extends Node

var max_health = 5
var player_health = 5
var remnant_max_health = 2
var max_number_of_remnants = 1

signal player_health_changed(new_health : int)

var _set_anchor_health = 5

func _ready() -> void:
	SignalBus.changed_scene_to.connect(reset)
	SignalBus.set_anchor.connect(func() : _set_anchor_health = player_health)
	SignalBus.reload_anchor.connect(func() : change_health_absolute(_set_anchor_health))

func reset(scene : Utils.GAME_SCENES):
	if scene == Utils.GAME_SCENES.GAME:
		reset_health()

func reset_health():
	change_health_absolute(max_health)

func change_health_absolute(new_health : int):
	var set_to = min(max_health, new_health)
	if set_to == player_health:
		return
	player_health = set_to
	check_health()
	player_health_changed.emit(player_health)

func change_health_delta(health_delta : int):
	var set_to = min(max_health, player_health + health_delta)
	if set_to == player_health:
		return
	player_health = set_to
	check_health()
	player_health_changed.emit(player_health)

func check_health():
	if player_health == 0:
		AudioManager.create_audio(SoundEffect.SOUND_EFFECT_TYPE.GAME_OVER)
		SignalBus.player_died.emit()  # for music
		SceneChanger.change_scene_to(Utils.GAME_SCENES.DEATH_SCREEN)
