UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

86 lines (68 loc) 2.12 kB
import { assert } from "../../../core/assert.js"; import { BinaryDataType } from "../../../core/binary/type/BinaryDataType.js"; import { DataType2TypedArrayConstructorMapping } from "../../../core/binary/type/DataType2TypedArrayConstructorMapping.js"; import Vector2 from "../../../core/geom/Vector2.js"; import { Sampler2D } from "../../../engine/graphics/texture/sampler/Sampler2D.js"; export class GridDataLayer { /** * Unique ID to reference the layer * @type {string} */ id = ""; /** * Relationship between size and number of cells in the sampler, higher resolution means more cells in the sampler * @type {number} */ resolution = 1; /** * * @type {Sampler2D} */ sampler = new Sampler2D([], 1, 0, 0); /** * * @type {Vector2} */ size = new Vector2(1, 1); /** * * @param {string} id * @param {BinaryDataType} type * @param {number} [resolution=1] * @returns {GridDataLayer} */ static from(id, type, resolution = 1) { assert.isString(id, 'id'); assert.enum(type, BinaryDataType, 'type'); assert.isNumber(resolution, 'resolution'); const TypedArrayConstructor = DataType2TypedArrayConstructorMapping[type]; if (TypedArrayConstructor === undefined) { throw Error(`No array constructor found for type '${type}'`); } const layer = new GridDataLayer(); layer.sampler.data = new TypedArrayConstructor(); layer.resolution = resolution; layer.id = id; return layer; } /** * * @param {number} x * @param {number} y */ resize(x, y) { if (this.size.x === x && this.size.y === y) { //do nothing, already right size return; } this.size.set(x, y); this.sampler.resize(x * this.resolution, y * this.resolution); } } /** * @readonly * @type {boolean} */ GridDataLayer.prototype.isGridDataLayer = true;