UNPKG

spaceship-sprites

Version:

Spaceship Sprite is a random ship sprite generator in typescript.

135 lines (134 loc) 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const builder_1 = require("./builder"); const color_1 = require("./color"); const validator_1 = require("./validator"); class Sprite { constructor({ dim, array, pallet, horizontalSymmetry, colorFill }) { validator_1.default.positiveInteger(dim[0], 'dim.x'); validator_1.default.positiveInteger(dim[1], 'dim.y'); let color = colorFill === undefined ? new color_1.default(0, 0, 0, 1) : colorFill; let arr = array || new Array(dim[0] * dim[1]).fill(null).map(() => color.copy()); if (dim[0] * dim[1] != arr.length) { throw new Error(`Invalid array dimensions [${dim[0]}, ${dim[1]}] for array of length ${arr.length}`); } this._dim = dim; this._array = arr; this._pallet = Sprite.trimPallet(pallet || new Array()); this._horizontalSymmetry = horizontalSymmetry || false; } get dim() { return [...this._dim]; } get array() { return [...this._array].map(color => color.copy()); } get pallet() { return [...this._pallet].map(color => color.copy()); } get horizontalSymmetry() { return this._horizontalSymmetry; } copy() { return new Sprite({ dim: this.dim, array: this.array, pallet: this.pallet, horizontalSymmetry: this.horizontalSymmetry }); } arrayValues() { return [...this._array].map(color => color.toArray()); } matrix() { return new Array(this.dim[0]) .fill(null) .map((_, i) => new Array(this.dim[1]) .fill(null) .map((_, j) => this.pixelAt(i, j).toArray())); } svgWidth(width, unit = 'px', parameters = '') { let w = width + this._dim[0] - (width % this._dim[0]); let h = (this._dim[1] / this._dim[0]) * w; return this.svgExact(w, h, unit, parameters); } svgHeight(height, unit = 'px', parameters = '') { let h = height + this._dim[1] - (height % this._dim[1]); let w = (this._dim[0] / this._dim[1]) * h; return this.svgExact(w, h, unit, parameters); } svg(width, height, unit = 'px', parameters = '') { let w = width + this._dim[0] - (width % this._dim[0]); let h = height + this._dim[1] - (height % this._dim[1]); return this.svgExact(w, h, unit, parameters); } svgExact(width, height, unit = 'px', parameters = '') { let result = `<svg ${parameters} width="${width}${unit}" height="${height}${unit}" viewBox="0, 0, ${this.dim[0]}, ${this.dim[1]}">`; for (let x = 0; x < this._dim[0]; x++) { for (let y = 0; y < this.dim[1]; y++) { let rgba = this.pixelAt(x, y).toRgba(); result += `<rect width="1" height="1" x="${x}" y="${y}" style="fill:${rgba};" />`; } } return result + '</svg>'; } svgScale(pixelSize, unit = 'px', parameters = '') { let width = this._dim[0] * pixelSize; let height = this._dim[1] * pixelSize; return this.svgExact(width, height, unit, parameters); } data() { let result = new Uint32Array(this._array.length); this._array.forEach((value, index) => { result[index] = value.toInt(); }); return result; } bytes() { let result = new Uint8Array(this._array.length * 4); for (let i = 0, j = 0; i < this._array.length; i += 1, j += 4) { let color = this._array[i]; result[j + 0] = color.alphaByte; result[j + 1] = color.redByte; result[j + 2] = color.greenByte; result[j + 3] = color.greenByte; } return result; } setPixelAt(x, y, color) { this.checkIndex(x, y); this._array[y * this._dim[0] + x] = color.copy(); } pixelAt(x, y) { this.checkIndex(x, y); return this._array[y * this._dim[0] + x].copy(); } pixelAtChecked(x, y) { if (!this.withinBounds(x, y)) return undefined; return this._array[y * this._dim[0] + x].copy(); } setPixelAtChecked(x, y, color) { if (!this.withinBounds(x, y)) return false; this._array[y * this._dim[0] + x] = color.copy(); return true; } static builder() { return new builder_1.default({}); } static trimPallet(pallet) { pallet = pallet.filter(value => value !== color_1.default.BLACK); return pallet; } checkIndex(x, y) { if (!this.withinBounds(x, y)) { throw new Error(`Index out of bounds [${x}, ${y}] when actual dimension is [${this.dim[0]}, ${this.dim[1]}]`); } } withinBounds(x, y) { return x >= 0 && x < this._dim[0] && y >= 0 && y < this._dim[1]; } } exports.default = Sprite;