1
0

commands.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var Commands = function(player, viewport, meter, messager) {
  2. this.update = function() {
  3. viewport.setDamage(player.getShip('damage'));
  4. viewport.setState(player.getShip('state'));
  5. var cargo = document.getElementById('cargo');
  6. cargo.innerHTML = "";
  7. for ( var key in player.inventory) {
  8. if (player.inventory[key] != 0) {
  9. cargo.innerHTML += "<p>" + key + " " + player.inventory[key]
  10. + "</p>";
  11. }
  12. }
  13. var ship = document.getElementById('shipStatus');
  14. ship.innerHTML = "status: " + player.getShip('state');
  15. var actions = document.getElementById('shipActions');
  16. var repairButton = document.getElementById("repair");
  17. repairButton.style.display = "none";
  18. if (player.getShip('damage') > 0
  19. && repairButton.style.display == "none") {
  20. repairButton.style.display = "inline";
  21. }
  22. };
  23. this.repair = function() {
  24. if (!meter.isRunning()) {
  25. if (player.getItem('spare part') > 0) {
  26. meter.set("repairing", 10, function() {
  27. player.addItem('spare part', -1);
  28. player.addShip('damage', -1);
  29. if (player.getShip('damage') <= 0) {
  30. player.setShip('state', 'idle');
  31. }
  32. commands.update();
  33. });
  34. } else {
  35. messager.warning("Repairs require at least 1 spare part.");
  36. }
  37. }
  38. this.update();
  39. };
  40. this.travel = function() {
  41. if (!meter.isRunning()) {
  42. if (player.getShip('damage') > 0) {
  43. messager.warning("Your ship is damaged and cannot travel.");
  44. } else if (player.getItem('fuel') == null
  45. || player.getItem('fuel') < 1) {
  46. messager.warning("You do not have enough fuel.");
  47. } else {
  48. player.setShip('state', 'travel');
  49. meter.set("traveling", 10, function() {
  50. player.addItem('fuel', -1);
  51. player.setShip('state', 'idle');
  52. commands.update();
  53. });
  54. }
  55. }
  56. this.update();
  57. };
  58. this.idle = function() {
  59. player.setShip('state', 'idle');
  60. };
  61. this.adrift = function() {
  62. player.setShip('state', 'adrift');
  63. meter.set("[error]", 0, function() {
  64. commands.update();
  65. });
  66. };
  67. this.craft = function(item, scale) {
  68. if (scale == null) {
  69. scale = 1;
  70. }
  71. if (!meter.isRunning()) {
  72. var recipie = recipies[item];
  73. if (recipie == null) {
  74. messager.warning("That is not a valid thing to replicate.");
  75. return;
  76. }
  77. var ingredients = recipie['ingredients'];
  78. for ( var key in ingredients) {
  79. var inventoryAmount = player.getItem(key);
  80. if (inventoryAmount == null
  81. || inventoryAmount < ingredients[key] * scale) {
  82. messager.warning("You need at least " + ingredients[key]
  83. * scale + " " + key + " to craft "
  84. + recipie['amount'] * scale + " " + item);
  85. return;
  86. }
  87. }
  88. meter.set("crafting " + item, 10 * scale, function() {
  89. player.addItem(item, recipie['amount'] * scale);
  90. for ( var key in ingredients) {
  91. var inventoryAmount = player.getItem(key);
  92. player.addItem(key, -(ingredients[key] * scale));
  93. }
  94. commands.update();
  95. });
  96. }
  97. this.update();
  98. };
  99. this.dock = function() {
  100. // if sector has station
  101. player.setShip('state', 'docking');
  102. meter.set("docking", 3, function() {
  103. player.setShip('state', 'dock');
  104. commands.update();
  105. });
  106. };
  107. };
  108. var commands = new Commands(player, viewport, meter, messager);