A Godot v4 game about working in an office
extends Node
class_name BattlePlayer

signal died(battler)
signal health_changed(old, new)
signal health_depleted
signal focus_changed(old, new)
signal focus_depleted
signal turn_ended

@onready var actions = $Actions

@export var max_health := 50
@export var max_focus := 20

enum MoveOption {ATTACK, SPELL, ITEM, RUN}

var health: int
var focus: int:
	set(val):
		var old_focus = focus
		focus = min(max(0, val), max_focus)
		focus_changed.emit(old_focus, focus)
		if focus == 0:
			focus_depleted.emit()
var is_alive: bool:
	get:
		return health > 0

#	{
#		health: 50,
#		max_health: 50,
#		focus: 20,
#		max_focus: 20,
#	},
func initialize(data):
	max_health = data.max_health
	health = data.health
	max_focus = data.max_focus
	focus = data.focus
	$PlayerBars.initialize()

func start_turn():
	print("starting turn")
	# TODO: A better way to do this, less entangled
	get_parent().get_node("Menu/menu").show_menu()

func choose_option(optionType: MoveOption, option: String):
	print("choosing option %s / %s" % [optionType, option])
	get_parent().get_node("Menu/menu").hide_menu()
	match optionType:
		MoveOption.ATTACK:
			print("You do an attack!")
			(get_parent() as Combat).show_dialog(["You do a %s" % option])
			await (get_parent() as Combat).dialog_box.dialog_hidden
			turn_ended.emit()
		MoveOption.SPELL:
			print("You cast a spell")
			(get_parent() as Combat).show_dialog(["You cast a %s" % option])
			await (get_parent() as Combat).dialog_box.dialog_hidden
			turn_ended.emit()
		MoveOption.ITEM:
			print("You use an item")
			(get_parent() as Combat).show_dialog(["You use a %s" % option])
			await (get_parent() as Combat).dialog_box.dialog_hidden
			turn_ended.emit()
		MoveOption.RUN:
			print("You try to run")
			(get_parent() as Combat).show_dialog(["You can't run!"])
			await (get_parent() as Combat).dialog_box.dialog_hidden
			get_parent().get_node("Menu/menu").show_menu()

func reset():
	health = self.max_health
	focus = self.max_focus

func take_damage(amount: int):
	var old_health = health
	health -= amount
	health = max(0, health)
	health_changed.emit(old_health, health)
	if health == 0:
		health_depleted.emit()

func heal(amount: int):
	var old_health = health
	health = min(health + amount, max_health)
	health_changed.emit(old_health, health)