Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. var planets = [];
  2. var asteroids = [];
  3. var canvas;
  4. var ctx;
  5.  
  6. function spaceship(){
  7. this.location = new vector(500, 500);
  8. this.velocity = new vector(0, 0);
  9. this.power = 0.1;
  10. this.draw = function(){
  11. ctx.rect(this.location.x-10,this.location.y-10,20,20);
  12. ctx.stroke();
  13. }
  14. this.up = function(){
  15. this.velocity = this.velocity.add(new vector(0, -this.power));
  16. }
  17. this.down = function(){
  18. this.velocity = this.velocity.add(new vector(0, this.power));
  19. }
  20. this.left = function(){
  21. this.velocity = this.velocity.add(new vector(-this.power, 0));
  22. }
  23. this.right = function(){
  24. this.velocity = this.velocity.add(new vector(this.power, 0));
  25. }
  26. }
  27.  
  28. var ss = new spaceship();
  29.  
  30. function vector(x, y) {
  31. //konstruuje wektor
  32. this.x = x;
  33. this.y = y;
  34. this.add = function(v){
  35. return new vector(this.x+v.x, this.y+v.y);
  36. }
  37. this.sub = function(v){
  38. return new vector(this.x-v.x, this.y-v.y);
  39. }
  40. this.div = function(n){
  41. return
  42. }
  43. this.dist = function(v){
  44. dx = this.x - v.x;
  45. dy = this.y - v.y;
  46.  
  47. return Math.sqrt(dx*dx+dy*dy);
  48. }
  49. }
  50.  
  51.  
  52. function planet(location, size){
  53. //konstruuje planetę
  54. this.location = location;
  55. this.size = size;
  56. this.mass = size;
  57.  
  58. this.dist = function(v){
  59. return this.location.dist(v);
  60. }
  61. this.gravity = function(point){
  62. var tmp = this.location.sub(point);
  63. tmp.x = (this.mass/(tmp.x))/10000;
  64. tmp.y = (this.mass/(tmp.y))/10000;
  65. return tmp;
  66. }
  67. this.draw = function(){
  68. centerX = this.location.x;
  69. centerY = this.location.y;
  70. radius = this.size;
  71. ctx.beginPath();
  72. ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  73. ctx.fillStyle = 'green';
  74. ctx.fill();
  75. ctx.lineWidth = 5;
  76. ctx.strokeStyle = '#003300';
  77. ctx.stroke();
  78. }
  79. }
  80.  
  81. function asteroid(location, velocity){
  82. //konstruuje planetę
  83. this.location = location;
  84. this.velocity = velocity;
  85. this.draw = function(){
  86. centerX = this.location.x;
  87. centerY = this.location.y;
  88. radius = 5;
  89. ctx.beginPath();
  90. ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  91. ctx.fillStyle = 'red';
  92. ctx.fill();
  93. ctx.lineWidth = 5;
  94. ctx.strokeStyle = '#003300';
  95. ctx.stroke();
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. function tick(){
  104. //wykonuje sie co klatkę
  105. for (var i = asteroids.length - 1; i >= 0; i--) {
  106. asteroids[i].location = asteroids[i].location.add(asteroids[i].velocity);
  107. ss.location = ss.location.add(ss.velocity);
  108. for (var j = planets.length - 1; j >= 0; j--) {
  109. asteroids[i].velocity = asteroids[i].velocity.add(planets[j].gravity(asteroids[i].location));
  110. //ss.velocity = ss.velocity.add(planets[j].gravity(ss.location));
  111. ctx.moveTo(500,500);
  112. var tmp = planets[j].gravity(ss.location);
  113. ctx.lineTo(500+tmp.x*10000000,500+tmp.y*10000000);
  114. ctx.stroke();
  115. };
  116. };
  117.  
  118. //zmień prędkość statku
  119.  
  120.  
  121. }
  122.  
  123.  
  124. function draw(){
  125. //zaktualizuj canvas
  126. ctx.clearRect(0,0, window.innerWidth, window.innerHeight);
  127. ss.draw();
  128. for (var i = planets.length - 1; i >= 0; i--) {
  129. planets[i].draw();
  130. };
  131.  
  132. for (var i = asteroids.length - 1; i >= 0; i--) {
  133. asteroids[i].draw();
  134. };
  135. }
  136.  
  137. $(function() {
  138. //po załadowaniu
  139. $("body").keydown(function(e){
  140. //alert(e.keyCode);
  141. var k = e.keyCode;
  142. if(k == 38)
  143. ss.up();
  144. if(k == 40)
  145. ss.down();
  146. if(k == 37)
  147. ss.left();
  148. if(k == 39)
  149. ss.right();
  150. });
  151. canvas = document.getElementById("board");
  152. ctx = canvas.getContext("2d");
  153. ctx.canvas.width = window.innerWidth-20;
  154. ctx.canvas.height = window.innerHeight-20;
  155.  
  156. //planets.push(new planet(new vector(50, 50), 10));
  157. planets.push(new planet(new vector(750, 500), 1));
  158. // planets.push(new planet(new vector(300, 100), 10));
  159. // planets.push(new planet(new vector(100, 355), 10));
  160. // planets.push(new planet(new vector(70, 70), 10));
  161.  
  162. for(var i=0; i<10; i++){
  163. var location = new vector(Math.random()*1000, Math.random()*1000);
  164. var velocity = new vector((Math.random()-0.5)*1, (Math.random()-0.5)*1);
  165. asteroids.push(new asteroid(location, velocity));
  166. }
  167.  
  168.  
  169.  
  170.  
  171. var timer = setInterval(function () {
  172. tick();
  173. draw();
  174. }, 1000 / 60); // 60FPS
  175.  
  176.  
  177. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement