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.bodyis 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 theautoStart()function.
logger: Logger
- Used to hold a reference to a
Loggerinstance. If non-null, game logging will be available through the game singleton.
profiler: TimeSlotProfiler
- Used to hold a reference to a
TimeSlotProfilerinstance. 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 === falsemay mean the game is paused, this=== falsemeans 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.
worldWidthSee property description. Defaults to800.worldHeightSee property description. Defaults to480.HTMLContainerSee property description. Defaults todocument.bodylogPasstrueto add aLoggerinstance to the game. Defaults tofalse.profilePasstrueto add aTimeSlotProfilerinstance to the game. Defaults tofalse.
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
thiscontext being the game, which may be of some use.
autoStart(callback?: Function): void
- Calls
start()when, and only when, all textures stored in[gameInstance].textureshave readyreadystate. 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:
time: AGameTimeTrackerinstance.keys: AKeyControllerinstance.camera: ACamerainstance.touch: ATouchControllerinstance.sound: ASoundControllerinstance.layers: AnOrderedUpdateDictionaryofLayerinstances. Only 1 layer is added with the keydefault.renderer: ACanvasRenderer2Dinstance.debugDrawer: ASpriteProfileRendererinstance.active === falseby default
- The
loadparameter should now be considered deprecated (>= 0.8.1). This function will create and return a game instance synchronously. The object returned is aDefaultGamePrefabinstance. This is simply aGamesubclass with typed getters for the above components.