commands.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 = "block";
  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 || player.getItem('fuel') < 1) {
  45. messager.warning("You do not have enough fuel.");
  46. } else {
  47. player.setShip('state', 'travel');
  48. meter.set("traveling", 10, function() {
  49. player.addItem('fuel', -1);
  50. player.setShip('state', 'idle');
  51. commands.update();
  52. });
  53. }
  54. }
  55. this.update();
  56. };
  57. this.idle = function() {
  58. player.setShip('state', 'idle');
  59. };
  60. this.adrift = function() {
  61. player.setShip('state', 'adrift');
  62. meter.set("[error]", 0, function() {
  63. commands.update();
  64. });
  65. };
  66. this.craft = function(item, scale) {
  67. if (scale == null) {
  68. scale = 1;
  69. }
  70. if (!meter.isRunning()) {
  71. var recipie = recipies[item];
  72. if (recipie == null) {
  73. messager.warning("That is not a valid thing to replicate.");
  74. return;
  75. }
  76. var ingredients = recipie['ingredients'];
  77. for ( var key in ingredients) {
  78. var inventoryAmount = player.getItem(key);
  79. if (inventoryAmount == null
  80. || inventoryAmount < ingredients[key] * scale) {
  81. messager.warning("You need at least " + ingredients[key]
  82. * scale + " " + key + " to craft "
  83. + recipie['amount'] * scale + " " + item);
  84. return;
  85. }
  86. }
  87. meter.set("crafting " + item, 10 * scale, function() {
  88. player.addItem(item, recipie['amount'] * scale);
  89. for ( var key in ingredients) {
  90. var inventoryAmount = player.getItem(key);
  91. player.addItem(key, -(ingredients[key] * scale));
  92. }
  93. commands.update();
  94. });
  95. }
  96. this.update();
  97. };
  98. };
  99. var commands = new Commands(player, viewport, meter, messager);