extends Node2D

onready var nav = $Navigation2D
onready var gui = $CanvasLayer/GUI
onready var player = $Navigation2D/TileMap/Player
onready var sound_player = $SoundPlayer

var enemies_remaining = 0

func _ready():
	var nodes = get_tree().get_nodes_in_group("enemy")
	enemies_remaining = len(nodes)
	for node in nodes:
		node.set_nav(nav)
		node.connect("enemy_killed", self, "handle_enemy_killed")
		node.connect("enemy_damaged", self, "handle_enemy_damaged")
	var medpacks = get_tree().get_nodes_in_group("medpack")
	for node in medpacks:
		node.connect("medpack_taken", self, "handle_medpack_taken")
	var ammo = get_tree().get_nodes_in_group("ammo")
	for node in ammo:
		node.connect("ammo_taken", self, "handle_ammo_taken")
	gui.set_hp(player.hp)

func handle_enemy_killed():
	enemies_remaining -= 1
	sound_player.play_enemy_killed1()
	if enemies_remaining <= 0:
		get_tree().paused = true
		gui.show_game_over()

func handle_enemy_damaged():
	sound_player.play_enemy_hurt1()

func handle_medpack_taken():
	sound_player.play_pickup_1()

func handle_ammo_taken():
	sound_player.play_powerup_1()

func _on_KinematicBody2D2_ammo_changed(ammo):
	gui.set_ammo(ammo)

func _on_KinematicBody2D2_hp_changed(hp):
	gui.set_hp(hp)

func _on_Player_player_dead():
	get_tree().paused = true
	gui.show_game_over()

func _on_Player_shot_fired():
	sound_player.play_shoot_1()

func _on_Player_player_hurt():
	sound_player.play_player_hurt1()
