Foundation.Game

The Game type is (as might be obvious) the primary game wrapper in Foundation. Game instances are simply OrderedUpdateDictionarys of Actor components (with some additional properties and methods). This structure allows for a fully customizable game loop as the "game loop" is simply a function which iterates over all actors in the collection and calls their update() method.

For convenience, a factory method is available (see below) which will create a game instance with a common set of components (time tracker, key, sound and touch controllers, a layer list, a renderer, etc.)


Example

// Create a prefab game object
var game = Foundation.Game.createDefaultPrefab(800, 400, document.body);

// Load assets
game.textures.push(new Foundation.Texture('someImage.jpg', 100, 100));

// Create objects etc.
game['layers']['default'].add(new Foundation.Tile(
  new Foundation.Rectangle(0, 0, 100, 100),
  game.textures[0].getRegion('all')
));

// Start the game when all textures have finished loading
game.autoStart();


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/_digest.ts'
  • '/foundation/core/Game.ts'

Extends

This type extends:

No base methods are overridden.

Implements

This type does not implement any interfaces (except those in the base).


Static Properties

Game.instance

  • A game singleton reference to the last created game instance. This is used by debugging types for logging and profiling, but is otherwise ignored for production code.

Public Properties

worldWidth: number

  • Defines the maximum width of the game world. Drawing outside of world bounds is not restricted (the world is technically infinite), but some optional inbuilt functionality will make use of this number (the camera, for example, can be restricted to these bounds).

worldHeight: number

  • Defines the maximum height of the game world. Drawing outside of world bounds is not restricted (the world is technically infinite), but some optional inbuilt functionality will make use of this number (the camera, for example, can be restricted to these bounds).

HTMLContainer: HTMLElement

  • The HTML element that is used as a container for the game (rendering canvases, screens, etc.) For full screen games, document.body is a good container. For non full screen games, a div is preferable. This div can be sized as needed via CSS, the game's view port will be scaled to fit.

textures: Texture[]

  • A convenience array to hold loaded textures. Textures can, of course, be stored in other places, but the autoStart() method (see below) will only take textures stored here into consideration. If storing textures elsewhere, a reference to each texture should also be added to this collection if using the autoStart() function.

logger: Logger

  • Used to hold a reference to a Logger instance. If non-null, game logging will be available through the game singleton.

profiler: TimeSlotProfiler

  • Used to hold a reference to a TimeSlotProfiler instance. If non-null, game time profiling will happen automatically on the game singleton.

stopped: boolean

  • Returns true if the game is not currently running
  • Note: not the same as the game being inactive. active === false may mean the game is paused, this === false means the game loop is NOT running.

Constructors

(worldWidth: number = 800, worldHeight: number = 480, HTMLContainer: HTMLElement = document.body, log: boolean = false, profile: boolean = false)

  • Creates a new game instance and sets it as the game singleton.
  • worldWidth See property description. Defaults to 800.
  • worldHeight See property description. Defaults to 480.
  • HTMLContainer See property description. Defaults to document.body
  • log Pass true to add a Logger instance to the game. Defaults to false.
  • profile Pass true to add a TimeSlotProfiler instance to the game. Defaults to false.

start(callback?: Function)

  • Starts the game loop running.
  • A callback is not strictly necessary here as the game will be immediately (synchronously) started. However, a callback here will be called with it's this context being the game, which may be of some use.

autoStart(callback?: Function): void

  • Calls start() when, and only when, all textures stored in [gameInstance].textures have ready ready state. I.e. will start the game when all textures are ready to draw.

pause(): void

  • Pauses update calls in the game loop. The game loop will continue to run (game life time will accumulate), but no actors will be updated.
  • The game can be resumed with a start() call.

stop(): void

  • Like pause, but fully stops the game loop.

Game Prefab Factory

createDefaultPrefab(worldWidth: number = 800, worldHeight: number = 480, HTMLContainer: HTMLElement = document.body, log: boolean = false,profile: boolean = false, load: Function = null)

  • Creates a game instance with a predefined set of components. The following components are available on the returned instance, in order:
  • The load parameter should now be considered deprecated (>= 0.8.1). This function will create and return a game instance synchronously. The object returned is a DefaultGamePrefab instance. This is simply a Game subclass with typed getters for the above components.