local e=nil local Q={}Q.__index=Q function Q.new()local o=setmetatable({currentStage=nil,currentStageClassName=nil;timeStageStarted=nil},Q)o.gameStageChanged=Signal.new()getGameStage.OnServerInvoke=function(e)return o.currentStageClassName end e=o return o end function Q.getInstance()return e end function Q.nextStage(e)e.currentStage:destroy()local Q=nil for o,H in ipairs(Conf.stages)do if H==e.currentStageClassName then Q=Conf.stages[o+1]break end end e:_initializeStage(Q)end function Q.start(e)e:_initializeStage(Conf.stages[1])end function Q.destroy(e)e.gameStageChanged=nil if e.currentStage then e.currentStage:destroy()end end function Q._initializeStage(Q,e)if not e then return end print(string.format("\073\110\105\116\105\108\105\122\105\110\103 \083\116\097\103\101\058 \037\115",e))local o=stageFolder:FindFirstChild(e)assert(o,string.format("\070\097\105\108\101\100 \116\111 \105\110\105\116\105\097\108\105\122\101 \115\116\097\103\101 \"\037\115\"\044 \115\116\097\103\101 \100\111\101\115 \110\111\116 \101\120\105\115\116",e))Q.currentStage=(require(o)).new(Q)Q.currentStageClassName=e Q.timeStageStarted=tick()Q.currentStage:initialize()Q.gameStageChanged:fire(e)gameStageChanged:FireAllClients(e)Broadcast.fireRemote("\071\065\077\069\095\083\084\065\071\069\095\067\072\065\078\071\069",e)end return Q


local singleton = nil

local GameStageHandler = {}
GameStageHandler.__index = GameStageHandler

function GameStageHandler.new()
	local self = setmetatable({
		currentStage = nil,
		currentStageClassName = nil,
		timeStageStarted = nil,
	}, GameStageHandler)

	self.gameStageChanged = Signal.new()

	getGameStage.OnServerInvoke = function(player)
		return self.currentStageClassName
	end

	singleton = self

	return self
end

function GameStageHandler.getInstance()
	return singleton
end

function GameStageHandler:nextStage()
	-- Destroy the previous stage
	self.currentStage:destroy()

	-- Figure out what the next stage will be
	local nextStageName = nil
	for index, value in ipairs(Conf.stages) do
		if value == self.currentStageClassName then
			nextStageName = Conf.stages[index + 1]
			break
		end
	end

	self:_initializeStage(nextStageName)
end

function GameStageHandler:start()
	self:_initializeStage(Conf.stages[1])
end

function GameStageHandler:destroy()
	self.gameStageChanged = nil

	if self.currentStage then
		self.currentStage:destroy()
	end
end

function GameStageHandler:_initializeStage(stageName)
	if not stageName then
		return
	end

	print(string.format("Initilizing Stage: %s", stageName))

	-- Initialize the next stage, if there is one
	local nextStage = stageFolder:FindFirstChild(stageName)

	assert(nextStage, string.format("Failed to initialize stage \"%s\", stage does not exist", stageName))

	self.currentStage = require(nextStage).new(self)
	self.currentStageClassName = stageName
	self.timeStageStarted = tick()

	self.currentStage:initialize()

	-- Fire local event
	self.gameStageChanged:fire(stageName)

	-- Notify clients of the stage change
	gameStageChanged:FireAllClients(stageName)
	Broadcast.fireRemote("GAME_STAGE_CHANGE", stageName)
end

return GameStageHandler
