snake-cli-ts
Version:
Nodejs cli snake game
189 lines (188 loc) • 6.72 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Direction = exports.Vector = void 0;
var InputController_1 = __importDefault(require("../helpers/InputController"));
var Snake_1 = __importDefault(require("./Snake"));
var Printer_1 = __importDefault(require("./Printer"));
var FoodManager_1 = __importDefault(require("./FoodManager"));
var events_1 = require("events");
var Vector;
(function (Vector) {
Vector["Up"] = "up";
Vector["Down"] = "down";
Vector["Left"] = "left";
Vector["Right"] = "right";
})(Vector = exports.Vector || (exports.Vector = {}));
var Direction;
(function (Direction) {
Direction["Horizontal"] = "horizontal";
Direction["Vertical"] = "vertical";
})(Direction = exports.Direction || (exports.Direction = {}));
var keyBindings = {
right: [1, 0],
left: [-1, 0],
up: [0, 1],
down: [0, -1],
};
var SnakeGame = (function (_super) {
__extends(SnakeGame, _super);
function SnakeGame(config) {
if (config === void 0) { config = {}; }
var _this = _super.call(this) || this;
_this.gameOvered = false;
_this.interval = null;
_this.printer = new Printer_1.default(_this);
_this.config = Object.assign({
moveThroughWall: true,
difficulty: 'easy',
}, config);
_this.snake = new Snake_1.default(_this);
_this.foodManager = new FoodManager_1.default(_this);
return _this;
}
Object.defineProperty(SnakeGame.prototype, "width", {
get: function () {
return Math.floor(process.stdout.columns / 2) - 2;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SnakeGame.prototype, "height", {
get: function () {
return Math.floor(process.stdout.rows / 1) - 2;
},
enumerable: false,
configurable: true
});
SnakeGame.prototype.set = function (key, value) {
return (this.config[key] = value);
};
SnakeGame.prototype.get = function (key) {
return this.config[key];
};
SnakeGame.prototype.isGameOver = function () {
return this.gameOvered;
};
SnakeGame.prototype.isNotStarted = function () {
return this.snake.dx === 0 && this.snake.dy === 0;
};
SnakeGame.prototype.getVector = function (dx, dy) {
return Object.keys(keyBindings).find(function (key) {
var _a = __read(keyBindings[key], 2), x = _a[0], y = _a[1];
return x === dx && y === dy;
});
};
SnakeGame.prototype.getDir = function (vector) {
if (vector === Vector.Down || vector === Vector.Up) {
return Direction.Vertical;
}
else {
return Direction.Horizontal;
}
};
SnakeGame.prototype.init = function () {
var self = this;
function onEveryKeypress(name) {
if (Object.values(Vector).includes(name)) {
self.setSnakeMoveDirection(name);
}
}
function onFirstKeypress() {
self.start();
}
InputController_1.default.on('keypress', onEveryKeypress);
InputController_1.default.once('keypress', onFirstKeypress);
this.on('destroy', function () {
InputController_1.default.off('keypress', onEveryKeypress);
InputController_1.default.off('keypress', onFirstKeypress);
});
this.reset();
this.printer.print();
return this;
};
SnakeGame.prototype.reset = function () {
this.gameOvered = false;
this.snake.reset();
this.foodManager.reset();
};
SnakeGame.prototype.start = function () {
var _this = this;
this.printer.print();
this.interval = setInterval(function () { return _this.tick(); }, 1000 / this.snake.speed);
return this;
};
SnakeGame.prototype.setSnakeMoveDirection = function (key) {
var _a = __read(keyBindings[key], 2), dx = _a[0], dy = _a[1];
var dir = this.getDir(this.getVector(dx, dy));
var lastDir = this.getDir(this.getVector(this.snake.lastDx, this.snake.lastDy));
if (this.isNotStarted() || dir !== lastDir) {
this.snake.dx = dx;
this.snake.dy = dy;
return true;
}
return false;
};
SnakeGame.prototype.tick = function () {
this.snake.move();
if (this.snake.isDead()) {
this.gameOver();
return;
}
this.printer.print();
return this;
};
SnakeGame.prototype.gameOver = function () {
this.gameOvered = true;
this.emit('gameOver', true);
this.snake.body = __spread(this.snake.lastBody);
this.printer.print();
this.destroy();
return this;
};
SnakeGame.prototype.destroy = function () {
if (this.interval !== null) {
clearInterval(this.interval);
}
this.interval = null;
this.emit('destroy');
return this;
};
return SnakeGame;
}(events_1.EventEmitter));
exports.default = SnakeGame;