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 += "
" + key + " " + player.inventory[key] + "
"; } } 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 = "inline"; } }; 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(); }; this.dock = function() { // if sector has station player.setShip('state', 'docking'); meter.set("docking", 3, function() { player.setShip('state', 'dock'); commands.update(); }); }; }; var commands = new Commands(player, viewport, meter, messager);