solarsystem.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. function SolarSystem(game, startPos) {
  2. this.id = 0;
  3. this.position = startPos;
  4. this.planets = [];
  5. this.ships = [];
  6. this.systemColor = null;
  7. this.radius = 400;
  8. this.mass = 150000;
  9. this.teamId = 0;
  10. this.init = function() {
  11. this.teamId = this.id;
  12. this.systemColor = this.getColor(this.teamId);
  13. for(var index in this.planets) {
  14. var planet = this.planets[index];
  15. planet.init(this);
  16. }
  17. var randomPlanet = this.planets[parseInt(this.planets.length * Math.random())];
  18. for(var index in this.ships) {
  19. var ship = this.ships[index];
  20. ship.init(this.systemColor, this);
  21. }
  22. }
  23. this.getColor = function(teamId) {
  24. switch(teamId) {
  25. case 1:
  26. return "crimson";
  27. case 2:
  28. return "orangered";
  29. case 3:
  30. return "gold";
  31. case 4:
  32. return "limegreen";
  33. case 5:
  34. return "dodgerblue";
  35. case 6:
  36. return "aqua";
  37. case 7:
  38. return "darkmagenta";
  39. case 8:
  40. return "ghostwhite";
  41. case 9:
  42. return "orchid";
  43. }
  44. }
  45. this.spawnPlanets = function(state){
  46. for(var i = 1; i <= 7; i++) {
  47. var isPlanet = parseInt(8 * Math.random()) > 4 || i == 4;
  48. if(isPlanet) {
  49. var planet = new Planet(game, i);
  50. planet.id = state.planetIndex++;
  51. if(i == 4) {
  52. planet.teamId = this.id;
  53. planet.teamColor = this.getColor(this.id);
  54. }
  55. this.planets.push(planet);
  56. }
  57. }
  58. return this.planets;
  59. }
  60. this.spawnShips = function(state){
  61. for(var i = 0; i < 4; i++) {
  62. var ship = new Ship(game, new BotController(game));
  63. ship.id = state.shipIndex++;
  64. this.ships.push(ship);
  65. }
  66. return this.ships;
  67. }
  68. this.tick = function() {
  69. for(var index in this.planets) {
  70. var planet = this.planets[index];
  71. planet.tick();
  72. }
  73. };
  74. this.update = function(delta) {
  75. for(var index in this.planets) {
  76. var planet = this.planets[index];
  77. planet.update(delta);
  78. }
  79. }
  80. this.draw = function(context) {
  81. //star
  82. context.fillStyle = "#000000";
  83. context.strokeStyle = "#DDDD33";
  84. context.lineWidth = 3;
  85. context.save();
  86. context.translate(this.position.x, this.position.y);
  87. context.beginPath();
  88. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  89. context.fill();
  90. context.stroke();
  91. context.restore();
  92. for(var index in this.planets) {
  93. var planet = this.planets[index];
  94. planet.draw(context);
  95. }
  96. context.save();
  97. context.translate(this.position.x, this.position.y);
  98. context.font = "16px sans-serif";
  99. context.textAlign = "center";
  100. context.fillStyle = "#DDDDDD";
  101. context.strokeStyle = "#333333";
  102. context.lineWidth = 2;
  103. var textMetrics = context.measureText("System " + this.id);
  104. var textWidth = textMetrics.width;
  105. context.fillText("System " + this.id, 0, 0);
  106. context.restore();
  107. }
  108. this.addGravity = function(ship) {
  109. var nearestSystemAngle = game.calculateAngleTo(this, ship);
  110. var nearestSystemDistance = game.calculateDistanceTo(this, ship);
  111. ship.acceleration.x += system.formatFloat((this.mass * (1 / Math.pow(nearestSystemDistance, 2))) * Math.cos(nearestSystemAngle));
  112. ship.acceleration.y += system.formatFloat((this.mass * (1 / Math.pow(nearestSystemDistance, 2))) * Math.sin(nearestSystemAngle));
  113. };
  114. this.checkCrash = function(ship) {
  115. if(ship.isDead) {
  116. return;
  117. }
  118. var distance = game.calculateDistanceTo(ship, this);
  119. if(this.radius + (ship.width / 2) > distance) {
  120. ship.touching = this;
  121. ship.die();
  122. return;
  123. }
  124. for(var planetIndex in this.planets) {
  125. var otherPlanet = this.planets[planetIndex];
  126. if(ship.touching == otherPlanet) {
  127. ship.touching = null;
  128. }
  129. var distance = game.calculateDistanceTo(ship, otherPlanet);
  130. if(otherPlanet.radius + (ship.width / 2) > distance) {
  131. ship.touching = otherPlanet;
  132. if(Math.abs(ship.velocity.x) + Math.abs(ship.velocity.y) > 3) {
  133. ship.die();
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. };