build babel files
This commit is contained in:
90
build/classes/interface.js
Normal file
90
build/classes/interface.js
Normal file
@ -0,0 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _ansi = require('ansi');
|
||||
|
||||
var _ansi2 = _interopRequireDefault(_ansi);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var _process = process;
|
||||
var stdout = _process.stdout;
|
||||
var stdin = _process.stdin;
|
||||
|
||||
var listeners = [];
|
||||
|
||||
var prefix = '\u001b';
|
||||
var keys = {
|
||||
right: prefix + '[C',
|
||||
up: prefix + '[A',
|
||||
left: prefix + '[D',
|
||||
down: prefix + '[B',
|
||||
exit: '\u0003',
|
||||
space: ' '
|
||||
};
|
||||
|
||||
var Interface = (function () {
|
||||
function Interface() {
|
||||
var output = arguments.length <= 0 || arguments[0] === undefined ? stdout : arguments[0];
|
||||
var input = arguments.length <= 1 || arguments[1] === undefined ? stdin : arguments[1];
|
||||
|
||||
_classCallCheck(this, Interface);
|
||||
|
||||
this.output = output;
|
||||
this.input = input;
|
||||
|
||||
this.input.setRawMode(true);
|
||||
this.input.setEncoding('utf8');
|
||||
|
||||
this.cursor = (0, _ansi2.default)(this.output).hide();
|
||||
|
||||
this.columns = this.output.columns;
|
||||
this.rows = this.output.rows;
|
||||
|
||||
this.input.addListener('data', function (data) {
|
||||
var key = Object.keys(keys).find(function (value, i) {
|
||||
return keys[value] === data;
|
||||
});
|
||||
|
||||
if (key === 'exit') process.exit();
|
||||
|
||||
var match = listeners.filter(function (listener) {
|
||||
return listener.key === key || listener.key === data;
|
||||
});
|
||||
|
||||
match.forEach(function (listener) {
|
||||
return listener.fn();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_createClass(Interface, [{
|
||||
key: 'clear',
|
||||
value: function clear() {
|
||||
this.output.write('\u001b[2J\u001b[0;0H');
|
||||
}
|
||||
}, {
|
||||
key: 'write',
|
||||
value: function write() {
|
||||
var _cursor;
|
||||
|
||||
(_cursor = this.cursor).write.apply(_cursor, arguments);
|
||||
}
|
||||
}, {
|
||||
key: 'onKey',
|
||||
value: function onKey(key, fn) {
|
||||
listeners.push({ key: key, fn: fn });
|
||||
}
|
||||
}]);
|
||||
|
||||
return Interface;
|
||||
})();
|
||||
|
||||
exports.default = Interface;
|
122
build/classes/unit.js
Normal file
122
build/classes/unit.js
Normal file
@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Unit = (function () {
|
||||
function Unit(output) {
|
||||
_classCallCheck(this, Unit);
|
||||
|
||||
this._health = 0;
|
||||
this.dead = false;
|
||||
this.output = output;
|
||||
|
||||
this.dieOnExit = false;
|
||||
}
|
||||
|
||||
_createClass(Unit, [{
|
||||
key: 'move',
|
||||
value: function move(x, y) {
|
||||
if (!x && !y) return this.move.apply(this, _toConsumableArray(this.speed()));
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
}
|
||||
}, {
|
||||
key: 'draw',
|
||||
value: function draw() {
|
||||
if (this.dead) return;
|
||||
|
||||
var x = this.x;
|
||||
var y = this.y;
|
||||
var shape = this.shape;
|
||||
|
||||
if (this.color && this.color[0] === '#') {
|
||||
this.output.cursor.hex(this.color);
|
||||
} else if (this.color) {
|
||||
this.output.cursor[this.color]();
|
||||
}
|
||||
if (this.bold) this.output.cursor.bold();
|
||||
|
||||
this.output.cursor.goto(Math.round(x), Math.round(y));
|
||||
this.output.write(shape);
|
||||
this.output.cursor.reset();
|
||||
}
|
||||
}, {
|
||||
key: 'go',
|
||||
value: function go(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}, {
|
||||
key: 'speed',
|
||||
value: function speed() {
|
||||
var signs = [Math.random() > 0.5 ? -1 : 1, Math.random() > 0.5 ? -1 : 1];
|
||||
return [Math.random() * signs[0], Math.random() * signs[1]];
|
||||
}
|
||||
}, {
|
||||
key: 'collides',
|
||||
value: function collides(target) {
|
||||
var _this = this;
|
||||
|
||||
if (Array.isArray(target)) {
|
||||
return target.find(function (t) {
|
||||
return _this.collides(t);
|
||||
});
|
||||
}
|
||||
|
||||
var targetX = Math.round(target.x);
|
||||
var targetY = Math.round(target.y);
|
||||
var x = Math.round(this.x);
|
||||
var y = Math.round(this.y);
|
||||
|
||||
return x === targetX && y == targetY;
|
||||
}
|
||||
}, {
|
||||
key: 'health',
|
||||
set: function set(value) {
|
||||
this._health = Math.min(0, value);
|
||||
|
||||
if (this.health === 0) {
|
||||
this.dead = true;
|
||||
}
|
||||
},
|
||||
get: function get() {
|
||||
return this._health;
|
||||
}
|
||||
}, {
|
||||
key: 'x',
|
||||
set: function set(value) {
|
||||
this._x = value;
|
||||
|
||||
if (this.dieOnExit && this._x > this.output.columns) {
|
||||
this.dead = true;
|
||||
}
|
||||
},
|
||||
get: function get() {
|
||||
return this._x;
|
||||
}
|
||||
}, {
|
||||
key: 'y',
|
||||
set: function set(value) {
|
||||
this._y = value;
|
||||
|
||||
if (this.dieOnExit && this._y > this.output.rows) {
|
||||
this.dead = true;
|
||||
}
|
||||
},
|
||||
get: function get() {
|
||||
return this._y;
|
||||
}
|
||||
}]);
|
||||
|
||||
return Unit;
|
||||
})();
|
||||
|
||||
exports.default = Unit;
|
Reference in New Issue
Block a user