gravitywellgame.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. function GravityWellGame() {
  2. this.game = null;
  3. this.system = null;
  4. this.player = null;
  5. this.ships = [];
  6. this.bullets = [];
  7. this.systems = [];
  8. this.planets = [];
  9. this.camera = null;
  10. this.background = null;
  11. this.farBackground = null;
  12. this.state = {shipIndex: 1, planetIndex: 1, solarIndex: 1};
  13. this.shipIndex = 1;
  14. this.planetIndex = 1;
  15. this.solarIndex = 1;
  16. this.settings = {};
  17. this.settings.enableMouseControl = true;
  18. this.tickDelay = 60;
  19. this.init = function(systemObject, canvas) {
  20. this.game = this;
  21. this.system = systemObject;
  22. this.camera = new Camera();
  23. this.camera.init(canvas);
  24. this.background = new Scenery(this.camera, 0.1);
  25. this.farBackground = new Scenery(this.camera, 0.05);
  26. this.systems.push(new SolarSystem(this.game, {x: -40000, y:-40000}));
  27. this.systems.push(new SolarSystem(this.game, {x: 0, y:-40000}));
  28. this.systems.push(new SolarSystem(this.game, {x: 40000, y:-40000}));
  29. this.systems.push(new SolarSystem(this.game, {x: -40000, y:0}));
  30. this.systems.push(new SolarSystem(this.game, {x: 0, y:0}));
  31. this.systems.push(new SolarSystem(this.game, {x: 40000, y:0}));
  32. this.systems.push(new SolarSystem(this.game, {x: -40000, y:40000}));
  33. this.systems.push(new SolarSystem(this.game, {x: 0, y:40000}));
  34. this.systems.push(new SolarSystem(this.game, {x: 40000, y:40000}));
  35. this.player = new Ship(this.game, new HumanController());
  36. this.player.id = this.state.shipIndex++;
  37. for(var index in this.systems) {
  38. var solar = this.systems[index];
  39. solar.id = this.state.solarIndex++;
  40. var solarPlanets = solar.spawnPlanets(this.state);
  41. var solarShips = solar.spawnShips(this.state);
  42. this.planets = this.planets.concat(solarPlanets);
  43. this.ships = this.ships.concat(solarShips);
  44. }
  45. var startingSystem = Math.min(Math.max(0, this.systems.length - 1), 4);
  46. var replaceShip = this.systems[startingSystem].ships[0];
  47. this.ships.splice(this.ships.indexOf(replaceShip), 1);
  48. this.ships.push(this.player);
  49. this.systems[startingSystem].ships[0] = this.player;
  50. for(var index in this.systems) {
  51. var solar = this.systems[index];
  52. solar.init();
  53. }
  54. this.ui = new HeadsUpDisplay(this.game, this.camera);
  55. this.ui.init(this.player, canvas);
  56. };
  57. this.tick = function() {
  58. for(var index in this.systems) {
  59. var solar = this.systems[index];
  60. solar.tick();
  61. }
  62. };
  63. this.updateDelta = function(canvas, delta){
  64. this.tickDelay -= delta;
  65. if(this.tickDelay <= 0) {
  66. this.tick();
  67. this.tickDelay = 60;
  68. }
  69. this.update(canvas);
  70. //this.player.update(delta);
  71. for(var index in this.ships) {
  72. var ship = this.ships[index];
  73. ship.update(delta);
  74. }
  75. for(var index in this.systems) {
  76. var solar = this.systems[index];
  77. solar.update(delta);
  78. }
  79. for(var index in this.bullets) {
  80. var bullet = this.bullets[index];
  81. bullet.update(delta);
  82. }
  83. this.camera.update(canvas, this.player, delta);
  84. this.farBackground.update(canvas, delta);
  85. this.background.update(canvas, delta);
  86. this.ui.update(delta);
  87. };
  88. this.update = function(canvas) {
  89. //this.player.control();
  90. for(var index in this.ships) {
  91. var ship = this.ships[index];
  92. ship.control();
  93. }
  94. //bullet-to-ship collision
  95. for(var index in this.bullets) {
  96. var bullet = this.bullets[index];
  97. for(var shipIndex in this.ships) {
  98. var ship = this.ships[shipIndex];
  99. if(!ship.isDead) {
  100. bullet.checkHit(ship);
  101. }
  102. }
  103. //todo: also check planets
  104. }
  105. for(var index in this.ships) {
  106. var ship = this.ships[index];
  107. if(ship.isDead) {
  108. continue;
  109. }
  110. //ship-to-ship collision
  111. for(var shipIndex in this.ships) {
  112. var otherShip = this.ships[shipIndex];
  113. if(ship == otherShip) {
  114. continue;
  115. }
  116. ship.checkHit(otherShip);
  117. }
  118. //ship-to-planet contact
  119. for(var systemIndex in this.systems) {
  120. var otherSystem = this.systems[systemIndex];
  121. otherSystem.checkCrash(ship);
  122. }
  123. }
  124. };
  125. this.draw = function(context) {
  126. this.farBackground.draw(context);
  127. this.background.draw(context);
  128. context.save();
  129. context.translate(this.camera.x, this.camera.y);
  130. for(var index in this.systems) {
  131. var solar = this.systems[index];
  132. solar.draw(context);
  133. }
  134. for(var index in this.ships) {
  135. var ship = this.ships[index];
  136. ship.draw(context);
  137. }
  138. for(var index in this.bullets) {
  139. var bullet = this.bullets[index];
  140. bullet.draw(context);
  141. }
  142. context.restore();
  143. this.ui.draw(context);
  144. };
  145. this.spawnBullet = function(ship) {
  146. var newBullet = new Bullet(this.game, ship.id);
  147. newBullet.fire(ship.position, ship.velocity, ship.targetAngle);
  148. this.bullets.push(newBullet);
  149. };
  150. this.expireBullet = function(bullet) {
  151. this.bullets.splice(this.bullets.indexOf(bullet), 1);
  152. }
  153. this.handleResize = function(canvas) {
  154. this.camera.handleResize(canvas);
  155. }
  156. this.mouseMove = function(canvas, x, y) {
  157. if(this.settings.enableMouseControl) {
  158. this.player.controller.mouseSteer(this.player, this.camera, x, y);
  159. }
  160. };
  161. this.mouseDown = function(canvas, button, x, y) {
  162. if(this.settings.enableMouseControl) {
  163. switch(button) {
  164. case 0:
  165. this.player.shoot();
  166. break;
  167. case 2:
  168. this.player.controller.mouseSteer(this.player, this.camera, x, y);
  169. this.player.accelerate();
  170. break;
  171. }
  172. }
  173. };
  174. this.mouseUp = function(canvas, button, x, y) {
  175. if(this.settings.enableMouseControl) {
  176. switch(button) {
  177. case 2:
  178. this.player.idle();
  179. break;
  180. }
  181. }
  182. };
  183. this.touchStart = function(canvas, x, y) {
  184. if(this.settings.enableMouseControl) {
  185. this.player.controller.mouseSteer(this.player, this.camera, x, y);
  186. this.player.accelerate();
  187. }
  188. };
  189. this.touchMove = function(canvas, x, y) {
  190. if(this.settings.enableMouseControl) {
  191. this.player.controller.mouseSteer(this.player, this.camera, x, y);
  192. this.player.accelerate();
  193. }
  194. };
  195. this.touchEnd = function(canvas, x, y) {
  196. if(this.settings.enableMouseControl) {
  197. this.player.idle();
  198. }
  199. };
  200. this.contextMenu = function(canvas, event) {
  201. };
  202. this.getNearestPlanet = function(solar, ship) {
  203. var closestDistance = Number.MAX_SAFE_INTEGER;
  204. var closestPlanet = null;
  205. for(var index in solar.planets) {
  206. var planet = solar.planets[index];
  207. var distance = this.calculateDistanceTo(ship, planet);
  208. if(distance < closestDistance) {
  209. closestPlanet = planet;
  210. closestDistance = distance;
  211. }
  212. }
  213. return closestPlanet;
  214. }
  215. this.getNearestSystem = function(game, ship) {
  216. var closestDistance = Number.MAX_SAFE_INTEGER;
  217. var closestSystem = null;
  218. for(var index in game.systems) {
  219. var solar = game.systems[index];
  220. var distance = this.calculateDistanceTo(ship, solar);
  221. if(distance < closestDistance) {
  222. closestSystem = solar;
  223. closestDistance = distance;
  224. }
  225. }
  226. return closestSystem;
  227. }
  228. this.getNearestShip = function(game, ship) {
  229. var closestDistance = Number.MAX_SAFE_INTEGER;
  230. var closestShip = null;
  231. for(var index in game.ships) {
  232. var other = game.ships[index];
  233. if(other == ship) {
  234. continue;
  235. }
  236. var distance = this.calculateDistanceTo(ship, other);
  237. if(distance < closestDistance) {
  238. closestShip = other;
  239. closestDistance = distance;
  240. }
  241. }
  242. return closestShip;
  243. }
  244. this.calculateDistanceTo = function(ship, other) {
  245. return Math.sqrt(Math.pow(ship.position.x - other.position.x, 2) + Math.pow(ship.position.y - other.position.y, 2));
  246. }
  247. this.calculateAngleTo = function(ship, other) {
  248. return Math.atan2(ship.position.y - other.position.y , ship.position.x - other.position.x);
  249. }
  250. this.calculateAngleToPlanet = function(ship, solar, planet) {
  251. return Math.atan2((solar.position.y + planet.position.y) - ship.position.y , (solar.position.x + planet.position.x) - ship.position.x);
  252. }
  253. };