extends Node

var score = 0
var highscore = 0
var wave = 0
var lives = 3
var game = null
var player = null
var ui = null
var score_file = "user://highscore.save"
var game_over = false
var paused = false

func new_wave():
	wave += 1
	ui.ui_update_wave(wave)

func load_highscore():
	var stored_highscore
	var f = File.new()
	if f.file_exists(score_file):
		f.open(score_file, File.READ)
		stored_highscore = f.get_var()
		f.close()
	else:
		stored_highscore = 0
	return stored_highscore
			
func save_highscore(hs):
	if load_highscore() < hs:
		var f = File.new()
		if f.file_exists(score_file):
			f.open("user://highscore.save", File.WRITE)
			f.store_var(hs)
			f.close()

func change_score(s):
	score += s
	ui.ui_update_score(score)
	if highscore <= score:
		highscore = score
		ui.ui_update_high_score(highscore)
	
func change_lives(x):
	lives += x
	lives = clamp(lives,0, 3)
	ui.ui_update_lives(lives)
	return lives

func game_over():
	game_over = true
	ui.ui_show_game_over_screen(score)
	save_highscore(highscore)
	
func restart_level():
	get_tree().reload_current_scene()
	lives = 3
	score = 0
	wave = 0
	
func start_level():
	game_over = false
	ui.ui_update_high_score(highscore)
	ui.ui_update_wave(1)