UNPKG

cli-artist

Version:
122 lines (107 loc) 2.66 kB
const engine = require('../engine'); engine.debug.enabled(true); class block { constructor(x) { this.y = 10; this.x = x; this.tiles = []; this.placed = 0; this.generateTiles(); this.prevRotation = 0; this.rotation = 0; } generateTiles() { let candidates = [ [ [1, 1, 1, 1] ], [ [1, 1], [1, 1] ], [ [1, 1, 1], [0, 1, 0] ], [ [0, 1, 1], [0, 1, 0], [1, 1, 0] ], [ [0, 1, 1], [1, 1, 0] ], [ [0, 1], [0, 1], [1, 1] ], [ [1, 0], [1, 0], [1, 1] ] ] this.tiles = candidates[Math.floor(Math.random() * candidates.length)] } show() { this.tiles.forEach((t, index) => { t.forEach((c, i) => { if (c) engine.drawRect(this.x + i * 2, this.y + index, 2, 1, "o") }) }) } update() { // this.y++; this.y = engine.constrain(this.y, 0, engine.height - this.tiles.length) } rotate(dir) { this.prevRotation = this.rotation; this.rotation += dir; this.rotation = engine.loop(this.rotation, 0, 4) } angle() { let newArr = [] for (let i in this.tiles) { newArr[i] = [] for (let j in this.tiles[i]) { newArr[i][j] = this.tiles[j][i] } } this.tiles = newArr } } let blocks = []; const setup = () => { blocks.push(new block(engine.width / 2)); } const draw = () => { engine.fillBackground('blue') engine.fillForeground('blue'); blocks.forEach(b => { b.show(); b.update(); }) } const keyPressed = key => { switch (key) { case "w": break; case "a": blocks[blocks.length - 1].rotate(-1) break; case "s": break; case "d": blocks[blocks.length - 1].rotate(1) break; case "q": blocks[blocks.length - 1].x -= 2; break; case "e": blocks[blocks.length - 1].x += 2; break; } } engine.init(setup, draw, keyPressed, 5);