UNPKG

blaze-2d

Version:

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

77 lines (76 loc) 2.67 kB
import { vec2 } from "gl-matrix"; import Shape from "./shape"; /** * Represents a triangle in 2D space with a width and height. */ export default class Triangle extends Shape { private height; private width; /** * Creates a new {@link Triangle} instance with dimensions, position and rotation. * * @param width The width of the triangle (x size) * @param height The height of the triangle (y size) * @param position The triangle's position in world space, default is [0, 0] * @param rotation The triangle's rotation, default is 0. */ constructor(width: number, height: number, position?: vec2, rotation?: number); /** * Calculates the triangle'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 triangle's vertices relative to the provided origin in world space */ getVerticesWorld(origin: vec2, rotation?: number, returnVecs?: boolean): number[] | vec2[]; /** * Calculates the base vertices of the triangle. * * These are the vertices for a triangle scaled to the width and height of the {@link Triangle} instance. * * @returns The triangles base vertices */ getBaseVertices(): vec2[]; /** * Gets the triangle's vertex indices for drawing using element arrays. * * @param offset An offset to apply to each index * @returns The triangle's vertex indices with the given offset added to each index */ getIndices(offset?: number): Uint16Array; /** * Calculates the triangles's UV coords to be used for texture rendering. * * @returns the triangle's UV coords */ getUVCoords(): Float32Array; /** * Sets the triangle's width. * * @throws When the the provided width is < 0 * * @param width The triangle's new width */ setWidth(width: number): void; /** * Gets the triangle's width in world space units. * * @returns The triangle's width in world space */ getWidth(): number; /** * Sets the triangle's height. * * @throws When the the provided height is < 0 * * @param height The triangle's new height */ setHeight(height: number): void; /** * Gets the triangle's height in world space units. * * @returns The triangle's height in world space */ getHeight(): number; }