UNPKG

evidently-pixi

Version:

TypeScript powered set of modules to make it easier to make games in Pixi.js.

168 lines (167 loc) 6.48 kB
import * as PIXI from "pixi.js"; import { AssetLoader } from "../Various/AssetLoader"; import { TextureStore } from "../Various/TextureStore"; import { Scene, SceneManager } from "../Managers/SceneManager"; import { PostProcessEffects } from "../Managers/PostProcessEffects"; import { GameStageLayer, GameStage } from "../Stages/GameStage"; import { KeyboardInput } from "evidently-input/dist/KeyboardInput"; import { MouseInput } from "evidently-input/dist/MouseInput"; /** * Configuration for initializing and running the game */ export interface GameConfiguration { /** * PIXI configuration */ pixiConfig: PIXI.ApplicationOptions; /** * The `document` is required to hook and handle input */ document: Document; /** * The `window` is required to handle window resizing events */ window: Window; /** * ID of the HTML element where PIXI should be put into */ gameContainerId: string; /** * Factory function to create the [[GameStage]] to use for your game * @param {Game} game The instance of the game that's being initialized */ stageFactory: (game: Game) => GameStage; /** * Factory function to create the initial [[Scene]]. * @param {Game} game The instance of the game that's being initialized */ initialSceneFactory: (game: Game) => Scene; /** * Allows setting up custom loader screen. * @param {Game} game The instance of the game that's being initialized */ onSetupLoadingScreen?: (game: Game) => any | void; /** * Allows removing the custom loader screen. * @param {Game} game The instance of the game that's being initialized */ onRemoveLoadingScreen?: (game: Game) => any | void; /** * Function to queue assets using Game's `assetLoader` - see [[AssetLoader]] for more details. * @param {Game} game The instance of the game that's being initialized */ onQueueAssets: (game: Game) => any | void; /** * Called after everything has finished initializing. * @param {Game} game The instance of the game that's being initialized */ onStartGame?: (game: Game) => any | void; } /** * The bootstrap and entrypoint for a PIXI game. It does the following things in order: * 1. Creates a Pixi instance inside the element specified in `config.gameContainerId`. * 2. Register mouse and keyboard listeners to grab input. * 3. Setups loading screen. * 4. Calls a custom callback to allow you to queue all the assets you need for loading. * 5. Loads all the queued assets. * 6. Removes loading screen. * 7. Initializes the first scene (like a screen of your game: main menu, game itself, game over, leaderboards, etc) * 8. Connects to the ticker that updates the active scene. * 9. Adds all other necessary listeners - changing game dimensions, updating input etc. * * ### Example configuration * * ``` * const game = new Game({ * // `document` and `window` are required to be passed to the configuration * document, * window, * * // ID of the element where the PIXI's canvas should be added * gameContainerId: 'game', * * // optional PIXI configuration, at the very least you want to provide width and height * pixiConfig: { * width: 640, * height: 360, * backgroundColor: 0, * antialias: false, * }, * * // Creates and configures a stage which scales the game pixel-perfect, snapping to full integers of scale, * // ie. x1, x2, x3, for a crisp pixel look. It also handles all of the magic required for Pixi's * // interactions to work and for `MouseInput` library to report proper position * stageFactory: (game: Game) => new ScalingStage( * game, * 640, * 360, * PIXI.SCALE_MODES.NEAREST, * Config.ScalingStageUpscaleMode, * ), * * // Queue some assets to be loaded * onQueueAssets: (game: Game): void { * game.assetLoader.queuePixiAutoFont('topaz_0.png', FontTopaz8Image); * game.assetLoader.queuePixiAutoFont('font-topaz', FontTopaz8); * game.assetLoader.queueTexture(GfxConstants.InitialTileset, InitialTileset); * game.assetLoader.queueTileset(GfxConstants.InitialTileset, { * tileWidth: 16, * tileHeight: 16, * offsetX: 0, * offsetY: 0, * spacingX: 0, * spacingY: 0, * }) * }, * * // Callback to create the starting scene * initialSceneFactory: (game) => new IntroScene(game), * * // Called once everything has finished initializing * onStartGame: () => { * console.log("Game started"); * }, * ); * * // Actually starts the initialization of the game * game.start(); * ``` */ export declare class Game { private readonly _config; readonly pixi: PIXI.Application; readonly document: Document; readonly keyboard: KeyboardInput; readonly mouse: MouseInput; readonly gameStage: GameStage; readonly assetLoader: AssetLoader; readonly textureStore: TextureStore; readonly sceneManager: SceneManager; readonly postProcessManager: PostProcessEffects; constructor(config: GameConfiguration); /** * Initializes and runs the game. */ start(): void; /** * Cretes a new PIXI.Sprite on the specified layer. This new sprite will be attached to the * [[GameStage]], and can be used to actually display something on the screen. * See [[GameStageLayer]] for more information about the types of layers. * @param {GameStageLayer} layer Layer on which to create the container. * @return {PIXI.Sprite} The sprite created and attached to stage. */ createContainer(layer?: GameStageLayer): PIXI.Sprite; /** * Removes a container previously created by the call to `createContainer` from the stage. Use * it when it's no longer in use, typicall when switching from a [[Scene]]. * @param {PIXI.Sprite} container The contianer to remove */ removeContainer(container: PIXI.Sprite): void; private update; private setupDefaultLoadingScreen; private removeDefaultLoadingScreen; private loadAssets; private initializeScene; private finalSetup; private onWindowResize; }