UNPKG

@fuwu-yuan/bgew

Version:

Baguette Game Engine Web is a french 2D Web game engine

240 lines (239 loc) 8.23 kB
import { Entity } from "./entity"; import { GameStep } from "./gamestep"; import { BoardConfig } from "./config"; import { AbstractNetworkManager } from "./network/networkmanager.abstract"; import { Debug } from "../classes/Debug"; import { Timer } from "./timer"; import { Collisions, Result } from "detect-collisions"; import { Sound } from "./sound"; /** * The borad is the main part of your Game */ export declare class Board { private _canvas; entities: Entity[]; private readonly _ctx; private gameloop; private gameloopId; private readonly _config; private readonly defaultStrokeStyle; private readonly defaultFillStyle; private steps; private _step; private _networkManager; private _name; private _version; private _lastCursor; private _debug; private dispatcher; private _scale; private _gameHTMLElement; private _gravity; private _collisionSystem; private _collisionResult; private _sounds; private _events; private _paused; constructor(name: string, version: string, width: number, height: number, gameElement?: HTMLElement | null, background?: string, enableHIDPI?: boolean, enableCollisionSystem?: boolean); private createCanvasElem; countEntities(): number; /** * Start the game loop (update and draw entities) */ start(): void; /** * Stop the game loop (game will freeze) */ stop(): void; /** * Add a step in step list (you need to add your game step before calling moveToStep(step)) * @param step */ addStep(step: GameStep): void; /** * Add an array of steps (you need to add your game step before calling moveToStep(step)) * @param steps */ addSteps(steps: GameStep[]): void; get paused(): boolean; set paused(paused: boolean); pause(): void; resume(): void; get version(): string; set version(value: string); get name(): string; set name(value: string); /** * Get Network Manager */ get networkManager(): AbstractNetworkManager; set networkManager(networkManager: AbstractNetworkManager); get canvas(): HTMLCanvasElement; set canvas(value: HTMLCanvasElement); /** * Get the current step */ get step(): GameStep; /** * Set the current step (no transitions function is called (onEnter / onLeave)) * * @param step */ set step(step: GameStep); /** * The the game config */ get config(): BoardConfig; /** * Change the game step (will call onLeave of current step, and onEnter on new step) * @param step * @param data * @param fade */ moveToStep(step: string, data?: any, fade?: { color: string; duration: number; }): void; /** * Add an entity to the board. * Entity will be updated and drawn immediately (calling update() and draw() from entity) * * @param entity */ addEntity(entity: Entity): void; /** * Add multiple entities * This function will call addEntity(entity) for each entity of the array * @param entities */ addEntities(entities: Entity[]): void; /** * Find an entity by id * @param id * @param recursive search recursively in entity that can contains other entities (like Entities.Container) * @return Entity found entity with the given id or undefined if not found */ findEntity(id: string, recursive?: boolean): Entity | undefined; /** * Remove the entity from board. * The entity will disappear from the game * @param entity */ removeEntity(entity: Entity): void; /** * Remove all the entities from board. * The entities will disappear from the game * @param entities */ removeEntities(entities: Entity[]): void; /** * Change the cursor * @param cursor */ changeCursor(cursor: "auto" | "inherit" | "crosshair" | "default" | "help" | "move" | "pointer" | "progress" | "text" | "wait" | "e-resize" | "ne-resize" | "nw-resize" | "n-resize" | "se-resize" | "sw-resize" | "s-resize" | "w-resize" | "none" | "context-menu" | "cell" | "vertical-text" | "alias" | "copy" | "no-drop" | "not-allowed" | "ew-resize" | "ns-resize" | "nesw-resize" | "nwse-resize" | "col-resize" | "row-resize" | "all-scroll" | "grab" | "grabbing" | "zoom-in" | "zoom-out" | string): void; restoreCursor(): void; /** * Register a sound to play it later using function {@link playSound} * @param name Sound name * @param src One or more src (same sound with multiple src formats) * @param repeat Ether to repeat of not the sound at the end * @param volume Volume to play sound by default (default 0.5) * @param sprites Object defining sprites in this sound (optional) * @return Sound the newly created sound */ registerSound(name: string, src: string | string[], repeat?: boolean, volume?: number, sprites?: { [key: string]: [number, number]; }): Sound; getSound(name: string): Sound | null; /** * Play a sound previously registered using function {@link registerSound} * @param name the registered sound name * @param repeat play the sound again at the end * @param volume volume to play the sound (between 0.0 and 1.0) * @param sprite the sprite to play */ playSound(name: string, repeat?: boolean, volume?: number, sprite?: string): number | null; /** * Stop the sound * * @param name Sound name * @param fadeout Ether to fadeout the sound before stopping it * @param fadeDuration Fadeout duration * @param id Optional sound id */ stopSound(name: string, fadeout?: boolean, fadeDuration?: number, id?: number): void; /** * Remove all entities and clear the board */ reset(): void; /** * Clear the board (will be drawn again on next game loop) */ clear(): void; /** * Get the CanvasRenderingContext2D to draw */ get ctx(): CanvasRenderingContext2D; /** * Reset all styles (stroke and fill) */ resetStyles(): void; /** * On mouse event, catch it and dispatch right event to entities * * @private */ private dispatchMouseEvents; /** * On keyboard event, catch it and dispatch right event to entities * * @private */ private dispatchKeyboardEvents; /** * Listen mouse event on this entity * @param event An event from this list : click, dblclick, contextmenu, mousedown, mouseup, mouseenter, mouseleave, mousemove, all * @param callback */ onMouseEvent(event: "click" | "dblclick" | "contextmenu" | "mousedown" | "mouseup" | "mouseenter" | "mouseleave" | "mousemove" | "all", callback: (event: MouseEvent, x: number, y: number) => void): void; /** * Listen mouse event on this entity * @param event An event from this list : keyup, keydown, keypress, all * @param callback */ onKeyboardEvent(event: "keyup" | "keydown" | "keypress" | "all", callback: (event: KeyboardEvent) => void): void; get width(): number; get height(): number; set width(value: number); set height(value: number); get scale(): number; set scale(value: number); get gravity(): number; set gravity(value: number); get debug(): Debug; get gameHTMLElement(): HTMLElement; getEntitiesAt(x?: number | null, y?: number | null): Entity[]; getEntitiesIn(x1: number, y1: number, x2: number, y2: number): Entity[]; getPixelRatio(): number; /** * Shortcut to add timer to current GameStep {@link GameStep.addTimer} * @param time * @param end * @param repeat */ addTimer(time: number, end: () => void, repeat?: boolean): Timer; /** * Shortcut to remove timer from current GameStep {@link GameStep.removeTimer} * @param timer */ removeTimer(timer: Timer): void; get collisionSystem(): Collisions | null; get collisionResult(): Result | null; private saveEventForLater; /** * Init all events from the board * * @private */ private initEvents; }