chore: add spacecraft image
This commit is contained in:
55
classes/interface.js
Normal file
55
classes/interface.js
Normal file
@ -0,0 +1,55 @@
|
||||
import ansi from 'ansi';
|
||||
const { stdout, stdin } = process;
|
||||
|
||||
let listeners = [];
|
||||
|
||||
const prefix = '\u001b';
|
||||
const keys = {
|
||||
right: `${prefix}[C`,
|
||||
up: `${prefix}[A`,
|
||||
left: `${prefix}[D`,
|
||||
down: `${prefix}[B`,
|
||||
exit: '\u0003',
|
||||
space: ' '
|
||||
}
|
||||
|
||||
export default class Interface {
|
||||
constructor(output = stdout, input = stdin) {
|
||||
this.output = output;
|
||||
this.input = input;
|
||||
|
||||
this.input.setRawMode(true);
|
||||
this.input.setEncoding('utf8');
|
||||
|
||||
this.cursor = ansi(this.output).hide();
|
||||
|
||||
this.columns = this.output.columns;
|
||||
this.rows = this.output.rows;
|
||||
|
||||
this.input.addListener('data', data => {
|
||||
let key = Object.keys(keys).find((value, i) => {
|
||||
return keys[value] === data;
|
||||
});
|
||||
|
||||
if ( key === 'exit' ) process.exit();
|
||||
|
||||
let match = listeners.filter(listener => {
|
||||
return listener.key === key || listener.key === data;
|
||||
});
|
||||
|
||||
match.forEach(listener => listener.fn());
|
||||
})
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.output.write('\u001b[2J\u001b[0;0H');
|
||||
}
|
||||
|
||||
write(...args) {
|
||||
this.cursor.write(...args);
|
||||
}
|
||||
|
||||
onKey(key, fn) {
|
||||
listeners.push({ key, fn });
|
||||
}
|
||||
}
|
91
classes/unit.js
Normal file
91
classes/unit.js
Normal file
@ -0,0 +1,91 @@
|
||||
export default class Unit {
|
||||
constructor(output) {
|
||||
this._health = 0;
|
||||
this.dead = false;
|
||||
this.output = output;
|
||||
|
||||
this.dieOnExit = false;
|
||||
}
|
||||
|
||||
set health(value) {
|
||||
this._health = Math.min(0, value);
|
||||
|
||||
if (this.health === 0) {
|
||||
this.dead = true;
|
||||
}
|
||||
}
|
||||
|
||||
get health() {
|
||||
return this._health;
|
||||
}
|
||||
|
||||
set x(value) {
|
||||
this._x = value;
|
||||
|
||||
if (this.dieOnExit && this._x > this.output.columns) {
|
||||
this.dead = true;
|
||||
}
|
||||
}
|
||||
|
||||
get x() {
|
||||
return this._x;
|
||||
}
|
||||
|
||||
set y(value) {
|
||||
this._y = value;
|
||||
|
||||
if (this.dieOnExit && this._y > this.output.rows) {
|
||||
this.dead = true;
|
||||
}
|
||||
}
|
||||
|
||||
get y() {
|
||||
return this._y;
|
||||
}
|
||||
|
||||
move(x, y) {
|
||||
if (!x && !y) return this.move(...this.speed());
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
}
|
||||
|
||||
draw() {
|
||||
if (this.dead) return;
|
||||
|
||||
let { x, y, shape } = this;
|
||||
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();
|
||||
}
|
||||
|
||||
go(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
speed() {
|
||||
let signs = [Math.random() > 0.5 ? -1 : 1, Math.random() > 0.5 ? -1 : 1];
|
||||
return [Math.random() * signs[0], Math.random() * signs[1]];
|
||||
}
|
||||
|
||||
collides(target) {
|
||||
if (Array.isArray(target)) {
|
||||
return target.find(t => this.collides(t));
|
||||
}
|
||||
|
||||
let targetX = Math.round(target.x);
|
||||
let targetY = Math.round(target.y);
|
||||
let x = Math.round(this.x);
|
||||
let y = Math.round(this.y);
|
||||
|
||||
return x === targetX && y == targetY;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user