planet.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. function Planet(game, depth) {
  2. this.id = 0;
  3. this.position = {x: 800, y: 0};
  4. this.velocity = {x: 0, y: 0};
  5. this.angleRadians = 0;
  6. this.fireRate = 5;
  7. this.lifetime = 100;
  8. this.timeToDie = 0;
  9. this.isDead = false;
  10. this.planetColor = "#AAAAAA";
  11. this.radius = 100;
  12. this.orbitRadius = 2000;
  13. this.angleRadians = 0;
  14. this.orbitSpeed = 1;
  15. this.rotationRadians = 0;
  16. this.rotationSpeed = 0.001;
  17. this.system = null;
  18. this.mass = 1000;
  19. this.teamId = 0;
  20. this.teamColor = "#DDDDDD";
  21. this.type = "barren";
  22. this.resources = 50;
  23. this.resourceRate = 0;
  24. this.init = function(solar) {
  25. this.system = solar;
  26. this.orbitRadius = 2000 * (depth + 1);
  27. this.angleRadians = (2 * Math.PI) * Math.random();
  28. this.position.x = solar.position.x + (this.orbitRadius * Math.cos(this.angleRadians));
  29. this.position.y = solar.position.y + (this.orbitRadius * Math.sin(this.angleRadians));
  30. this.radius = 100;
  31. this.mass = 1000;
  32. this.type = "barren";
  33. if(depth > 2 && depth < 6) {
  34. this.planetColor = "#33DD33";
  35. this.radius = 140;
  36. this.mass = 2800;
  37. this.type = "habitable";
  38. this.resourceRate = 1;
  39. }
  40. this.rotationRadians = (2 * Math.PI) * Math.random();
  41. //this.orbitSpeed = 0.00001 * Math.pow(1/1000, depth);
  42. this.orbitSpeed = 0.00001;// * Math.pow(1/10, depth);
  43. }
  44. this.tick = function() {
  45. this.resources += this.resourceRate;
  46. };
  47. this.update = function(delta) {
  48. this.angleRadians += this.orbitSpeed;
  49. this.rotationRadians += this.rotationSpeed;
  50. this.position.x = this.system.position.x + (this.orbitRadius * Math.cos(this.angleRadians));
  51. this.position.y = this.system.position.y + (this.orbitRadius * Math.sin(this.angleRadians));
  52. }
  53. this.draw = function(context) {
  54. var solar = {x: 0, y: 0};
  55. solar.x = this.system.position.x;
  56. solar.y = this.system.position.y;
  57. //context.save();
  58. //context.translate(solar.x, solar.y);
  59. //context.rotate(this.angleRadians);
  60. context.save();
  61. context.fillStyle = "#000000";
  62. context.translate(this.position.x, this.position.y);
  63. context.beginPath();
  64. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  65. context.fill();
  66. context.restore();
  67. context.strokeStyle = "#3333DD";
  68. context.lineWidth = 1;
  69. context.save();
  70. context.translate(this.system.position.x, this.system.position.y);
  71. context.beginPath();
  72. context.arc(0, 0, this.orbitRadius, 0, 2 * Math.PI);
  73. context.stroke();
  74. context.restore();
  75. context.save();
  76. context.strokeStyle = this.planetColor;
  77. context.lineWidth = 2;
  78. context.translate(this.position.x, this.position.y);
  79. context.rotate(this.rotationRadians);
  80. context.beginPath();
  81. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  82. context.stroke();
  83. //for(var index in this.structures) {
  84. // var building = this.structures[index];
  85. // building.draw(context);
  86. //}
  87. /*context.beginPath();
  88. context.fillStyle = "#33BBBB";
  89. context.strokeStyle = "#33DDDD";
  90. context.rect(10, 10, 50, 30);
  91. context.fill();
  92. context.stroke();
  93. context.restore();*/
  94. context.restore();
  95. context.save();
  96. context.translate(this.position.x, this.position.y);
  97. //ownership flag
  98. context.beginPath();
  99. context.fillStyle = this.teamColor;
  100. context.rect(-8, -6, 16, 12);
  101. context.fill();
  102. context.font = "16px sans-serif";
  103. context.textAlign = "center";
  104. context.fillStyle = "#DDDDDD";
  105. context.strokeStyle = "#333333";
  106. context.lineWidth = 2;
  107. var textMetrics = context.measureText("Planet " + this.id);
  108. var textWidth = textMetrics.width;
  109. context.fillText("Planet " + this.id, 0, 0);
  110. context.restore();
  111. }
  112. this.addGravity = function(ship) {
  113. var nearestPlanetAngle = game.calculateAngleTo(this, ship);
  114. var nearestPlanetDistance = game.calculateDistanceTo(this, ship);
  115. ship.acceleration.x += system.formatFloat((this.mass * (1 / Math.pow(nearestPlanetDistance, 2))) * Math.cos(nearestPlanetAngle));
  116. ship.acceleration.y += system.formatFloat((this.mass * (1 / Math.pow(nearestPlanetDistance, 2))) * Math.sin(nearestPlanetAngle));
  117. };
  118. this.claim = function(ship) {
  119. this.teamId = ship.teamId;
  120. this.teamColor = ship.shipColor;
  121. ship.setSpawn(this);
  122. };
  123. this.canSpawnShip = function() {
  124. return this.resources > 10;
  125. };
  126. this.spawnShip = function() {
  127. this.resources -= 10;
  128. }
  129. };