js13kgames2015-reverse/src/main.js

113 lines
2.9 KiB
JavaScript
Raw Normal View History

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-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();
this.asteroids = [this.current_asteroid];
2015-08-24 16:28:52 +02:00
this.resources = {
dust: 0,
2015-08-25 16:04:47 +02:00
stone: 0,
carbon: 0,
metal: 0
2015-08-24 16:28:52 +02:00
}
}
tick() {
this.ticks++;
}
update() {
document.querySelector('#time').innerHTML = this.ticks + ' hours';
2015-08-25 16:04:47 +02:00
document.querySelector('#asteroid-classification').innerHTML = this.current_asteroid.classification;
2015-08-24 16:28:52 +02:00
document.querySelector('#res-dust').innerHTML = this.resources.dust;
2015-08-25 16:04:47 +02:00
document.querySelector('#res-stone').innerHTML = this.resources.stone;
document.querySelector('#res-carbon').innerHTML = this.resources.carbon;
document.querySelector('#res-metal').innerHTML = this.resources.metal;
2015-08-24 16:28:52 +02:00
}
}
class Player {
constructor(engine) {
this.engine = engine;
2015-08-25 16:29:37 +02:00
this.equipment = ['probe'];
2015-08-24 16:28:52 +02:00
}
mine() {
2015-08-25 09:16:58 +02:00
let type = this.engine.current_asteroid.mine_resource();
this.engine.resources[type]++;
2015-08-24 16:28:52 +02:00
}
}
class Asteroid {
2015-08-25 16:04:47 +02:00
constructor() {
// Determine the class
this.classification = [
'A', // small, stone, dust, random
'C', // dark carbon
'S', // stone
'X' // metal
][Helper.random_number(0, 4)];
}
mine_resource(player) {
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];
let res_type = resources[Helper.random_number(0, resources.length)];
// TODO: gain!
}
}
2015-08-25 16:29:37 +02:00
class Station {
constructor() {
this.inventory = [
// 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-25 16:29:37 +02:00
];
}
buy(player, item) {
if (this.inventory.indexOf(item) !== -1 &&
player.equipment.indexOf(item) === -1)
player.equipment.push(item);
}
}
2015-08-24 16:28:52 +02:00
var engine = new Engine();
var player = new Player(engine);
2015-08-25 16:29:37 +02:00
var station = new Station();
2015-08-24 16:28:52 +02:00
2015-08-24 15:40:02 +02:00
var bootstrap = function() {
2015-08-24 16:28:52 +02:00
setInterval(() => {
engine.tick();
engine.update();
}, 1000);
document.querySelector('#btn-mine').onclick = () => {
player.mine();
engine.update();
};
2015-08-25 16:04:47 +02:00
engine.update();
2015-08-24 15:40:02 +02:00
};