UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

224 lines (223 loc) 7.18 kB
import Color, { ColorLike } from "./utils/color"; import ThreadPool from "./threading/threadPool"; import { System } from "./system"; import Editor from "./editor/editor"; import Scene from "./scene"; import BlazeElement from "./ui/element"; import World from "./world"; import "./ui/styles/root.css"; import "./ui/styles/canvas.css"; import TimeStep from "./timestep"; import RenderController from "./renderer/controller"; export interface BlazeOptions { antialias: boolean; } /** * Represents the Blaze engine. */ export default abstract class Blaze { /** * The webgl canvas used by blaze. */ private static canvas; /** * The color used to clear the {@link Renderer}. */ private static bgColor; /** * This value represents the number of Z positions that can be used within the world space. * * Allows the user to specify zIndexes as integer values (-1, 1, 2, 3) that are scaled into a -1.0 - 1.0 range. * * The higher this value the more zIndexes the camera will be able to see. */ private static zLevels; static renderController: RenderController; private static systems; private static fixedSystems; private static threadPool; /** * The {@link TimeStep} for normal updates. */ private static timeStep; /** * The minimum time in ms between each fixed update. */ static fixedDt: number; /** * The maximum fixed delta time step in ms. * * If the time since the last fixed update exceeds this amount then it will be clamped. */ static maxFixedDt: number; /** * The {@link TimeStep} for fixed updates. */ private static fixedTimeStep; /** * The current {@link Scene}. */ private static scene; /** * The editor. */ static editor: Editor; /** * Initializes the engine and creates the renderer. * * @param canvas The canvas to use to create the renderer * @param opts The options to use when setting up the engine */ static init(canvas: HTMLCanvasElement, opts?: BlazeOptions): void; /** * Starts Blaze's update/render loop. * * i.e. calls `this.update` */ static start(): void; /** * Blaze's update loop. * * Update is called on every animation frame using [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame). * * Update clears the {@link Renderer} with the set background color and calls `update` on any systems in `this.systems`. * * Update also updates the engine's current {@link Scene}. * * At the end of the update the {@link Renderer} queue is flushed, drawing anything that was queued for rendering during the frame. */ static update(): void; /** * Blaze's fixed update loop. * * Fixed update is called every `this.fixedTimeStep`ms using [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout), * due to how the JS event loop works and any other tasks being ran on the main thread this may not always be the case. * * To account for the possible variations in time between each update a seperate delta time value is calculated for the fixed update loop. * * Fixed update calls `update` on all systems in `this.fixedSystems`. * * Fixed update also steps the {@link Physics} engine. */ static fixedUpdate(): void; /** * Updates the engine's timestep to the current time. * * @param fixed Wether or not to set the fixed time step */ private static nextTimestep; /** * Gets the systems added to the engine. * * @returns The engine's top level systems */ static getSystems(): System[]; /** * Gets the fixed systems added to the engine. * * @returns The engine's fixed systems */ static getFixedSystems(): System[]; /** * Adds a system to the engine. * * @param system The system to add * @param fixed Wether the system will run in the fixed update or normal update loop */ static addSystem(system: System, fixed?: boolean): void; /** * Removes a system from the engine. * * Will search for the system in both the fixed and normal system arrays. * * If the system is in both the fixed and normal system arrays then it will only * be removed from the normal array. Another call to removeSystem will remove it * from the fixed array. * * @param system The system to remove * @returns Wether or not the system was removed */ static removeSystem(system: System): boolean; /** * Gets the canvas used by blaze. * * @returns The {@link BlazeElement} for blaze's canvas */ static getCanvas(): BlazeElement<HTMLCanvasElement>; /** * Sets the engine's scene. * * @param scene The scene to use */ static setScene(scene: Scene): void; /** * Gets the engine's scene. * * @returns The engine's scene */ static getScene(): Scene; /** * Gets the current scene's {@link World}. * * @returns The current scene's world */ static getWorld(): World; /** * Gets the current scene's {@link Physics} world. * * @returns THe current scene's physics world */ static getPhysics(): import("./physics/physics").default; /** * Sets the clear color to be used when clearing the webgl buffer, mimics having a bg color. * * @param color The {@link Color} instance to set the engine's bg color to * @returns The set {@link Color} instance */ static setBgColor(color: Color): Color; /** * Sets the clear color to be used when clearing the webgl buffer, mimics having a bg color. * * @param color The {@link ColorLike} representation to use when instantiating the bg color * @returns The set {@link Color} instance */ static setBgColor(color: ColorLike): Color; /** * Gets the engine's current bg color. * * @returns The engine's current bg color */ static getBgColor(): Color; /** * Sets the amount of zLevels the engine (physics, world, renderer) will use. * * @throws When zLevels is < 0 or zLevels is a floating point number. * * @param zLevels The number to set zLevels to */ static setZLevels(zLevels: number): void; /** * Gets the current number of zLevels the engine is set to support. * * @returns The number of zLevels for the engine */ static getZLevels(): number; /** * Gets the {@link TimeStep} between each update. * * @returns The most recent fixed time step */ static getTimeStep(): TimeStep; /** * Gets the {@link TimeStep} between each fixed update. * * @returns The most recent fixed time step */ static getFixedTimeStep(): TimeStep; /** * Gets the current thread pool being used. * * @returns The thread pool in use by the engine */ static getThreadPool(): ThreadPool; }