SpaceRPGGame.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. require('SpriteAtlas');
  2. require('Background');
  3. require('Ship');
  4. require('Sector');
  5. require('Inventory');
  6. require('Crafting');
  7. require('PlayerHud');
  8. require('PlayerProgress');
  9. class SpaceRPGGame {
  10. constructor() {
  11. this.atlas = new SpriteAtlas(`./assets/sprites.xml`);
  12. this.center = { x: 0, y: 0 };
  13. this.playerHud = new PlayerHud();
  14. this.background = new Background();
  15. this.ship = new Ship();
  16. this.inventory = new Inventory();
  17. this.crafting = new Crafting();
  18. this.sector = new Sector();
  19. this.progress = new PlayerProgress();
  20. }
  21. init(canvas) {
  22. this.canvas = canvas;
  23. this.canvas.addEventListener("click", (event) => this.playerHud.dialog.advance(event));
  24. this.center = { x: Math.floor(this.canvas.width / 2), y: Math.floor(this.canvas.height / 2) };
  25. this.sector.init();
  26. this.progress.init();
  27. this.isLoading = true;
  28. this.atlas.load().then(() => {
  29. this.isLoading = false;
  30. this.playerHud.attach(canvas).then(() => this.playerHud.init());
  31. this.background.init();
  32. this.ship.init();
  33. this.inventory.init();
  34. this.crafting.init();
  35. });
  36. setTimeout(() => {
  37. this.playerHud.dialog.leftSay("COMM: Can you read me? You seem to be having a bit of trouble.");
  38. if (this.progress.getProgress("starting").name == "init") {
  39. setTimeout(() => {
  40. this.playerHud.dialog.leftSay("COMM: You'll want to repair your ship and head to the nearest station.");
  41. }, 5000);
  42. } else {
  43. setTimeout(() => {
  44. this.playerHud.dialog.leftSay("COMM: When you're up and running, head to the nearest station.");
  45. }, 5000);
  46. }
  47. }, 3000);
  48. }
  49. draw(context, delta) {
  50. if (this.isLoading) {
  51. return;
  52. }
  53. this.background.draw(context, delta);
  54. this.sector.draw(context, delta);
  55. this.ship.draw(context, delta);
  56. this.playerHud.draw(context, delta);
  57. }
  58. handleResize(event) {
  59. this.center = { x: Math.floor(this.canvas.width / 2), y: Math.floor(this.canvas.height / 2) };
  60. this.background.handleResize();
  61. this.playerHud.handleResize(event);
  62. this.sector.handleResize(event);
  63. }
  64. handleOrientation(event) {
  65. //this.ship.handleOrientation(event);
  66. }
  67. repairShip() {
  68. if (!game.playerHud.main.isProgressFinished()) {
  69. return;
  70. }
  71. if (this.ship.damage == 0) {
  72. //You do not need to repair
  73. return false;
  74. }
  75. if (!this.inventory.consumeShipRepairMaterials()) {
  76. //You do not have the necessary materials
  77. game.playerHud.dialog.rightSay('Insufficient Materials', 'error-message');
  78. if (this.progress.getProgress("starting").name == "repair") {
  79. game.playerHud.dialog.advance();
  80. this.progress.advanceProgress("starting");
  81. game.playerHud.crafting.show();
  82. setTimeout(() => {
  83. this.playerHud.dialog.leftSay("COMM: If you're out of spare parts, use the replicator to make more.");
  84. }, 2000);
  85. }
  86. return false;
  87. }
  88. game.playerHud.main.startProgress(1000, () => {
  89. if (this.progress.getProgress("starting").name == "init") {
  90. this.progress.advanceProgress("starting");
  91. game.playerHud.inventory.show();
  92. }
  93. if (this.ship.repair()) {
  94. game.playerHud.ship.disableRepair();
  95. this.background.move({ x: 0, y: 0 });
  96. this.ship.animateRotation(0);
  97. this.playerHud.ship.updateShipStatus();
  98. if (this.progress.getProgress("starting").name == "craft") {
  99. game.playerHud.dialog.advance();
  100. this.progress.advanceProgress("starting");
  101. game.playerHud.navigation.show();
  102. setTimeout(() => {
  103. this.playerHud.dialog.leftSay("COMM: Nice. You should be able to pick a destination on the System Nav.");
  104. }, 2000);
  105. }
  106. }
  107. });
  108. return false;
  109. }
  110. craftRecipe(recipeId) {
  111. if (!game.playerHud.main.isProgressFinished()) {
  112. return;
  113. }
  114. let recipe = this.crafting.getRecipe(recipeId);
  115. if (!recipe) {
  116. game.playerHud.dialog.rightSay('Invalid Recipe', 'error-message');
  117. }
  118. if (!this.inventory.consumeCraftingMaterials(recipe.ingredients)) {
  119. //not enough ingredients
  120. game.playerHud.dialog.rightSay('Insufficient Materials', 'error-message');
  121. return false;
  122. }
  123. game.playerHud.main.startProgress(recipe.duration, () => {
  124. this.inventory.addItems(recipe.results);
  125. if (this.progress.getProgress("starting").name == "repairfail") {
  126. game.playerHud.dialog.advance();
  127. this.progress.advanceProgress("starting");
  128. }
  129. });
  130. return true;
  131. }
  132. startShipTravel() {
  133. if (!game.playerHud.main.isProgressFinished()) {
  134. return;
  135. }
  136. if (!this.inventory.consumeShipTravelMaterials()) {
  137. game.playerHud.dialog.rightSay('Insufficient Fuel', 'error-message');
  138. if (this.progress.getProgress("starting").name == "repair2") {
  139. game.playerHud.dialog.advance();
  140. this.progress.advanceProgress("starting");
  141. this.crafting.enableRecipe("fuel");
  142. setTimeout(() => {
  143. this.playerHud.dialog.leftSay("COMM: You can also make ship fuel out of Hydrogen using the replicator.");
  144. }, 2000);
  145. return false;
  146. }
  147. return false;
  148. }
  149. let duration = 5000;
  150. this.ship.move();
  151. this.background.move({ x: 0, y: -0.8 });
  152. setTimeout(() => {
  153. this.playerHud.dialog.leftSay("COMM: Excellent! Glad to see you up and running again.");
  154. setTimeout(() => {
  155. this.playerHud.dialog.leftSay("COMM: Dock at the station and I'll buy you a drink at the bar.");
  156. }, 2000);
  157. }, 2000);
  158. game.playerHud.navigation.startTravelAnimation(duration);
  159. game.playerHud.main.startProgress(duration, () => {
  160. this.playerHud.navigation.finishShipTravel();
  161. this.ship.stop();
  162. this.background.move({ x: 0, y: -0.01 });
  163. this.ship.setLocation("");
  164. this.sector.dock();
  165. this.playerHud.dialog.advance();
  166. setTimeout(() => {
  167. this.playerHud.dialog.leftSay("STATION: Contact, \"" + this.ship.name + "\" you are cleared to dock.");
  168. }, 2000);
  169. });
  170. return true;
  171. }
  172. dockingFinished() {
  173. this.ship.dock();
  174. this.playerHud.ship.enableDocking();
  175. setTimeout(() => {
  176. this.playerHud.dialog.leftSay("STATION: Welcome aboard, \"" + this.ship.name + "\".");
  177. }, 2000);
  178. }
  179. }