local k=nil local q={}q.__index=q function q.new()local P=setmetatable({currentStage=nil;currentStageClassName=nil;timeStageStarted=nil},q)P.gameStageChanged=Signal.new()getGameStage.OnServerInvoke=function(k)return P.currentStageClassName end k=P return P end function q.getInstance()return k end function q.nextStage(k)k.currentStage:destroy()local q=nil for P,L in ipairs(Conf.stages)do if L==k.currentStageClassName then q=Conf.stages[P+1]break end end k:_initializeStage(q)end function q.start(k)k:_initializeStage(Conf.stages[1])end function q.destroy(k)k.gameStageChanged=nil if k.currentStage then k.currentStage:destroy()end end function q._initializeStage(q,k)if not k 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",k))local P=stageFolder:FindFirstChild(k)assert(P,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",k))q.currentStage=(require(P)).new(q)q.currentStageClassName=k q.timeStageStarted=tick()q.currentStage:initialize()q.gameStageChanged:fire(k)gameStageChanged:FireAllClients(k)Broadcast.fireRemote("\071\065\077\069\095\083\084\065\071\069\095\067\072\065\078\071\069",k)end return q
--[[
	Implementation
]]

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
