Refactor to remove duplicated code and add translations to the root directory

This commit is contained in:
Julian Calvento 2019-05-16 14:35:42 -03:00
parent c1198070bd
commit 7582b76ee1
6 changed files with 40 additions and 51 deletions

View File

@ -16,5 +16,10 @@ if (!game) {
require('babel-polyfill'); require('babel-polyfill');
const gameToPlay = require(__dirname + '/build/' + game).default; const gameToPlay = require(__dirname + '/build/' + game).default;
gameToPlay(language);
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
const Polyglot = require('node-polyglot');
const i18n = new Polyglot({phrases: locale});
gameToPlay(i18n);

View File

@ -1,6 +1,5 @@
import Unit from './classes/unit'; import Unit from './classes/unit';
import Interface from './classes/interface'; import Interface from './classes/interface';
import Polyglot from "node-polyglot";
let FRAME = 100; let FRAME = 100;
let ui = new Interface(); let ui = new Interface();
@ -24,7 +23,7 @@ point.random();
let score = 0; let score = 0;
let stop = false; let stop = false;
function loop(polyglot) { function loop(i18n) {
if (stop) return; if (stop) return;
ui.clear(); ui.clear();
@ -69,23 +68,6 @@ ui.onKey('left', () => {
changeDirection(LEFT); changeDirection(LEFT);
}); });
ui.onKey(() => {
if (!stop) return;
stop = false;
snake = [];
head = createPart();
head.color = '#71da29';
createPart();
createPart();
score = 0;
point.random();
loop();
})
function changeDirection(dir) { function changeDirection(dir) {
if (head.direction === UP && dir === DOWN || if (head.direction === UP && dir === DOWN ||
head.direction === DOWN && dir === UP || head.direction === DOWN && dir === UP ||
@ -120,17 +102,17 @@ function createPart() {
if (this.direction !== ahead.direction) { if (this.direction !== ahead.direction) {
this.changeTo = ahead.direction; this.changeTo = ahead.direction;
} }
} };
part.speed = function(multiplier = 1) { part.speed = function(multiplier = 1) {
let { direction } = part; let { direction } = part;
let x = direction == LEFT ? -1 : let x = direction === LEFT ? -1 :
direction == RIGHT ? 1 : 0; direction === RIGHT ? 1 : 0;
let y = direction == UP ? -1 : let y = direction === UP ? -1 :
direction == DOWN ? 1 : 0; direction === DOWN ? 1 : 0;
return [x * multiplier, y * multiplier]; return [x * multiplier, y * multiplier];
} };
let [dX, dY] = part.speed(); let [dX, dY] = part.speed();
dX *= -1; dX *= -1;
@ -168,10 +150,23 @@ process.on('exit', function() {
ui.cursor.show(); ui.cursor.show();
}); });
export default function(language) { export default function(i18n) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`); loop(i18n);
const i18n = new Polyglot({phrases: locale}); ui.onKey(() => {
if (!stop) return;
stop = false;
snake = [];
head = createPart();
head.color = '#71da29';
createPart();
createPart();
score = 0;
point.random();
loop(i18n); loop(i18n);
});
} }

View File

@ -1,6 +1,5 @@
import Unit from './classes/unit'; import Unit from './classes/unit';
import Interface from './classes/interface'; import Interface from './classes/interface';
import Polyglot from "node-polyglot";
const FRAME = 20; const FRAME = 20;
let ENEMY_SPAWN_RATE = 1000; let ENEMY_SPAWN_RATE = 1000;
@ -22,11 +21,7 @@ let missles = [];
let enemies = []; let enemies = [];
let score = 0; let score = 0;
export default function(language) { export default function(i18n) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
const i18n = new Polyglot({phrases: locale});
setInterval((i18n) => { setInterval((i18n) => {
ui.clear(); ui.clear();
@ -62,9 +57,9 @@ export default function(language) {
enemies.splice(i, 1); enemies.splice(i, 1);
} }
if (enemy.killed == 3) enemy.dead = true; if (enemy.killed === 3) enemy.dead = true;
if (enemy.killed < 3) enemy.killed++; if (enemy.killed < 3) enemy.killed++;
}) });
ui.cursor.goto(0, 0).yellow().write(`${i18n.t('spacecraft.score')}: ${score}`); ui.cursor.goto(0, 0).yellow().write(`${i18n.t('spacecraft.score')}: ${score}`);
ui.cursor.reset(); ui.cursor.reset();
@ -110,7 +105,7 @@ ui.onKey('space', () => {
enemy.speed = () => { enemy.speed = () => {
return [Math.random() > 0.9 ? 0.4 : 0, 0.06]; return [Math.random() > 0.9 ? 0.4 : 0, 0.06];
} };
enemies.push(enemy); enemies.push(enemy);
@ -118,6 +113,6 @@ ui.onKey('space', () => {
}()); }());
process.on('exit', function() { process.on('exit', function() {
ui.cursor.horizontalAbsolute(0).eraseLine() ui.cursor.horizontalAbsolute(0).eraseLine();
ui.cursor.show(); ui.cursor.show();
}); });

View File

@ -1,7 +1,5 @@
import Unit from './classes/unit';
import Tank from './classes/tank'; import Tank from './classes/tank';
import Interface from './classes/interface'; import Interface from './classes/interface';
import Polyglot from "node-polyglot";
let FRAME = 50; let FRAME = 50;
let ui = new Interface(); let ui = new Interface();
@ -90,13 +88,13 @@ ui.onKey('down', () => {
if (immoblize) return; if (immoblize) return;
turn().angle -= 1; turn().angle -= 1;
}) });
ui.onKey('up', () => { ui.onKey('up', () => {
if (immoblize) return; if (immoblize) return;
turn().angle += 1; turn().angle += 1;
}) });
ui.onKey('left', () => { ui.onKey('left', () => {
if (immoblize) return; if (immoblize) return;
@ -134,7 +132,7 @@ ui.onKey(() => {
loop(); loop();
} }
}) });
let TURN = true; let TURN = true;
function turn() { function turn() {
@ -142,10 +140,6 @@ function turn() {
return two; return two;
} }
export default function(language) { export default function(i18n) {
const locale = require(`${__dirname}/locales/${language || 'en'}.json`);
const i18n = new Polyglot({phrases: locale});
loop(i18n); loop(i18n);
} }