Article Index
Introduction
Game Engine
Game Logic
Modding

Game Logic

The "game logic" is really the essence of the game; it creates the player character, the enemies, the environment, the user interface, everything!  The game logic code uses the game engine code to achieve its goals (by expressing the game through graphics, sound, and input).

When the game first starts up, the main menu is displayed.  This menu is controlled via the mainmenu.cpp file.  The CMainMenu class's constructor creates a number of sprite objects and uses them to display the GUI elements you see on screen.  The Update method of the CMainMenu class is called once every frame; this allows the main menu to respond to input and update its appearance.  You'll see that the Update method contains code to check for mouse clicks, handle character selection, exit the game, and start a level.  When the player clicks a level he/she wishes to play, the CMainMenu class then passes control over to the CGame class.

Contained within the game.cpp file is the CGame class code.  It handles all of the "game screen" input, and ensures that this input is appropriately passed to the player's game entity.  (The code for the player and enemy entities will be explained below.)  CGame ensures that all game entities are given a chance to "Update" each frame, by calling the CEntity::UpdateAll() method.

Additionally, the CGame class handles a number of housekeeping tasks, such as pausing the game if the escape key is pressed, updating the game timer that tracks how long the player has taken on the level, and checking for victory and failure conditions.

The elements of the game itself (the player, the enemies, the terrain, and the collectable tokens) are created via a tree of inherited classes.  The base class is CEntity (contained within the entity.cpp file).  All entities inherit common traits from this class, such as some generic initialization/de-initialization code and generic movement/collision detection methods.

2008_02_25_blank.jpg

The terrain and the collectable tokens are coded in the entityTerrainGlue.cpp, entityTerrainGrass.cpp, and entityToken.cpp files.  These entities are fairly simple, and are primarily responsible for loading the appropriate sprites to give themselves a graphical expression on-screen.  The CEntityToken class also contains some logic to handle the "mouse-click collecting" game mechanic: It detects clicks, and responds to them by moving the token towards the player.  The CEntityToken class also checks for collisions with the terrain, as tokens will stop moving towards the player when they collide with terrain entities.

2008_02_25_terrain.jpg

The game's enemies are coded within the entityAIFly.cpp and entityAIWalk.cpp files. Both the CEntityAIFly and CEntityAIWalk classes inherit from the CEntityAI class, which can be found in the entityAI.cpp file.  These "AI" entities are responsible for managing their own animations via sprite objects, and must also move themselves around the screen and check for collisions with the terrain.

2008_02_25_ai.jpg

The game's player entity is coded within the entityPlayer.cpp file. Similar to the other entities, the CEntityPlayer class maintains its own appearance via sprite objects.  However, the player has more animations to keep track of: Standing, running, jumping, and falling.  And, like the CEntityAI enemy classes, it must check for collisions with the terrain, and behave appropriately. However, the CEntityPlayer must do much more.  It receives input from the CGame class in the form of calls to its Run, Jump, and Drop methods.  This is how the CEntityPlayer class knows how the user at the keyboard wishes it to behave.  Additionally, the CEntityPlayer class must check for collisions with enemies and handle the "smoke" animation that occurs when the player is defeated.

2008_02_25_player.jpg

To recap the game logic:

Each time a frame is processed, CGame calls CEntity::UpdateAll() to allow all of the entities to run their respective "Update" methods and handle their sprites and collision detection and movement (if necessary).  CGame then checks for keyboard input and passes it to the CEntityPlayer object in the form of Run, Jump, and Drop method calls.  Additionally, CGame passes mouse click data to the CEntityToken class so that the various CEntityToken objects can respond properly if they have been clicked.  It is in this way that CGame facilitates the update and display of the game state, including the player, enemy, token, and terrain objects.




DDJ