build babel files

This commit is contained in:
Mahdi Dibaiee 2015-12-11 21:19:42 +03:30
parent 94683e0672
commit f34f78112e
9 changed files with 355 additions and 3 deletions

25
Gruntfile.js Normal file
View File

@ -0,0 +1,25 @@
module.exports = function(grunt) {
grunt.initConfig({
babel: {
scripts: {
files: [{
expand: true,
cwd: 'src',
src: '**/*.js',
dest: 'build/'
}]
}
},
watch: {
scripts: {
files: ['src/**/*.js'],
tasks: ['babel']
}
}
});
grunt.loadNpmTasks('grunt-babel');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['babel']);
};

View 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
View 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;

111
build/spacecraft.js Normal file
View File

@ -0,0 +1,111 @@
'use strict';
var _unit = require('./classes/unit');
var _unit2 = _interopRequireDefault(_unit);
var _interface = require('./classes/interface');
var _interface2 = _interopRequireDefault(_interface);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var FRAME = 20;
var ENEMY_SPAWN_RATE = 400;
var ui = new _interface2.default();
var player = new _unit2.default(ui);
player.go(1, ui.output.rows / 2);
player.shape = '=>';
player.color = '#77d6ff';
player.bold = true;
var explosion = new _unit2.default(ui);
explosion.dead = true;
var missles = [];
var enemies = [];
var score = 0;
setInterval(function () {
ui.clear();
player.draw();
missles.forEach(function (missle, i) {
missle.move(1, 0);
missle.draw();
var enemy = missle.collides(enemies);
if (enemy) {
enemy.killed = 1;
enemy.color = 'red';
enemy.shape = '*';
missle.dead = true;
score++;
}
if (missle.dead) {
missles.splice(i, 1);
}
});
enemies.forEach(function (enemy, i) {
// move with speed
enemy.move();
enemy.draw();
if (enemy.dead) {
enemies.splice(i, 1);
}
if (enemy.killed == 3) enemy.dead = true;
if (enemy.killed < 3) enemy.killed++;
});
ui.cursor.goto(0, 0).yellow().write('Score: ' + score);
ui.cursor.reset();
}, FRAME);
ui.onKey('right', function () {
player.move(1, 0);
});
ui.onKey('down', function () {
player.move(0, 1);
});
ui.onKey('up', function () {
player.move(0, -1);
});
ui.onKey('left', function () {
player.move(-1, 0);
});
ui.onKey('space', function () {
var missle = new _unit2.default(ui);
missle.go(player.x, player.y);
missle.shape = '+';
missle.dieOnExit = true;
missles.push(missle);
});
setInterval(function () {
if (enemies.length > 5) return;
var enemy = new _unit2.default(ui);
enemy.go(Math.random() * ui.output.columns, 0);
enemy.shape = 'o';
enemy.color = '#f7c71e';
enemy.dieOnExit = true;
enemy.speed = function () {
return [Math.random() > 0.9 ? 0.4 : 0, 0.06];
};
enemies.push(enemy);
}, 1000);
process.on('exit', function () {
ui.cursor.horizontalAbsolute(0).eraseLine();
ui.cursor.show();
});

View File

@ -11,6 +11,5 @@ if (!game) {
} }
require('babel-polyfill'); require('babel-polyfill');
require('babel-core/register');
require(__dirname + '/' + game); require(__dirname + '/build/' + game);

View File

@ -1,6 +1,6 @@
{ {
"name": "node-games", "name": "node-games",
"version": "1.0.1", "version": "1.0.2",
"description": "Simple node console games", "description": "Simple node console games",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -26,5 +26,10 @@
"babel-core": "6.3.17", "babel-core": "6.3.17",
"babel-polyfill": "6.3.14", "babel-polyfill": "6.3.14",
"babel-preset-es2015": "6.3.13" "babel-preset-es2015": "6.3.13"
},
"devDependencies": {
"grunt": "0.4.5",
"grunt-babel": "6.0.0",
"grunt-contrib-watch": "0.6.1"
} }
} }