function init(self)
	msg.post(".", "acquire_input_focus")
	-- Add initialization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
	self.timer = 0
	self.curse_created = false
	self.timeout = 2.0
	self.curse_count = 0
	self.curses = {}
	self.amulet_count = 0
	self.amulets = {}
	self.mummy_timer = 0
	self.paused = false

	createamulets(self)
	msg.post("level3:/pause#sprite", "disable")
end

function createamulets(self)

	local positions = {
		vmath.vector3(800,75,0.3),
		vmath.vector3(100,200,0.3),
		vmath.vector3(850,325,0.3),
		vmath.vector3(400,450,0.3),
		vmath.vector3(300,580,0.3),
		vmath.vector3(600,600,0.3),
	}
		
	for loop=1,5,1 do
		local ids = collectionfactory.create("amulets#amulet_factory", positions[loop], nil)
		local amulet_id = ids[hash("/amulets.go")]
		table.insert(self.amulets, amulet_id)
		local init_table = { creator = msg.url() }
		msg.post(amulet_id, "creator", init_table)
	end
end

function final(self)
	-- Add finalization code here
	-- Learn more: https://defold.com/manuals/script/
	-- Remove this function if not needed
end

function update(self, dt)
	if not self.paused then
		self.timer = self.timer + dt
		if self.timer >= self.timeout then
			self.timer = 0
			self.curse_created = false
		end

		self.mummy_timer = self.mummy_timer - dt
		if self.mummy_timer < 0 then
			sprite.play_flipbook("mummy#still", hash("stillmummy"))
			msg.post("mummy#curse", "disable")
			msg.post("mummy#still", "enable")
		end
		
		if not self.curse_created then
			self.curse_created = true
			self.timeout = math.random() * 2 + 2
			self.mummy_timer = 0.5
			sprite.play_flipbook("mummy#curse", hash("cursemummy"))
			msg.post("mummy#curse", "enable")
			msg.post("mummy#still", "disable")
			local ids = collectionfactory.create("curses#curse_factory", vmath.vector3(900,560,0.0), nil)
			local curse_id = ids[hash("/curses")]
			table.insert(self.curses, curse_id)
			
			local msg_table = { creator = msg.url() }
			msg.post(curse_id, "creator", msg_table)
			
			self.curse_count = self.curse_count + 1
		end
	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)
	if message_id == hash("curse_deleted") then
		for i, id in ipairs(self.curses) do
			local id_url = msg.url(id)
			if id_url.path == sender.path then
				--print("Matched and removing:", id)
				go.delete(id) 
				table.remove(self.curses, i)
				break
			end
		end		
		self.curse_count = 0	
		for _ in pairs(self.curses) do
			self.curse_count = self.curse_count + 1
		end
		--print ("Curse removed, we now have " .. self.curse_count .. " curses left")
	end
	if message_id == hash("amulet_deleted") then
		for i, id in ipairs(self.amulets) do
			local id_url = msg.url(id)
			if id_url.path == sender.path then
				--print("Matched and removing:", id)
				go.delete(id) 
				table.remove(self.amulets, i)
				break
			end
		end		
		self.amulet_count = 0	
		for _ in pairs(self.amulets) do
			self.amulet_count = self.amulet_count + 1
		end
		--print ("Amulet removed, we now have " .. self.amulet_count .. " amulets left")
	end
end

function on_input(self, action_id, action)
	if action_id == hash("escape") and action.pressed then
		msg.post("main:/controller#main", "escape")
	elseif action_id == hash("pause") and action.pressed then
		if not self.paused then
			self.paused = true
			msg.post("/pause#sprite", "enable")
			msg.post("/player#player", "pause")
			for i, id in ipairs(self.curses) do
				local id_url = msg.url(id)
				msg.post(id_url, "pause")
			end
		else
			self.paused = false
			msg.post("/player#player", "unpause")
			for i, id in ipairs(self.curses) do
				local id_url = msg.url(id)
				msg.post(id_url, "unpause")
				msg.post("/pause#sprite", "disable")
			end
		end
	end
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
