tool
extends StaticBody2D

# 怪物信息
var monster_info : Dictionary

var monster_sprite : AnimatedSprite

signal monster_dead

func _ready():
	monster_sprite = get_parent()
	monster_info = MonsterBook.get_info_by_name(monster_sprite.animation)
	add_to_group("monster")

# 玩家触碰回调
func on_player_touched(player):
	player.stop()
	var hurt = get_hurt(player)
	if hurt > player.hp:
		Messages.showDialog("你无法打败此怪物")
		player.resume()
		return
	#播放动画
	var skill_sprite : AnimatedSprite = $SkillSprite
	skill_sprite.visible = true
	skill_sprite.playing = true
	yield(skill_sprite,"animation_finished")
	#减扣英雄血量
	player.hp -= hurt
	#增加金币
	player.money += monster_info.money
	#删除怪物
	monster_sprite.queue_free()
	player.resume()
	#通知
	emit_signal("monster_dead")

# 计算伤害
func get_hurt(player) -> int:
	var count = (self.monster_info.hp / (player.atk - monster_info.def)) as int
	var hurt = count * (monster_info.atk - player.def)
	if hurt < 0:
		return 0
	else:
		return hurt
