UNPKG

blaze-2d

Version:

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

158 lines (157 loc) 5.08 kB
import { vec2 } from "gl-matrix"; import Camera from "./camera/camera"; import Entity from "./entity"; import { System } from "./system"; import TimeStep from "./timestep"; import Fluid from "./physics/fluid/fluid"; export declare type EntityListener = (event: "add" | "remove", entity: Entity, index: number, entities: Entity[]) => void; /** * Represents the 2D world. */ export default class World implements System { cellSize: vec2; useBatchRenderer: boolean; private camera; private entities; private fluids; /** * Callbacks which are fired whenever an entity is added or removed. */ private entityListeners; /** * Creates a {@link World} instance. * * @param cellSize The width and height of each world cell in pixels * @param cameraViewport The width and height of the camera's viewport in pixels */ constructor(cellSize: vec2, cameraViewport: vec2); /** * Creates a {@link World} instance with a dynamic viewport that resizes to the {@link Renderer}'s dimensions. * * @param cellSize The width and height of each world cell in pixels */ constructor(cellSize: vec2); /** * Updates the world's camera, terrain and entities. * * Also calls the render function. * * @param ts The {@link TimeStep} for this update */ update(ts: TimeStep): void; /** * Renders the entities in the world using the world's current camera. */ renderEntities(): void; /** * Renders the fluids in the world using the world's current camera. */ renderFluids(): void; /** * Calculates the world space to clip space scale. * * @returns The number that multiplying a world space coordinate by provides the equivalent clip space coordinate. */ getWorldtoClipSpace(): vec2; /** * Calculates the world space to pixel space scale. * * @returns The number that multiplying a world space coordinate by provides the equivalent pixel space coordinate. */ getWorldToPixelSpace(): vec2; /** * Calculates the pixel space to world space scale. * * @returns The number that multiplying a pixel space coordinate by provides the equivalent world space coordinate. */ getPixelToWorldSpace(): vec2; /** * Gets the world location of a pixel on the screen. * * This is calculated using the viewport of the world camera. * * @param pixel A pixel position on the screen */ getWorldFromPixel(pixel: vec2): vec2; /** * Gets the pixel coordinate of a world location. * * This is calculated using the viewport of the world camera. * * @param world A world position */ getPixelFromWorld(world: vec2): vec2; /** * Gets all entities currently in the world. * * @returns All entites in the world */ getEntities(): Entity[]; /** * Gets all entities with the given name. * * @param name The name of the entity */ getEntitiesByName(name: string): Entity[]; /** * Adds an entity to the world. * * @param entity The entity to add * @param fireListeners Wether or not to execute the world's entity listeners */ addEntity(entity: Entity, fireListeners?: boolean): void; /** * Adds the given entities to the world. * * By default entity listeners are fired for each entity added however, if the last argument * passed to this function is a boolean then it will be used to decide wether or not to fire entity listeners. * * @param entities The entities to add */ addEntities(...entities: (Entity | boolean)[]): void; /** * Removes an entity from the world. * * @param entity The entity to remove * @param fireListeners Wether or not to execute the world's entity listeners * @returns Wether or not the entity was removed */ removeEntity(entity: Entity, fireListeners?: boolean): boolean; private callEntityListeners; /** * Adds a callback which will be called whenever an entity is added or removed from the world. * * @param listener The listener to add */ addEntityListener(listener: EntityListener): void; /** * Removes an {@link EntityListener} from the world. * * @param listener The listener to remove */ removeEntityListener(listener: EntityListener): void; /** * Adds a fluid to the world. * * @param fluid The fluid to add */ addFluid(fluid: Fluid): void; /** * Removes a fluid from the world. * * @param fluid The fluid to remove */ removeFluid(fluid: Fluid): void; /** * Sets the camera to use for rendering. * * @param camera The camera to use for rendering */ useCamera(camera: Camera): void; /** * Gets the camera that is currently being used for rendering. * * @returns The camera that is currently being used for rendering */ getCamera(): Camera; }