--go.property("creator", msg.url())

function init(self)
	sprite.play_flipbook("#sprite", hash("curse"))
	self.velocity = vmath.vector3(0, 0, 0)
	self.direction = vmath.vector3(-1, 0, 0)
	self.speed = 250
	self.gravity = -5000 -- gravity force
	self.angle = 0
	self.amulet_visible = false
	self.testing_amulet = false
	self.dying = false
	self.dying_timer = 1
	self.paused = false
end

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

function update(self, dt)
	if not self.paused then
		if self.dying then
			self.dying_timer = self.dying_timer - dt
			if self.dying_timer < 0 then
	--			self.dying = false
				msg.post(self.creator, "curse_deleted")
			end
		end
		if self.deleted then
			return -- skip logic if object is flagged for deletion
		end
		
		self.velocity.x = self.direction.x * self.speed
		local pos = go.get_position()

		pos = pos + self.velocity * dt
		if pos.x < 24 or pos.y < 0 then
			self.deleted = true
			msg.post(self.creator, "curse_deleted")
		elseif pos.x > 1030 then
			pos.x = 1030
		end

		--msg.post("@render:", "draw_line", { start_point = centre, end_point = platform, color = vmath.vector4(1,0,0,1) })

		if not self.deleted then
			local centre = vmath.vector3(pos.x, pos.y, 0)
			local platform = pos +  vmath.vector3(0, -20, 0)
			
			local platform_result = physics.raycast(centre, platform, { hash("platform") })
			if platform_result then
				local point = platform_result.position - pos
				if platform_result.normal.x <= 0 then
					self.direction.x = -1
				else 
					self.direction.x = 1
				end
				--msg.post("@render:", "draw_debug_text", { text = "Platform", position = vmath.vector3(20, 400, 0), color = vmath.vector4(1, 0, 0, 1) } )
				--msg.post("@render:", "draw_debug_text", { text = "X : " .. tostring(point.x), position = vmath.vector3(20, 380, 0), color = vmath.vector4(1, 0, 0, 1) } )
				--msg.post("@render:", "draw_debug_text", { text = "Y : " .. tostring(point.y), position = vmath.vector3(20, 360, 0), color = vmath.vector4(1, 0, 0, 1) } )
				self.grounded = true
				self.speed = 250
				self.velocity.y = 0
				if point.y > -20 then
					pos.y = pos.y + (point.y + 20)
				end
			else
				self.grounded = false
			end

			if not self.grounded then
				self.velocity.y = self.velocity.y + self.gravity * dt
			end

			go.set_position(pos)
			
			local angle = math.rad(self.angle)  -- convert degrees to radians
			local rot = vmath.quat_rotation_z(angle)
			go.set_rotation(rot)
			self.angle = self.angle + (self.direction.x * -12)
		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("contact_point_response") then
		if message.group == hash("holding_amulet") then
			if self.testing_amulet == false then
				msg.post("/holding_amulet#holdingamulet", "testing_amulet")
				self.testing_amulet = true
			end
		end
		if message.group == hash("curse_jump_pads") then
			print("Oh yeah!")
			self.velocity.y = 150
			self.speed = 550
		end
	end
	if message_id == hash("visible") then
		self.amulet_visible = true
	end
	if message_id == hash("testing_amulet_response") then
		if message.visible == true then
			sprite.play_flipbook("#sprite", hash("explosion"))
			msg.post("main:/controller#main", "play curse dying sound")
			self.deleted = true
			self.dying = true
			msg.post("main:/controller#main", "add 100 points")
		end
	end
	if message_id == hash("dying") then
		msg.post(sender, ("curse_dying_reply"), {dying = self.dying})
	end
	if message_id == hash("pause") then
		self.paused = true
	end
	if message_id == hash("unpause") then
		self.paused = false
	end
	if message_id == hash("creator") then
		self.creator = message.creator
	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
