extends KinematicBody2D

class_name Player

const touch_callback_function_name = "on_player_touched"

export(NodePath) var navigation_tile_map_path : NodePath

export var hp : int = 1000 setget set_hp

export var atk : int = 10 setget set_atk

export var def : int = 10 setget set_def

export var money : int = 0 setget set_money

export var yellow_key : int = 0 setget set_yellow_key

export var blue_key : int = 0 setget set_blue_key

export var red_key : int = 0 setget set_red_key
# 拥有的道具
export var props : Array = []

var navigation_tile_map : NavigationTileMap

var navigation_rect : Rect2

onready var array_tween : ArrayTween = $ArrayTween

onready var animation_tree : AnimationTree = $AnimationTree

signal hp_changed(hp)
signal atk_changed(atk)
signal def_changed(def)
signal money_changed(money)
signal yellow_key_changed(yellow_key)
signal blue_key_changed(blue_key)
signal red_key_changed(red_key)
signal prop_added(prop_name)

func _ready():
	self.add_to_group("serializable")
	#初始化导航相关数据
	if not navigation_tile_map_path.is_empty():
		navigation_tile_map = get_node(navigation_tile_map_path)
		navigation_rect = navigation_tile_map.get_used_rect()
		navigation_rect.position *= navigation_tile_map.cell_size
		navigation_rect.size *= navigation_tile_map.cell_size

# 监听屏幕点击事件
func _unhandled_input(event):
	if event is InputEventMouseButton and event.is_pressed() and navigation_tile_map != null:
		if not navigation_rect.has_point(event.position):
			return
		var astar = navigation_tile_map.astar
		var start_id = astar.get_closest_point(position)
		var end_id = astar.get_closest_point(event.position)
		var paths = astar.get_point_path(start_id,end_id)
		if paths.size() < 2:
			return
		animation_tree.active = true
		array_tween.interpolate_array(paths)

func _on_ArrayTween_array_completed():
	animation_tree.active = false

func _on_ArrayTween_array_value_step(step):
	var diff = step - position
	animation_tree["parameters/player/blend_position"] = diff
	var result = move_and_collide(diff)
	call_collider(result)

# 停止角色运动
func stop():
	set_process_unhandled_input(false)
	array_tween.stop_all()
	animation_tree.active = false

# 恢复角色运动
func resume():
	set_process_unhandled_input(true)

# 调用被碰撞对象的方法
func call_collider(collision:KinematicCollision2D):
	if collision == null or collision.collider == null:
		return
	var collider = collision.collider
	if collider is TileMap:
		return
	if collider.has_method(touch_callback_function_name):
		array_tween.stop_all()
		animation_tree.active = false
		collider.call(touch_callback_function_name,self)

# 保存游戏数据
func save_data(data:Dictionary,global:Dictionary):
	if not global.has("player"):
		global.player = {}
	global.player.hp = hp
	global.player.atk = atk
	global.player.def = def
	global.player.money = money
	global.player.yellow_key = yellow_key
	global.player.blue_key = blue_key
	global.player.red_key = red_key
	global.player.props = props
	data.player_position = position

# 还原游戏数据
func load_data(data:Dictionary,global:Dictionary):
	if data.has("player_position"):
		self.position = data.player_position
	self.hp = global.player.hp
	self.atk = global.player.atk
	self.def = global.player.def
	self.money = global.player.money
	self.yellow_key = global.player.yellow_key
	self.blue_key = global.player.blue_key
	self.red_key = global.player.red_key
	self.props = global.player.props
	#通知
	for prop in self.props:
		emit_signal("prop_added",prop)
	
func set_hp(value):
	hp = value
	emit_signal("hp_changed",value)

func set_atk(value):
	atk = value
	emit_signal("atk_changed",value)
	
func set_def(value):
	def = value
	emit_signal("def_changed",value)
	
func set_money(value):
	money = value
	emit_signal("money_changed",value)
	
func set_yellow_key(value):
	yellow_key = value
	emit_signal("yellow_key_changed",value)
	
func set_blue_key(value):
	blue_key = value
	emit_signal("blue_key_changed",value)
	
func set_red_key(value):
	red_key = value
	emit_signal("red_key_changed",value)

# 增加道具
func add_prop(name:String)->void:
	self.props.append(name)
	emit_signal("prop_added",name)
