Add latest version of build

This commit is contained in:
Julian Calvento
2019-05-16 15:18:59 -03:00
parent 90bde04df8
commit 88b09091a0
8 changed files with 952 additions and 0 deletions

152
build/classes/interface.js Normal file
View File

@ -0,0 +1,152 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
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,
stdout = _process.stdout,
stdin = _process.stdin;
var listeners = [];
var prefix = '\x1B';
var keys = {
right: prefix + '[C',
up: prefix + '[A',
left: prefix + '[D',
down: prefix + '[B',
exit: '\x03',
space: ' '
};
var Interface = function () {
function Interface() {
var _this = this;
var output = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : stdout;
var input = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : stdin;
_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.input.addListener('data', function (data) {
var always = listeners.filter(function (listener) {
return listener.key === '';
});
always.forEach(function (listener) {
return listener.fn();
});
var key = Object.keys(keys).find(function (value, i) {
return keys[value] === data;
});
if (key === 'exit') {
_this.output.write('\x1B[2J\x1B[0;0H');
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('\x1B[2J\x1B[0;0H');
}
}, {
key: 'write',
value: function write() {
var _cursor;
(_cursor = this.cursor).write.apply(_cursor, arguments);
}
}, {
key: 'onKey',
value: function onKey(key, fn) {
if (typeof key === 'function') {
fn = key;
key = '';
}
listeners.push({ key: key, fn: fn });
}
}, {
key: 'line',
value: function line(from, to) {
var delta = {
x: to.x - from.x,
y: to.y - from.y
};
var error = 0;
var deltaerr = Math.abs(delta.y / delta.x);
var y = from.y;
for (var x = from.x; x < to.x; x++) {
this.cursor.goto(x, y);
this.write('.');
error += deltaerr;
while (error >= 0.5) {
this.cursor.goto(x, y);
this.write('.');
y += Math.sign(delta.y);
error -= 1;
}
}
}
}, {
key: 'columns',
get: function get() {
return this.output.columns;
}
}, {
key: 'rows',
get: function get() {
return this.output.rows;
}
}, {
key: 'center',
get: function get() {
return {
x: this.output.columns / 2,
y: this.output.rows / 2
};
}
}]);
return Interface;
}();
exports.default = Interface;

162
build/classes/tank.js Normal file
View File

@ -0,0 +1,162 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Bullet = undefined;
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; }; }();
var _unit = require('./unit');
var _unit2 = _interopRequireDefault(_unit);
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"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BULLET_SPEED = 10;
var Tank = function (_Unit) {
_inherits(Tank, _Unit);
function Tank(ui) {
_classCallCheck(this, Tank);
var _this = _possibleConstructorReturn(this, (Tank.__proto__ || Object.getPrototypeOf(Tank)).call(this, ui));
_this._angle = 0;
_this.bullets = [];
_this.health = 100;
_this.size = { x: 5, y: 3 };
return _this;
}
_createClass(Tank, [{
key: 'shoot',
value: function shoot() {
var bullet = new Bullet(this.output);
bullet.go(this.x + 2, this.y - 2);
bullet.velocity = this.normalize();
this.bullets.push(bullet);
}
}, {
key: 'draw',
value: function draw() {
if (this.dead) return;
var x = this.x,
y = this.y;
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();
x = Math.round(x) + 2;
y = Math.round(y) - 2;
var cannon = void 0;
if (this.angle < 35) cannon = '_';else if (this.angle < 70) cannon = '/';else if (this.angle < 115) cannon = '|';else if (this.angle < 160) cannon = '\\';else cannon = '_';
this.output.cursor.goto(x + 2, y - 2);
this.output.write(cannon);
this.output.cursor.goto(x, y - 1);
this.output.write('.===.');
this.output.cursor.goto(x, y);
this.output.write('o===o');
this.output.cursor.reset();
}
}, {
key: 'normalize',
value: function normalize() {
return [Math.cos(this._angle), Math.sin(this._angle) * -1];
}
}, {
key: 'angle',
set: function set(deg) {
if (deg < 0) deg = 0;
if (deg > 180) deg = 180;
this._angle = _radian(deg);
},
get: function get() {
return _degree(this._angle);
}
}], [{
key: 'radian',
value: function radian(deg) {
return _radian(deg);
}
}, {
key: 'degree',
value: function degree(rad) {
return _degree(rad);
}
}]);
return Tank;
}(_unit2.default);
exports.default = Tank;
var Bullet = exports.Bullet = function (_Unit2) {
_inherits(Bullet, _Unit2);
function Bullet(ui) {
_classCallCheck(this, Bullet);
var _this2 = _possibleConstructorReturn(this, (Bullet.__proto__ || Object.getPrototypeOf(Bullet)).call(this, ui));
_this2.velocity = [0, 0];
_this2.shape = '•';
_this2.color = 'red';
_this2.dieOnExit = true;
_this2.gravity = [0.8, 0.8];
_this2.acceleration = [0, 0.015];
return _this2;
}
_createClass(Bullet, [{
key: 'speed',
value: function speed() {
this.velocity[0] += this.acceleration[0];
this.velocity[1] += this.acceleration[1];
var x = this.velocity[0] * this.gravity[0];
var y = this.velocity[1] * this.gravity[1];
return [x, y];
}
}, {
key: 'collides',
value: function collides(target) {
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 - target.size.x && x < targetX + target.size.x && y > targetY - target.size.y && y < targetY + target.size.y;
}
}]);
return Bullet;
}(_unit2.default);
function _radian(deg) {
return deg * Math.PI / 180;
}
function _degree(rad) {
return rad * 180 / Math.PI;
}

128
build/classes/unit.js Normal file
View File

@ -0,0 +1,128 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
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,
y = this.y,
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: 'random',
value: function random() {
this.x = Math.max(1, Math.floor(Math.random() * this.output.columns));
this.y = Math.max(1, Math.floor(Math.random() * this.output.rows));
}
}, {
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.max(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;