function init(self)
	msg.post(".", "acquire_input_focus")
	-- show the title screen when the game is loaded
	
	msg.post("#startscreen_proxy", "load")
--	msg.post("#level2_proxy", "load")
	self.collection_to_load = ""
	self.current_collection = "#startscreen_proxy"
--	self.current_collection = "#level2_proxy"
	
	--msg.post("#level2_proxy", "load")
	--self.collection_to_load = ""
	--self.current_collection = "#level2_proxy"
	
	local my_file_path = sys.get_save_file("cursegame", "high_scores")
	self.highscore_table = sys.load(my_file_path)
	if not next(self.highscore_table) then
		for i = 1, 10, 1 do
			table.insert(self.highscore_table, {"---", 0})
		end
		sys.save(my_file_path, self.highscore_table)
	end
	-- Validate highy score tables, as nils can can problems
	for i = 1, 10, 1 do
		if self.highscore_table[i][1] == nil then
			self.highscore_table[i][1] = "---"
		end
		if self.highscore_table[i][2] == nil then
			self.highscore_table[i][2] = 0
		end
	end
	
	self.highscore_position = 0
	self.current_lives = 3
	self.current_level = "level1"
	self.current_score = 0
	self.hide_banner = true
end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self, dt)
	if not self.hide_banner then
		msg.post("/banner#score", "show")
		msg.post("/banner#score", "set_scores", { score = self.current_score })
		msg.post("/banner#banner", "lives", { lives = self.current_lives })
	else
		msg.post("/banner#score", "hide")
		msg.post("/banner#banner", "hide")
	end
end

function fixed_update(self, dt)
	-- This function is called if 'Fixed Update Frequency' is enabled in the Engine section of game.project
	-- Can be coupled with fixed updates of the physics simulation if 'Use Fixed Timestep' is enabled in
	-- Physics section of game.project
	-- Add update code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function on_message(self, message_id, message, sender)
	-- show the collection once loaded into memory
	if message_id == hash("proxy_loaded") then
		msg.post(sender, "enable")
	-- load the correct collection when a message is received
	elseif message_id == hash("show level1 screen") then
		-- title screen
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#level1_proxy"
		self.current_score = 0
		self.current_lives = 3
		self.current_level = "level1"
		self.hide_banner = false
	--load game over scsreen when character is dead
	elseif message_id == hash("NoMoreLives") then
		self.hide_banner = true
		local show_gameover = true
		--print("dead")
		for i = 1, 10, 1 do
			if self.current_score > self.highscore_table [i][2] then
				for j = 9, i, -1 do
					self.highscore_table[j+1][1] = self.highscore_table[j][1]
					self.highscore_table[j+1][2] = self.highscore_table[j][2]
				end
				self.highscore_table [i][2] = self.current_score
				--self.highscore_table [self.highscore_position][1] = 
				self.highscore_position = i
				local my_file_path = sys.get_save_file("cursegame", "high_scores")
				sys.save(my_file_path, self.highscore_table)
				msg.post(self.current_collection, "unload")
				self.collection_to_load = "#entername_proxy"
				show_gameover = false
				break
			end
		end
		if show_gameover == true then
			msg.post(self.current_collection, "unload")
			self.collection_to_load = "#gameover_proxy"
		end
	elseif message_id == hash("show scoreboard screen") then
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#scoreboard_proxy"
	elseif message_id == hash("show instructions screen") then
		-- instructions screen
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#instructions_proxy"
	elseif message_id == hash("show settings screen") then
		-- settings screen
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#settings_proxy"
	elseif message_id == hash("escape") then
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#startscreen_proxy"
	elseif message_id == hash("send_table") then
		msg.post("scoreboard:/scoreboard#scoreboard", "highscore_table", { high_scores = self.highscore_table})
	elseif message_id == hash("save_name") then
		self.highscore_table[self.highscore_position][1] = message.name
		local my_file_path = sys.get_save_file("cursegame", "high_scores")
		sys.save(my_file_path, self.highscore_table)
		msg.post(self.current_collection, "unload")
		self.collection_to_load = "#scoreboard_proxy"
	elseif message_id == hash("LevelCompleted") then
		if self.current_collection == "#level1_proxy" then
			self.current_level = "level2"
			msg.post(self.current_collection, "unload")
			self.collection_to_load = "#level2_proxy"
		elseif self.current_collection == "#level2_proxy" then
			self.current_level = "level3"
			msg.post(self.current_collection, "unload")
			self.collection_to_load = "#level3_proxy"
		end
	elseif message_id == hash("LifeLost") then
		self.current_lives = self.current_lives - 1
		if self.current_lives <= 0 then
			msg.post("main:/controller#main", "NoMoreLives")
		end
	elseif message_id == hash("proxy_unloaded") then
		msg.post(self.collection_to_load, "load")
		self.current_collection = self.collection_to_load
		self.collection_to_load = ""
	elseif message_id == hash("add 100 points") then
		self.current_score = self.current_score + 100
		--print(self.current_score)
	end
end

function on_input(self, action_id, action)
	-- Add input-handling code here. The game object this script is attached to
	-- must have acquired input focus:
	--
	--    msg.post(".", "acquire_input_focus")
	--
	-- All mapped input bindings will be received. Mouse and touch input will
	-- be received regardless of where on the screen it happened.
	-- Learn more: https://defold.com/manuals/input/
	-- Remove this function if not needed
end

function on_reload(self)
	-- Add reload-handling code here
	-- Learn more: https://defold.com/manuals/hot-reload/
	-- Remove this function if not needed
end
