nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
GameState Class Referenceabstract

Abstract definition of a game state. More...

#include <GameState.hpp>

Inheritance diagram for GameState:

Public Types

enum  StateCode {
  QUIT , CONTINUE , MAIN_MENU , GAME_START ,
  GAME_OVER
}
 All possible transitions between states. More...
 

Public Member Functions

virtual void load (int stack=0)=0
 Where every state initializes it's resources.
 
virtual int unload ()=0
 Where every state destroys it's resources.
 
virtual StateCode update ()=0
 Called every frame, where states calculate everything that can change.
 
virtual void draw ()=0
 Called every frame, where states draw stuff on screen.
 

Detailed Description

Abstract definition of a game state.

For dummies

A game state is a certain well-defined way the game behaves.

Like the main menu (a state), for example. From there, the player can start the actual game (another state) and, show the pause menu (even another state).

Examples include the game over screen, high score screen, cutscenes, etc.

For developers

All the other game states inherit from this baby. Each one must implement at least four methods - load(), unload(), update() and draw().

Each state controls the game flow via return values of the update() method. If, for instance, the player dies during the game, it should return GAME_OVER or QUIT.

A state can also comunicate with the next one by returning a value from unload(). That value will be passed on the next state's load() and it should know what to do with it.

Definition at line 31 of file GameState.hpp.

Member Enumeration Documentation

◆ StateCode

All possible transitions between states.

They are used to change from one state to the other. The current state returns this at update() and the StateManager makes the appropriate changes.

Definition at line 39 of file GameState.hpp.

Constructor & Destructor Documentation

◆ ~GameState()

virtual GameState::~GameState ( )
inlinevirtual

Definition at line 51 of file GameState.hpp.

Member Function Documentation

◆ draw()

virtual void GameState::draw ( )
pure virtual

Called every frame, where states draw stuff on screen.

Implemented in GameStateGame, and GameStateMainMenu.

◆ load()

virtual void GameState::load ( int stack = 0)
pure virtual

Where every state initializes it's resources.

The stack is the previous state's returned value from unload(), allowing a state to communicate with the next one.

Implemented in GameStateGame, and GameStateMainMenu.

◆ unload()

virtual int GameState::unload ( )
pure virtual

Where every state destroys it's resources.

The returned value will be sent to the next state's load() so we can send a specific message to it.

Implemented in GameStateGame, and GameStateMainMenu.

◆ update()

virtual StateCode GameState::update ( )
pure virtual

Called every frame, where states calculate everything that can change.

The returned value will be checked by the StateManager to see if we must change the current state - if so, which one should we go next.

Implemented in GameStateGame, and GameStateMainMenu.


The documentation for this class was generated from the following file: