March 5, 2014

High Level Design: The Engine Core and Scenes

Engine Core

The engine core provides the starting point for all game functions. The engine core is primarily concerned with keeping track of the game’s state (in the form of scenes) and running the game loop. No game logic is included in the core; instead, it continues to run the loop until it receives instructions to change the active scene.

The engine core’s initialization method takes a UIView object as a parameter. This is the view onto which the game will be rendered. The first step in creating a game on this engine is to create a UIView (often by means of a UIViewController) and pass it into an instance of the Engine class. That view will only have a single child view at a time: the active scene’s view. When the gotoScene method of the engine core is called, the old scene’s view is removed from the root view, and the new scene’s view is added to it.

Scenes

Although the engine is what kicks everything off, the scene is where a programmer can start implementing his own logic. The engine provides a scene template that can be subclassed by developers to use when creating games. The subclassed scene is responsible for creating and adding to itself game objects (entities) and systems.

Scenes are not a standard piece of the entity component system architecture, so one might ask what motivation exists for including them in a game engine. It is assumed that every game is composed of a series of states. For example, consider a game that starts with a menu state. When a user provides input to start the game, the game transitions to a playing state. The playing state remains active until the game ends, either because the user lost, beat the game, or chose to quit from a pause menu. At this point, the game transitions back to the initial menu state. The motivation for scenes, then, is that because games can be broken down into states, the framework behind a game should facilitate that.

All scene transitions are handled within the active scene. When the active scene detects that a scene transition is needed, because the player selected “start game” from the menu, for example, or the player got a game over, it creates a new instance of the next scene and passes it to the engine core’s gotoScene method. The engine core then replaces the active scene and allows the previous scene to be released from memory.

Scenes are made up of systems and entities. The initialization method of the scene is made up of two parts. First, all of the systems required by the scene are added to the list of systems. Second, entities are added to the list of entities. Systems are aware of the kinds of entities over which it has control, as will be discussed in a future post on systems, so as each entity is added to the scene, it is also added to any and all systems that can govern it. Once the scene has been activated, no additional systems may be added to it. However, entities may be added or removed as needed.

Posted by Luke Godfrey

Luke Godfrey is a graduate student at the University of Arkansas, pursuing a PhD in Computer Science. He has created applications on a number of platforms, and is especially interested in mobile and web development.

View more posts from this author

Leave a Reply

Your email address will not be published. Required fields are marked *