2015-08-25 16:04:47 +02:00
|
|
|
// Theme: Reversed
|
2015-08-25 16:29:37 +02:00
|
|
|
// https://en.wikipedia.org/wiki/Asteroid_mining
|
2015-08-25 16:04:47 +02:00
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
let MINI = require('minified');
|
|
|
|
let _=MINI._, $=MINI.$, $$=MINI.$$, EE=MINI.EE, HTML=MINI.HTML;
|
|
|
|
|
2015-08-24 16:28:52 +02:00
|
|
|
class Engine {
|
|
|
|
constructor() {
|
|
|
|
this.ticks = 0;
|
2015-08-25 09:16:58 +02:00
|
|
|
this.current_asteroid = new Asteroid();
|
2015-08-26 12:16:49 +02:00
|
|
|
this.station = new Station();
|
|
|
|
this.ship = new Ship(this.current_asteroid);
|
2015-08-25 09:16:58 +02:00
|
|
|
this.asteroids = [this.current_asteroid];
|
2015-08-24 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tick() {
|
|
|
|
this.ticks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2015-08-26 12:16:49 +02:00
|
|
|
// Resources
|
2015-08-28 23:56:30 +02:00
|
|
|
_.eachObj(this.ship.resources, (key, value) => {
|
|
|
|
$('#res-' + key).fill(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// The ship
|
|
|
|
$('#active-tool').fill(this.ship.active_tool);
|
2015-08-26 12:16:49 +02:00
|
|
|
|
2015-08-29 14:52:56 +02:00
|
|
|
if (_.keys(engine.ship.equipment).length > $('#ship-equipment > li').length) {
|
|
|
|
$('#ship-equipment').fill();
|
|
|
|
_.eachObj(engine.ship.equipment, (tool, props) => {
|
|
|
|
// Add the tool to the DOM inventory and add a mount button
|
|
|
|
let mount_button = EE('a', {'@href': '#', '%tool': tool}, 'mount').onClick((e) => {
|
|
|
|
engine.ship.active_tool = tool;
|
|
|
|
engine.update();
|
|
|
|
});
|
|
|
|
$('#ship-equipment').add(EE('li', tool+' ').add(mount_button));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-26 12:16:49 +02:00
|
|
|
if (this.ship.docked_to instanceof Station) {
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#asteroid').hide();
|
|
|
|
$('#station').show();
|
2015-08-26 12:16:49 +02:00
|
|
|
}
|
2015-08-25 16:04:47 +02:00
|
|
|
|
2015-08-26 12:16:49 +02:00
|
|
|
if (this.ship.docked_to instanceof Asteroid) {
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#station').hide();
|
|
|
|
$('#asteroid').show();
|
2015-08-26 12:16:49 +02:00
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#time').fill((this.ticks-this.current_asteroid.landed_on) + ' hours');
|
|
|
|
$('#asteroid-classification').fill(this.current_asteroid.classification);
|
2015-08-26 12:16:49 +02:00
|
|
|
}
|
2015-08-24 16:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 12:16:49 +02:00
|
|
|
class Ship {
|
|
|
|
constructor(asteroid) {
|
|
|
|
this.docked_to = asteroid;
|
2015-08-28 23:56:30 +02:00
|
|
|
this.active_tool = 'probe';
|
2015-08-26 12:16:49 +02:00
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
this.equipment = {
|
|
|
|
'probe': {'A': 1, 'C': 1, 'S': 1, 'X': 1}
|
|
|
|
};
|
2015-08-26 12:16:49 +02:00
|
|
|
this.resources = {
|
|
|
|
dust: 0,
|
|
|
|
stone: 0,
|
|
|
|
carbon: 0,
|
|
|
|
metal: 0
|
|
|
|
}
|
2015-08-24 16:28:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 14:52:56 +02:00
|
|
|
mount(tool) {
|
2015-08-28 23:56:30 +02:00
|
|
|
this.active_tool = tool;
|
|
|
|
}
|
|
|
|
|
2015-08-29 14:52:56 +02:00
|
|
|
equip(tool, props) {
|
|
|
|
this.equipment[tool] = props;
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:28:52 +02:00
|
|
|
mine() {
|
2015-08-26 12:16:49 +02:00
|
|
|
if (this.docked_to instanceof Asteroid) {
|
2015-08-28 23:56:30 +02:00
|
|
|
let [type, amount] = this.docked_to.harvest(this);
|
2015-08-26 12:16:49 +02:00
|
|
|
this.resources[type] += amount;
|
|
|
|
}
|
2015-08-24 16:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:42:43 +02:00
|
|
|
class Asteroid {
|
2015-08-25 16:04:47 +02:00
|
|
|
constructor() {
|
2015-08-26 12:16:49 +02:00
|
|
|
this.landed_on = 0;
|
|
|
|
|
2015-08-25 16:04:47 +02:00
|
|
|
// Determine the class
|
|
|
|
this.classification = [
|
|
|
|
'A', // small, stone, dust, random
|
|
|
|
'C', // dark carbon
|
|
|
|
'S', // stone
|
|
|
|
'X' // metal
|
|
|
|
][Helper.random_number(0, 4)];
|
|
|
|
}
|
|
|
|
|
2015-08-26 12:16:49 +02:00
|
|
|
harvest(ship) {
|
2015-08-25 16:04:47 +02:00
|
|
|
let resources = {
|
|
|
|
'A': ['dust', 'dust', 'stone'],
|
|
|
|
'C': ['dust', 'carbon', 'carbon', 'carbon'],
|
|
|
|
'S': ['dust', 'stone', 'stone'],
|
|
|
|
'X': ['metal']
|
|
|
|
}[this.classification];
|
2015-08-25 16:40:26 +02:00
|
|
|
let res_type = resources[Helper.random_number(0, resources.length)];
|
2015-08-28 23:56:30 +02:00
|
|
|
let amount = ship.equipment[ship.active_tool][this.classification];
|
|
|
|
return [res_type, amount];
|
2015-08-24 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 16:29:37 +02:00
|
|
|
class Station {
|
|
|
|
constructor() {
|
2015-08-26 12:16:49 +02:00
|
|
|
this.inventory = {
|
2015-08-25 16:40:26 +02:00
|
|
|
// take probes from the asteroid
|
|
|
|
'probe': {'A': 1, 'C': 1, 'S': 1, 'X': 1},
|
|
|
|
|
|
|
|
// mine on surface
|
|
|
|
'conveyor': {'A': 2, 'C': 8, 'S': 4, 'X': 1},
|
|
|
|
|
|
|
|
// shaft mining into the asteroid
|
|
|
|
'pipe-drill': {'A': 3, 'C': 15, 'S': 12, 'X': 2},
|
|
|
|
|
|
|
|
// pick up loose grains with magnet, x-class asteroids only
|
|
|
|
'magnet': {'A': 3, 'C': 1, 'S': 1, 'X': 20},
|
|
|
|
|
|
|
|
// melt the matrix
|
|
|
|
'vaporizer': {'A': 4, 'C': 7, 'S': 6, 'X': 5}
|
2015-08-26 12:16:49 +02:00
|
|
|
};
|
2015-08-25 16:29:37 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
sell(ship, tool) {
|
|
|
|
if (_.keys(this.inventory).contains(tool) &&
|
|
|
|
!_.keys(ship.equipment).contains(tool))
|
2015-08-29 14:52:56 +02:00
|
|
|
ship.equip(tool, this.inventory[tool]);
|
2015-08-25 16:29:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:28:52 +02:00
|
|
|
var engine = new Engine();
|
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
$(() => {
|
2015-08-24 16:28:52 +02:00
|
|
|
setInterval(() => {
|
|
|
|
engine.tick();
|
|
|
|
engine.update();
|
|
|
|
}, 1000);
|
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#btn-mine').onClick(() => {
|
2015-08-26 12:16:49 +02:00
|
|
|
engine.ship.mine();
|
2015-08-24 16:28:52 +02:00
|
|
|
engine.update();
|
2015-08-28 23:56:30 +02:00
|
|
|
});
|
2015-08-25 16:04:47 +02:00
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#btn-to-station').onClick(() => {
|
2015-08-26 12:16:49 +02:00
|
|
|
engine.ship.docked_to = engine.station;
|
|
|
|
engine.current_asteroid = null;
|
|
|
|
engine.update();
|
2015-08-28 23:56:30 +02:00
|
|
|
});
|
2015-08-26 12:16:49 +02:00
|
|
|
|
2015-08-28 23:56:30 +02:00
|
|
|
$('#btn-to-asteroid').onClick(() => {
|
2015-08-26 12:16:49 +02:00
|
|
|
engine.ship.docked_to = engine.asteroids[0];
|
|
|
|
engine.current_asteroid = engine.asteroids[0];
|
|
|
|
engine.current_asteroid.landed_on = engine.ticks;
|
|
|
|
engine.update();
|
2015-08-28 23:56:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
_.eachObj(engine.station.inventory, (tool, props) => {
|
|
|
|
let buy_button = EE('a', {'@href': '#', '%tool': tool}, 'buy').onClick((e) => {
|
|
|
|
let tool = $(e.target).get('%tool');
|
|
|
|
engine.station.sell(engine.ship, tool);
|
2015-08-29 14:52:56 +02:00
|
|
|
engine.update();
|
2015-08-28 23:56:30 +02:00
|
|
|
})
|
|
|
|
$('#station-inventory').add(EE('li', tool+' ').add(buy_button));
|
|
|
|
});
|
|
|
|
|
2015-08-25 16:04:47 +02:00
|
|
|
engine.update();
|
2015-08-28 23:56:30 +02:00
|
|
|
})
|