headsupdisplay.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. function HeadsUpDisplay(game, camera) {
  2. this.ship = null
  3. this.hudCanvas = null;
  4. this.hudContext = null;
  5. this.minimapCanvas = null;
  6. this.minimapContext = null;
  7. this.radarCanvas = null;
  8. this.radarContext = null;
  9. this.init = function(ship, canvas) {
  10. this.ship = ship;
  11. this.canvas = canvas;
  12. this.prepareMinimap();
  13. this.prepareRadar();
  14. }
  15. this.prepareMinimap = function() {
  16. this.minimapCanvas = document.createElement("canvas");
  17. this.minimapCanvas.width = 202;
  18. this.minimapCanvas.height = 202;
  19. this.minimapContext = this.minimapCanvas.getContext("2d");
  20. }
  21. this.prepareRadar = function() {
  22. this.radarCanvas = document.createElement("canvas");
  23. this.radarCanvas.width = 202;
  24. this.radarCanvas.height = 202;
  25. this.radarContext = this.radarCanvas.getContext("2d");
  26. }
  27. this.update = function(delta) {
  28. }
  29. this.preRender = function() {
  30. if(this.spriteContext == null) {
  31. this.spriteCanvas = document.createElement("canvas");
  32. this.spriteCanvas.width = this.width;
  33. this.spriteCanvas.height = this.height;
  34. this.spriteContext = this.spriteCanvas.getContext("2d");
  35. this.spriteContext.fillStyle = this.shipColor;
  36. this.spriteContext.strokeStyle = this.shipColor;
  37. this.spriteContext.lineWidth = 4;
  38. this.spriteContext.save();
  39. this.spriteContext.translate(this.width / 2, this.height / 2);
  40. this.spriteContext.beginPath();
  41. this.spriteContext.moveTo(this.width / 2, 0);
  42. this.spriteContext.lineTo(-(this.width / 2), -(this.height / 3));
  43. this.spriteContext.lineTo(-(this.width / 2), this.height / 3);
  44. this.spriteContext.lineTo(this.width / 2, 0);
  45. this.spriteContext.fill();
  46. this.spriteContext.restore();
  47. }
  48. }
  49. this.draw = function(context) {
  50. for(var index in game.planets) {
  51. var planet = game.planets[index];
  52. /*
  53. context.fillStyle = planet.planetColor;
  54. context.beginPath();
  55. context.arc((index * 20) + 25, 13, 8 ,0 , 2 * Math.PI);
  56. context.fill();
  57. context.fillStyle = planet.teamColor;
  58. context.beginPath();
  59. context.rect((index * 20) + 20, 9, 10 , 8);
  60. context.fill();
  61. */
  62. context.fillStyle = planet.teamColor;
  63. context.beginPath();
  64. context.rect((index * 20) + 20, 9, 10 , 8);
  65. context.fill();
  66. }
  67. /*for(var index in game.systems) {
  68. var solar = game.systems[index];
  69. if(solar == nearestSystem) {
  70. context.lineWidth = 1;
  71. context.strokeStyle = "#00FF00";
  72. context.beginPath();
  73. context.moveTo(this.ship.position.x + camera.x, this.ship.position.y + camera.y);
  74. context.lineTo(solar.position.x + camera.x, solar.position.y + camera.y);
  75. context.stroke();
  76. }
  77. }*/
  78. //this.preRender();
  79. //draw minimap
  80. context.save();
  81. context.translate(this.canvas.width - (this.minimapCanvas.width + 5), 5);
  82. context.drawImage(this.drawMinimap(), 0, 0);
  83. context.restore();
  84. //draw radar
  85. context.save();
  86. context.translate(this.canvas.width - (this.radarCanvas.width + 5), this.canvas.height - (this.radarCanvas.height + 5));
  87. context.drawImage(this.drawRadar(), 0, 0);
  88. context.restore();
  89. context.save();
  90. context.translate(this.canvas.width - (this.radarCanvas.width + 5), this.canvas.height - (this.radarCanvas.height + 25));
  91. var healthPercentage = (this.ship.health / this.ship.maxHealth);
  92. context.font = "16px sans-serif";
  93. context.textAlign = "center";
  94. context.fillStyle = "#DDDDDD";
  95. context.strokeStyle = "#333333";
  96. context.lineWidth = 2;
  97. var textMetrics = context.measureText(this.ship.health + " / " + this.ship.maxHealth);
  98. var textWidth = textMetrics.width;
  99. context.fillText(this.ship.health + " / " + this.ship.maxHealth, 0, 0);
  100. context.restore();
  101. }
  102. this.drawMinimap = function() {
  103. this.minimapContext.strokeStyle = "#FFFFFF";
  104. this.minimapContext.fillStyle = "#000000";
  105. this.minimapContext.lineWidth = 2;
  106. this.minimapContext.clearRect(0, 0, this.minimapCanvas.width, this.minimapCanvas.height);
  107. this.minimapContext.beginPath();
  108. this.minimapContext.rect(1, 1, 200, 200);
  109. this.minimapContext.fill();
  110. this.minimapContext.stroke();
  111. this.minimapContext.save();
  112. this.minimapContext.lineWidth = 1;
  113. this.minimapContext.translate(this.minimapCanvas.width / 2 + 1, this.minimapCanvas.height / 2 + 1);
  114. var mapScale = 600;
  115. for(var index in game.systems) {
  116. var solar = game.systems[index];
  117. var minimapScaled = {x: solar.position.x / mapScale, y: solar.position.y / mapScale, radius: solar.radius / 100}
  118. this.minimapContext.strokeStyle = "#FFFF00";
  119. this.minimapContext.beginPath();
  120. this.minimapContext.arc(minimapScaled.x, minimapScaled.y, minimapScaled.radius,0, 2 * Math.PI);
  121. this.minimapContext.stroke();
  122. for(var planetIndex in solar.planets) {
  123. var planet = solar.planets[planetIndex];
  124. var orbit = planet.orbitRadius / mapScale;
  125. this.minimapContext.strokeStyle = "#0000FF";
  126. this.minimapContext.beginPath();
  127. this.minimapContext.arc(minimapScaled.x, minimapScaled.y, orbit, 0, 2 * Math.PI);
  128. this.minimapContext.stroke();
  129. }
  130. }
  131. for(var index in game.systems) {
  132. var solar = game.systems[index];
  133. for(var planetIndex in solar.planets) {
  134. var planet = solar.planets[planetIndex];
  135. var minimapScaled = {x: planet.position.x / mapScale, y: planet.position.y / mapScale, planet: planet.radius / 10};
  136. this.minimapContext.fillStyle = planet.planetColor;
  137. this.minimapContext.beginPath();
  138. this.minimapContext.arc(minimapScaled.x - 1, minimapScaled.y - 1, 2,0, 2 * Math.PI);
  139. this.minimapContext.fill();
  140. }
  141. }
  142. this.minimapContext.strokeStyle = "#FFFFFF";
  143. this.minimapContext.beginPath();
  144. this.minimapContext.rect((this.ship.position.x / mapScale) - 5, (this.ship.position.y / mapScale) - 5, 10, 10);
  145. this.minimapContext.stroke();
  146. this.minimapContext.restore();
  147. return this.minimapCanvas;
  148. }
  149. this.drawRadar = function() {
  150. this.radarContext.clearRect(0, 0, this.radarCanvas.width, this.radarCanvas.height);
  151. var nearestSystem = game.getNearestSystem(game, this.ship);
  152. var nearestSystemAngle = game.calculateAngleTo(nearestSystem, this.ship);
  153. var nearestPlanet = game.getNearestPlanet(nearestSystem, this.ship);
  154. var nearestPlanetAngle = game.calculateAngleTo(nearestPlanet, this.ship);
  155. var nearestShip = game.getNearestShip(game, this.ship);
  156. var nearestShipAngle = game.calculateAngleTo(nearestShip, this.ship);
  157. //draw hud container
  158. this.radarContext.lineWidth = 1;
  159. this.radarContext.save();
  160. this.radarContext.translate(this.radarCanvas.width / 2, this.radarCanvas.height / 2);
  161. this.radarContext.lineWidth = 10;
  162. this.radarContext.strokeStyle = "#33AA33";
  163. this.radarContext.beginPath();
  164. var healthPercentage = Math.max(0, Math.min(this.ship.health / this.ship.maxHealth, 5));
  165. this.radarContext.arc(0, 0, 95, 0, (2 * Math.PI) * healthPercentage);
  166. this.radarContext.stroke();
  167. this.radarContext.lineWidth = 1;
  168. this.radarContext.strokeStyle = "#FFFFFF";
  169. this.radarContext.beginPath();
  170. this.radarContext.arc(0, 0, 100, 0, 2 * Math.PI);
  171. this.radarContext.stroke();
  172. this.radarContext.fillStyle = "#000000";
  173. this.radarContext.beginPath();
  174. this.radarContext.arc(0, 0, 90, 0, 2 * Math.PI);
  175. this.radarContext.fill();
  176. this.radarContext.stroke();
  177. this.radarContext.save();
  178. this.radarContext.translate(-40, -40);
  179. this.radarContext.scale(4, 4);
  180. this.ship.preRender(this.radarContext, this.ship.targetAngle);
  181. this.radarContext.restore();
  182. //draw nearest star
  183. this.radarContext.save();
  184. this.radarContext.rotate(nearestSystemAngle);
  185. this.radarContext.translate(75, 0);
  186. this.radarContext.strokeStyle = "#FFFF00";
  187. this.radarContext.beginPath();
  188. this.radarContext.arc(0, 0, nearestSystem.radius / 40, 0, 2 * Math.PI);
  189. this.radarContext.stroke();
  190. this.radarContext.restore();
  191. //draw nearest planet
  192. this.radarContext.save();
  193. this.radarContext.rotate(nearestPlanetAngle);
  194. this.radarContext.translate(78, 0);
  195. this.radarContext.strokeStyle = nearestPlanet.planetColor;
  196. this.radarContext.beginPath();
  197. this.radarContext.arc(0, 0, nearestPlanet.radius / 20, 0, 2 * Math.PI);
  198. this.radarContext.stroke();
  199. this.radarContext.restore();
  200. this.radarContext.save();
  201. this.radarContext.rotate(nearestShipAngle);
  202. this.radarContext.translate(70, 0);
  203. this.radarContext.scale(0.5, 0.5);
  204. nearestShip.preRender(this.radarContext, nearestShip.angleRadians);
  205. this.radarContext.restore();
  206. this.radarContext.restore();
  207. return this.radarCanvas;
  208. }
  209. };