Code Overview

From TACWiki
Revision as of 10:30, 10 October 2007 by Zinsser (Talk | contribs) (Layout)

Jump to: navigation, search

This page contains a high-level overview of the code for the Autism Game.


XNA

This game is built on XNA, which is suite of classes written by Microsoft. It provides methods for importing and playing content, a basic program flow (detailed below), portability, and has a good online community of home brew game developers and hobbyists.


Layout

All code is broken into 2 main categories: Engine and Game. They are delineated in the code base into the namespaces "tAC_Engine" and "Colony_Simulator." The tAC_Engine code is completely generic, whereas the Colony_Simulator code is specific to the Autism Game. In other words, if someone were to make a similar game, they could use all the code from the tAC_Engine but very little of the code from the Colony_Simulator. The XNA framework is largely transparent, so developers can just treat XNA as part of tAC_Engine.

All content for the game lives in the "Content" directory. This includes fonts, images, sounds, and particle effects.

The BaseApplication classes (GenericBaseApplication and ColonyBaseApplication) facilitate communication between the main game, minigames, and forms by statically referring to each component.

There are other components used by the tAC_Engine, including text-drawing and particle effects. Those components are covered in the Code How To's.

Program Flow

An XNA game has 2 main cycles, Draw and Update. Both are called up to 60 times per second (usually). If the CPU load is high, XNA will automatically skip Draw cycles in an attempt to keep up with the Update cycles. This game has special requirements, so the Update cycle is actually called every ms and a new "Logic Update" cycle is called if 16.7 ms have passed since the last cycle. This is transparent to the minigames, so they can call Update and Draw as if they were part of a standard XNA game.

  • The entry point for the game is Program.Main() (located in ColonySimulator\Program.cs)
    • Main instantiates all the major components used in this game and assigns them to the ColonyBaseApplication
    • The game mode is set to the startup sequence
  • mainGame.Run() is called
    • This calls several initialization functions (Engine\GenericGameManager.cs)
    • The Update and Draw cycles start (GenericGameManager.Update()/.Draw())
  • GenericGameManager.Update() calls the appropriate main game or minigame's update based on the current mode (initially the startup sequence)
  • GenericGameManager.Draw() works the same way
  • There is a persistent game (the colony simulator) and also a "current" game
    • When the game mode is set to a minigame, the current game is that minigame
    • When the colony simulator mode is set, the current game is the colony simulator
    • The startup sequence mode and main menu mode are candidates for the current game
    • The colony simulator always exists
      • This allows minigames to interact with the simulator mdoe
      • Its Update and Draw cycles are only called while it is the current game
  • When a game is finished, it must set the appropriate mode by calling ColonyBaseApplication.switchMode(...)


Object Interactions

  • Each game is responsible for running and drawing itself (that is, a game must implement Update() and Draw())
    • This responsibility can be delegated to helper classes
  • Every game object should derive from Entity
  • Except for extremely specific circumstances, ALL objects or classes used in a game should derive from some class in the tAC_Engine namespace
    • If there is no suitable generic class, it may be necessary to create one and add it to the engine code
  • Use ColonyBaseApplication to communicate between games and with windows forms
    • See Code How To's for several examples of this, including checking time, screen dimensions, and sharing global data