UNPKG

blaze-2d

Version:

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

78 lines (77 loc) 3.08 kB
import { vec2 } from "gl-matrix"; import Camera from "../camera/camera"; import Object2D from "../object2d"; import Texture from "../texture/texture"; /** * Represents an arbitrary shape in 2D world space. * * A shape is self contained and includes functions to render itself. */ export default abstract class Shape extends Object2D { texture: Texture; constructor(); /** * Renders the shape using the {@link Renderer}. * * @param position The x and y position to render the shape at * @param rotation The rotation to apply to the rendered shape * @param zIndex The z position of the rendered shape */ render(position?: vec2, rotation?: number, zIndex?: number): void; /** * Calculates the shape's UV coords to be used for texture rendering. * * @returns the shape's UV coords */ abstract getUVCoords(): Float32Array; /** * Calculates the shape's vertices in local space. * * @returns The shape's vertices */ getVertices(): number[]; /** * Calculates the shape's vertices relative to the provided origin in world space. * * @param origin The origin to calculate the vertices relative to, should be a world position * @param rotation An optional world space rotation to apply to the vertices * @param returnVecs Wether or not to return the vertices as vec2s or raw numbers * @returns The shape's vertices relative to the provided origin in world space */ abstract getVerticesWorld(origin: vec2, rotation?: number, returnVecs?: boolean): number[] | vec2[]; /** * Calculates the shape's vertices relative to the provided origin in world space. * * @param origin The origin to calculate the vertices relative to, should be a world position * @param scale The vector to scale the world space vertices by to obtain clip space values * @param rotation An optional rotation to apply to the vertices * @param camera An optional camera to get vertices relative to * @returns The shape's vertices relative to the provided origin in clip space */ getVerticesClipSpace(origin: vec2, scale: vec2, rotation?: number, camera?: Camera): number[]; /** * Calculates the base vertices of the shape. * * These are the vertices for the type of shape at unit size, scaled to the shape's dimensions. * * @returns The shape's vertices */ abstract getBaseVertices(): vec2[]; /** * Gets the shape's vertex indices for drawing using element arrays. * * @param offset An offset to apply to each index * @returns The shape's vertex indices with the given offset added to each index */ abstract getIndices(offset?: number): Uint16Array; private cachedPoints; private posDiff; /** * Calculates the bounding points of the {@link Shape} instance. * * **NOTE: The shape's vertices are recalculated every time this function is called.** * * @returns The bounding points of the box */ getPoints(): vec2[]; }