--[[
	Function called upon entering the state
]]

function PlayerGame.onEnter(stateMachine, serverPlayer, event, from, to)
	local player = serverPlayer.player

	coroutine.wrap(
		function()
			local character = player.Character

			if not character or not character.PrimaryPart then
				player:LoadCharacter()
				character = player.Character or player.CharacterAdded:Wait()

				while not character.PrimaryPart do
					wait()
				end

				character:WaitForChild("Humanoid")
			end

			if from ~= "camera" then
				onCharacterAdded(player, character)
			end

			local humanoid = character:FindFirstChild("Humanoid")

			humanoid.WalkSpeed = 16

			local primaryPart = character.PrimaryPart

			connections.characterAdded =
				player.CharacterAdded:Connect(
				function(character)
					while not character.Parent or character:FindFirstChild("Humanoid") do
						wait()
					end

					onCharacterAdded(player, character)
				end
			)

			while stateMachine.current == "game" do
				if isMoving then
					local currentTick = tick()
					MonsterManager.createInterestWhileTrue(
						primaryPart.Position,
						NOISE_GENERATED,
						function()
							return not isMoving or tick() < currentTick + NOISE_TIMEOUT
						end
					)
				end
				wait(0.25)
			end
		end
	)()
end
