extends Node2D

onready var dialog : PopupDialog = $"ShopDialog"

# 文字

onready var message : Label = $"ShopDialog/Message"

# 按钮

onready var close_button : Button = $"ShopDialog/VBoxContainer/CloseDialog"

onready var add_hp_button : Button = $"ShopDialog/VBoxContainer/AddHp"

onready var add_atk_button : Button = $"ShopDialog/VBoxContainer/AddAtk"

onready var add_def_button : Button = $"ShopDialog/VBoxContainer/AddDef"

# 增加的hp
export var added_hp = 100

# 增加的攻击力
export var added_atk = 2

# 增加的防御力
export var added_def = 4

# 初始金币
export var init_money = 20

# 使用的次数
export var used_count = 0

# 玩家
var player : Player

func _ready():
	var used_money = get_used_money()
	message.text = "如果供奉%s金币，便可以增强你的力量，你想要什么呢......" % used_money
	add_hp_button.text = "生命+%s" % added_hp
	add_atk_button.text = "攻击力+%s" % added_atk
	add_def_button.text = "防御力+%s" % added_def
	
# 触碰回调
func on_player_touched(player):
	self.player = player
	dialog.popup_centered()

func _on_CloseDialog_pressed():
	dialog.visible = false

func _on_AddHp_pressed():
	if enough_money_to_pay():
		player.hp += added_hp
		used_count += 1

func _on_AddAtk_pressed():
	if enough_money_to_pay():
		player.atk += added_atk
		used_count += 1

func _on_AddDef_pressed():
	if enough_money_to_pay():
		player.def += added_def
		used_count += 1

func get_used_money() -> int:
	return init_money * (used_count + 1)

func enough_money_to_pay() -> bool:
	var money = get_used_money()
	if player.money >= money:
		player.money -= money
		return true
	else:
		Messages.showDialog("金钱不足")
		return false
