123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- var Commands = function(player, viewport, meter, messager) {
- this.update = function() {
- viewport.setDamage(player.getShip('damage'));
- viewport.setState(player.getShip('state'));
- var cargo = document.getElementById('cargo');
- cargo.innerHTML = "";
- for ( var key in player.inventory) {
- if (player.inventory[key] != 0) {
- cargo.innerHTML += "<p>" + key + " " + player.inventory[key]
- + "</p>";
- }
- }
- var ship = document.getElementById('shipStatus');
- ship.innerHTML = "status: " + player.getShip('state');
- var actions = document.getElementById('shipActions');
- var repairButton = document.getElementById("repair");
- repairButton.style.display = "none";
- if (player.getShip('damage') > 0
- && repairButton.style.display == "none") {
- repairButton.style.display = "block";
- }
- };
- this.repair = function() {
- if (!meter.isRunning()) {
- if (player.getItem('spare part') > 0) {
- meter.set("repairing", 10, function() {
- player.addItem('spare part', -1);
- player.addShip('damage', -1);
- if (player.getShip('damage') <= 0) {
- player.setShip('state', 'idle');
- }
- commands.update();
- });
- } else {
- messager.warning("Repairs require at least 1 spare part.");
- }
- }
- this.update();
- };
- this.travel = function() {
- if (!meter.isRunning()) {
- if (player.getShip('damage') > 0) {
- messager.warning("Your ship is damaged and cannot travel.");
- } else if (player.getItem('fuel') == null || player.getItem('fuel') < 1) {
- messager.warning("You do not have enough fuel.");
- } else {
- player.setShip('state', 'travel');
- meter.set("traveling", 10, function() {
- player.addItem('fuel', -1);
- player.setShip('state', 'idle');
- commands.update();
- });
- }
- }
- this.update();
- };
- this.idle = function() {
- player.setShip('state', 'idle');
- };
- this.adrift = function() {
- player.setShip('state', 'adrift');
- meter.set("[error]", 0, function() {
- commands.update();
- });
- };
- this.craft = function(item, scale) {
- if (scale == null) {
- scale = 1;
- }
- if (!meter.isRunning()) {
- var recipie = recipies[item];
- if (recipie == null) {
- messager.warning("That is not a valid thing to replicate.");
- return;
- }
- var ingredients = recipie['ingredients'];
- for ( var key in ingredients) {
- var inventoryAmount = player.getItem(key);
- if (inventoryAmount == null
- || inventoryAmount < ingredients[key] * scale) {
- messager.warning("You need at least " + ingredients[key]
- * scale + " " + key + " to craft "
- + recipie['amount'] * scale + " " + item);
- return;
- }
- }
- meter.set("crafting " + item, 10 * scale, function() {
- player.addItem(item, recipie['amount'] * scale);
- for ( var key in ingredients) {
- var inventoryAmount = player.getItem(key);
- player.addItem(key, -(ingredients[key] * scale));
- }
- commands.update();
- });
- }
- this.update();
- };
- };
- var commands = new Commands(player, viewport, meter, messager);
|