The State pattern in action - StageManager

Another aspect of the Mach5 Engine that uses the State pattern is the M5StageManager class:

class M5StageManager 
{
public:
friend class M5App;

//Registers a GameStage and a builder with the the StageManger
static void AddStage(M5StageTypes type, M5StageBuilder* builder);
//Removes a Stage Builder from the Manager
static void RemoveStage(M5StageTypes type);
//Clears all stages from the StageManager
static void ClearStages(void);
//Sets the given stage ID to the starting stage of the game
static void SetStartStage(M5StageTypes startStage);
//Test if the game is quitting
static bool IsQuitting(void);
//Test stage is restarting
static bool IsRestarting(void);
//Gets the pointer to the users game specific data
static M5GameData& GetGameData(void);
//Sets the next stage for the game
static void SetNextStage(M5StageTypes nextStage);
// Pauses the current stage, so it can be resumed but changes stages
static void PauseAndSetNextStage(M5StageTypes nextStage);
// Resumes the previous stage
static void Resume(void);
//Tells the game to quit
static void Quit(void);
//Tells the stage to restart
static void Restart(void);
private:
static void Init(const M5GameData& gameData, int framesPerSecond);
static void Update(void);
static void Shutdown(void);
static void InitStage(void);
static void ChangeStage(void);

};//end M5StageManager

Since there will only be one of these in the game, all of the functionality has been made static similarly to a Singleton but, depending on the state that the project is in, it will do different things. Take, for example, changing what stage we are in. I'm sure you'll find that it looks very similar to how we changed states earlier:

void M5StageManager::ChangeStage(void)
{
/*Only unload if we are not restarting*/
if (s_isPausing)
{
M5ObjectManager::Pause();
M5Phy::Pause();
M5Gfx::Pause(s_drawPaused);
PauseInfo pi(s_pStage, s_currStage);
s_pauseStack.push(pi);
s_isPausing = false;
}
else if (s_isResuming)
{
/*Make sure to shutdown the stage*/
s_pStage->Shutdown();
delete s_pStage;
s_pStage = nullptr;
}
else if (!s_isRestarting) //Just changine the stage
{
/*Make sure to shutdown the stage*/
s_pStage->Shutdown();
delete s_pStage;
s_pStage = nullptr;

//If we are setting the next state, that means we are ignore all
//paused states, so lets clear the pause stack
while (!s_pauseStack.empty())
{
M5Gfx::Resume();
M5Phy::Resume();
M5ObjectManager::Resume();
PauseInfo pi = s_pauseStack.top();
pi.pStage->Shutdown();
delete pi.pStage;
s_pauseStack.pop();
}

}
else if (s_isRestarting)
{
/*Make sure to shutdown the stage*/
s_pStage->Shutdown();
}

s_currStage = s_nextStage;
}

I highly advise taking a closer look at the file and going through each function to see how they interact with each other.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118