UNPKG

blaze-2d

Version:

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

107 lines 2.83 kB
import Logger from "./logger"; import RigidBody from "./physics/rigidbody"; import validateZIndex from "./utils/validators"; /** * Represents a generic entity in 3D space. */ export default class Entity extends RigidBody { /** * Creates a new {@link Entity} instance with a position, bounding box and body pieces. * * @param position The entity's position in world space * @param collider The entity's bounding box * @param pieces The entity's body pieces for rendering * @param mass The entity's mass in kg (0 for infinite mass) * @param name A name which can be used to identify the entity */ constructor(position, collider, pieces = [], mass, name = "") { super(collider, mass); this.name = ""; this.zIndex = 0; this.setPosition(position); this.pieces = pieces; this.name = name; } /** * Sets the events that can be attached to in `listeners`. */ setupEvents() { super.setupEvents(); this.listeners.update = []; } /** * Updates the entity's physics and bounds. * * @param delta Time since last tick */ update(delta) { this.fireEvent("update", delta || 0); } /** * Renders the entity's pieces. */ render() { for (const p of this.pieces) { p.render(this.getPosition(), this.getRotation(), this.zIndex); } } /** * Sets the entity's body pieces. * * @param pieces The entity's new pieces */ setPieces(pieces) { this.pieces = pieces; } /** * Gets the entity's body pieces. * * @returns The entity's pieces */ getPieces() { return this.pieces; } /** * Adds a body piece to the entity. * * @param piece The piece to add to the entity */ addPiece(piece) { this.pieces.push(piece); } /** * Removes a body piece from the entity. * * @param piece The piece to remove from the entity * @returns Wether or not the piece was removed */ removePiece(piece) { const i = this.pieces.findIndex((p) => p === piece); if (i === -1) return false; this.pieces.splice(i, 1); return true; } /** * Sets the entites z index. * * @throws When {@link validateZIndex} returns a string. * * @param zIndex The entites new zIndex */ setZIndex(zIndex) { const valid = validateZIndex(zIndex); if (valid !== true) Logger.error("Entity", valid); this.zIndex = zIndex; } /** * Gets the entities z index. * * @returns The entities z index */ getZIndex() { return this.zIndex; } } //# sourceMappingURL=entity.js.map