UNPKG

@haelp/teto

Version:

A typescript-based controllable TETR.IO client.

217 lines (216 loc) 6.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Tetromino", { enumerable: true, get: function() { return Tetromino; } }); const _ = require(".."); const _kicks = require("../kicks"); const _data = _export_star(require("./data"), exports); _export_star(require("./types"), exports); function _export_star(from, to) { Object.keys(from).forEach(function(k) { if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { Object.defineProperty(to, k, { enumerable: true, get: function() { return from[k]; } }); } }); return from; } class Tetromino { #rotation; symbol; states; location; locking; lockResets; rotResets; safeLock; highestY; fallingRotations; totalRotations; irs; ihs; aox; aoy; keys; constructor(options){ this.rotation = options.initialRotation; this.symbol = options.symbol; const tetromino = _data.tetrominoes[this.symbol.toLowerCase()]; this.states = tetromino.matrix.data; this.location = [ Math.floor(options.boardWidth / 2 - tetromino.matrix.w / 2), options.boardHeight + 2.04 ]; // other stuff this.locking = 0; this.lockResets = 0; this.rotResets = 0; this.safeLock = options.from?.safeLock ?? 0; this.highestY = options.boardHeight + 2; this.fallingRotations = 0; this.totalRotations = 0; this.irs = options.from?.irs ?? 0; this.ihs = options.from?.ihs ?? false; this.aox = 0; this.aoy = 0; this.keys = 0; } get blocks() { return this.states[Math.min(this.rotation, this.states.length)]; } get absoluteBlocks() { return this.blocks.map((block)=>[ block[0] + this.location[0], -block[1] + this.y ]); } absoluteAt({ x = this.location[0], y = this.location[1], rotation = this.rotation }) { const currentState = [ this.location[0], this.location[1], this.rotation ]; this.location = [ x, y ]; this.rotation = rotation; const res = this.absoluteBlocks; this.location = [ currentState[0], currentState[1] ]; this.rotation = currentState[2]; return res; } get rotation() { return this.#rotation % 4; } set rotation(value) { this.#rotation = value % 4; } get x() { return this.location[0]; } set x(value) { this.location[0] = value; } get y() { return Math.floor(this.location[1]); } set y(value) { this.location[1] = value; } isStupidSpinPosition(board) { return !(0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0], -block[1] + this.y - 1 ]), board); } isAllSpinPosition(board) { return !(0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0] - 1, -block[1] + this.y ]), board) && !(0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0] + 1, -block[1] + this.y ]), board) && !(0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0], -block[1] + this.y + 1 ]), board) && !(0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0], -block[1] + this.y - 1 ]), board); } rotate(board, kickTable, amt, maxMovement) { const rotatedBlocks = this.states[(this.rotation + amt) % 4]; const kickRes = (0, _kicks.performKick)(kickTable, this.symbol, this.location, [ this.aox, this.aoy ], maxMovement, rotatedBlocks, this.rotation, (this.rotation + amt) % 4, board); if (typeof kickRes === "object") { this.location = [ ...kickRes.newLocation ]; } if (kickRes) { this.rotation = this.rotation + amt; return kickRes; } return false; } moveRight(board) { if ((0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0] + 1, -block[1] + this.y ]), board)) { this.location[0]++; return true; } return false; } moveLeft(board) { if ((0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0] - 1, -block[1] + this.y ]), board)) { this.location[0]--; return true; } return false; } dasRight(board) { if (this.moveRight(board)) { while(this.moveRight(board)){} return true; } return false; } dasLeft(board) { if (this.moveLeft(board)) { while(this.moveLeft(board)){} return true; } return false; } softDrop(board) { const start = this.location[1]; while((0, _kicks.legal)(this.blocks.map((block)=>[ block[0] + this.location[0], -block[1] + this.y - 1 ]), board)){ this.location[1]--; } return start !== this.location[1]; } snapshot() { return { aox: this.aox, aoy: this.aoy, fallingRotations: this.fallingRotations, highestY: this.highestY, ihs: this.ihs, irs: this.irs, keys: this.keys, rotation: this.rotation, location: (0, _.deepCopy)(this.location), locking: this.locking, lockResets: this.lockResets, rotResets: this.rotResets, safeLock: this.safeLock, symbol: this.symbol, totalRotations: this.totalRotations }; } } //# sourceMappingURL=index.js.map