Advertisement
Guest User

asteroid wont spawn

a guest
Apr 1st, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.11 KB | None | 0 0
  1.  
  2. // creates a canvas to play on
  3. var canvas = document.getElementById("gameCanvas");
  4. var context = canvas.getContext("2d");
  5.  
  6. /////////////////////////////////////////////////////////////DELTA TIME///////////////////////////////////////////////////////////////////////////////////
  7. var startFrameMillis = Date.now();
  8. var endFrameMillis = Date.now();
  9. function getDeltaTime() // Only call this function once per frame
  10. {
  11. endFrameMillis = startFrameMillis;
  12. startFrameMillis = Date.now();
  13. var deltaTime = (startFrameMillis - endFrameMillis) * 0.001;
  14. if (deltaTime > 1) // validate that the delta is within range
  15. {
  16. deltaTime = 1;
  17. }
  18. return deltaTime;
  19. }
  20. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21.  
  22.  
  23. // load the image to use for the tiled background
  24. var grass = document.createElement("img");
  25. grass.src = "grass.png";
  26.  
  27.  
  28. // create the tiled background
  29. var background = [];
  30. for(var y = 0; y < 15; y++)
  31. {
  32. background[y] = [];
  33. for(var x = 0; x < 20; x++)
  34. background[y][x] = grass;
  35. }
  36.  
  37. //draw an awesome spacey background on top of tiles and canvas
  38. var backgroundImage = document.createElement("img");
  39. backgroundImage.src = "space.pic.png";
  40.  
  41.  
  42.  
  43. // set up some event handlers to process keyboard input
  44. window.addEventListener('keydown', function(evt) { onKeyDown(evt); }, false);
  45. window.addEventListener('keyup', function(evt) { onKeyUp(evt); }, false);
  46.  
  47.  
  48.  
  49. var SCREEN_WIDTH = canvas.width;
  50. var SCREEN_HEIGHT = canvas.height;
  51. var ASTEROID_SPEED = 200;
  52. var PLAYER_SPEED = 100;
  53. var PLAYER_TURN_SPEED = 0.05;
  54. var BULLET_SPEED = 200;
  55.  
  56.  
  57.  
  58. // Player image reference & position
  59. // this creates the player object and assigns it some properties
  60. var player = {
  61. image: document.createElement("img"),
  62. x:SCREEN_WIDTH/2,
  63. y:SCREEN_HEIGHT/2,
  64. width: 93,
  65. height: 80,
  66. directionX: 0,
  67. directionY: 0,
  68. angularDirection: 0,
  69. rotation: 0,
  70. isDead: false
  71. };
  72. // set the image for the player to use
  73. player.image.src = "ship.png";
  74.  
  75.  
  76. // create the asteroids array or collection
  77. var asteroids = [];
  78. for(var i=0; i<8; i++)
  79. {
  80. document.writeln(asteroids[i] + "</BR>")
  81. }
  82.  
  83. // rand(floor, ceil)
  84. // Return a random number within the range of the two input variables
  85.  
  86. function rand(floor, ceil)
  87. {
  88. return Math.floor( (Math.random()* (ceil-floor)) +floor );
  89. }
  90.  
  91.  
  92. // Create a new random asteroid and add it to our asteroids array.
  93. // We'll give the asteroid a random position (just off screen) and
  94. // set it moving towards the center of the screen
  95.  
  96. spawnTimer = 0;
  97. function spawnAsteroid()
  98. {
  99. // make a random variable to specify which asteroid image to use
  100. // (small, mediam or large)
  101. var type = rand(0,3);
  102.  
  103. // create the new asteroid
  104.  
  105. var asteroid = {};
  106. asteroid.image = document.createElement("img");
  107. asteroid.image.src = "rock_large.png";
  108. asteroid.width = 69;
  109. asteroid.height = 75;
  110.  
  111. // to set a random position just off screen, we'll start at the centre of the
  112. // screen then move in a random direction by the width of the screen
  113.  
  114. var x = SCREEN_WIDTH/2;
  115. var y = SCREEN_HEIGHT/2;
  116. var dirX = rand(-10,10);
  117. var dirY = rand(-10,10);
  118. // 'normalize' the direction (the hypotenuse of the triangle formed
  119. // by x,y will equal 1)
  120. var magnitude = (dirX * dirX) + (dirY * dirY);
  121.  
  122. if(magnitude != 0)
  123. {
  124. var oneOverMag = 1 / Math.sqrt(magnitude);
  125. dirX *= oneOverMag;
  126. dirY *= oneOverMag;
  127. }
  128.  
  129. // now we can multiply the dirX/Y by the screen width to move that amount from
  130. // the centre of the screen
  131.  
  132. var movX = dirX * SCREEN_WIDTH;
  133. var movY = dirY * SCREEN_HEIGHT;
  134.  
  135. // add the direction to the original position to get the starting position of the
  136. // asteroid
  137.  
  138. asteroid.x = x + movX;
  139. asteroid.y = y + movY;
  140.  
  141. // now, the easy way to set the velocity so that the asteroid moves towards the
  142. // centre of the screen is to just reverse the direction we found earlier
  143.  
  144. asteroid.velocityX = -dirX * ASTEROID_SPEED;
  145. asteroid.velocityY = -dirY * ASTEROID_SPEED;
  146.  
  147. // finally we can add our new asteroid to the end of our asteroids array
  148.  
  149. asteroids.push(asteroid);
  150. }
  151.  
  152.  
  153. // Bullet Array or collection & timer
  154. var bullets = [];
  155.  
  156. var shootTimer = 0;
  157.  
  158. function playerShoot()
  159. {
  160. var bullet = {
  161. image: document.createElement("img"),
  162. x: player.x,
  163. y: player.y,
  164. width: 5,
  165. height: 5,
  166. velocityX: 0,
  167. velocityY: 0
  168. };
  169. bullet.image.src = "bullet.png";
  170.  
  171. var velX = 0;
  172. var velY = 1;
  173.  
  174. var s = Math.sin( player.rotation );
  175. var c = Math.cos( player.rotation );
  176.  
  177. var xVel = ( velX * c ) - ( velY * s );
  178. var yVel = ( velX * s ) + ( velY * c );
  179.  
  180. bullet.velocityX = xVel * BULLET_SPEED;
  181. bullet.velocityY = yVel * BULLET_SPEED;
  182.  
  183. bullets.push ( bullet );
  184. }
  185.  
  186.  
  187. // key constants
  188. // Go here for a list of key codes:
  189. // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
  190. var KEY_SPACE = 32;
  191. var KEY_LEFT = 37;
  192. var KEY_UP = 38;
  193. var KEY_RIGHT = 39;
  194. var KEY_DOWN = 40;
  195. var KEY_A = 65;
  196. var KEY_D = 68;
  197.  
  198.  
  199. // Key functions
  200. function onKeyDown(event)
  201. {
  202. if(event.keyCode == KEY_UP)
  203. {
  204. player.directionY = 1;
  205. }
  206. if(event.keyCode == KEY_DOWN)
  207. {
  208. player.directionY = -1;
  209. }
  210. if(event.keyCode == KEY_LEFT)
  211. {
  212. player.angularDirection = -1;
  213. }
  214. if(event.keyCode == KEY_RIGHT)
  215. {
  216. player.angularDirection = 1;
  217. }
  218. if(event.keyCode == KEY_A)
  219. {
  220. player.directionX = 1;
  221. }
  222. if(event.keycode == KEY_D)
  223. {
  224. player.directionX = -1;
  225. }
  226. if(event.keyCode == KEY_SPACE && shootTimer <= 0)
  227. {
  228. shootTimer += 0.2;
  229. playerShoot();
  230. }
  231. }
  232.  
  233.  
  234.  
  235. function onKeyUp(event)
  236. {
  237. if(event.keyCode == KEY_UP)
  238. {
  239. player.directionY = 0;
  240. }
  241. if(event.keyCode == KEY_DOWN)
  242. {
  243. player.directionY = 0;
  244. }
  245. if(event.keyCode == KEY_LEFT)
  246. {
  247. player.angularDirection = 0;
  248. }
  249. if(event.keyCode == KEY_RIGHT)
  250. {
  251. player.angularDirection = 0;
  252. }
  253. if(event.keyCode == KEY_A)
  254. {
  255. player.directionX = 0;
  256. }
  257. if(event.keycode == KEY_D)
  258. {
  259. player.directionX = 0;
  260. }
  261. if(event.keyCode == KEY_SPACE)
  262. {
  263.  
  264. }
  265. }
  266.  
  267. // tests if two rectangles are intersecting.
  268. // Pass in the x,y coordinates, width and height of each rectangle.
  269. // Returns 'true' if the rectangles are intersecting
  270. function intersects(x1, y1, w1, h1, x2, y2, w2, h2)
  271. {
  272. if( y2 + h2 < y1 ||
  273. x2 + w2 < x1 ||
  274. x2 > x1 + w1 ||
  275. y2 > y1 + h1 )
  276. {
  277. return false;
  278. }
  279. return true;
  280. }
  281.  
  282.  
  283. ////////////////////////////////////////////////////////////////FUNCTION RUN/////////////////////////////////////////////////////////////////////////////
  284. function run()
  285. {
  286. context.fillStyle = "#ccc";
  287. context.fillRect(0, 0, canvas.width, canvas.height);
  288. context.drawImage ( backgroundImage, 0, 0 )
  289.  
  290.  
  291. var deltaTime = getDeltaTime();
  292.  
  293. // draw the tiled background (make sure you do this first, so everything
  294. // else in the scene will be drawn on top of the tiled background)
  295.  
  296. for (var y = 0; y < 15; y++)
  297. {
  298. for (var x = 0; x < 20; x++)
  299. {
  300. context.drawImage(background[y][x], x*32, y*32);
  301. }
  302. }
  303. context.drawImage ( backgroundImage, 0, 0 )
  304. // update the shoot timer
  305.  
  306. if( shootTimer > 0 )
  307. {
  308. shootTimer -= deltaTime;
  309. }
  310.  
  311. // update all the bullets
  312.  
  313. for ( var i=0; i < bullets.length; i++ )
  314. {
  315. bullets[i].x += bullets[i].velocityX * deltaTime;
  316. bullets[i].y += bullets[i].velocityY * deltaTime;
  317. }
  318.  
  319. // check if the bullet has gone out of the screen boundaries
  320. // and if so kill it
  321. for ( var i = 0; i < bullets.length; i++)
  322. {
  323. if ( bullets[i].x < -bullets[i].width ||
  324. bullets[i].x > SCREEN_WIDTH ||
  325. bullets[i].y < -bullets[i].height ||
  326. bullets[i].y > SCREEN_HEIGHT)
  327. {
  328. bullets.splice( i, 1);
  329. break;
  330. }
  331. }
  332.  
  333. // draw all the bullets
  334. for ( var i = 0; i < bullets.length; i++ )
  335. {
  336. context.drawImage( bullets[i].image,
  337. bullets[i].x - bullets[i].width/2,
  338. bullets[i].y - bullets[i].height/2);
  339. }
  340.  
  341. // update all the asteroids - Spawner
  342.  
  343. for(var i = 0; i < asteroids.length; i++)
  344. {
  345. var velX = asteroids[i].dirX;
  346. var velY = asteroids[i].dirY;
  347. asteroids[i].posX = asteroids[i].posX + velX * deltaTime;
  348. asteroids[i].posY = asteroids[i].posY + velY * deltaTime;
  349. }
  350. {
  351. for (var i = 0; i < asteroids.length; i++)
  352. {
  353. context.drawImage( asteroids[i].image, asteroids[i].width/2, asteroids[i].height/2);
  354. context.beginPath();
  355. context.rect(asteroids[i].posX-(asteroids[i].width/2), asteroids[i].posY-(asteroids[i].height/2),
  356. asteroids[i].width, asteroids[i].height);
  357. context.stroke();
  358. }
  359.  
  360. spawnTimer -= deltaTime;
  361.  
  362. if (spawnTimer <= 0)
  363. {
  364. spawnTimer = 1;
  365. spawnAsteroid();
  366. }
  367. }
  368.  
  369.  
  370. //////////////////player positions
  371. if (player.isDead == false)
  372. // calculate sin and cos for the player's current rotation
  373. {
  374. var s = Math.sin(player.rotation);
  375. var c = Math.cos(player.rotation);
  376.  
  377. // figure out what the player's velocity will be based on the current rotation
  378. // (so that if the ship is moving forward, then it will move forward according to its
  379. // rotation. Without this, the ship would just move up the screen when we pressed 'up',
  380. // regardless of which way it was rotated)
  381. // for an explanation of this formula, see http://en.wikipedia.org/wiki/Rotation_matrix
  382. var xDir = (player.directionX * c) - (player.directionY * s);
  383. var yDir = (player.directionX * s) + (player.directionY * c);
  384. var xVel = xDir * PLAYER_SPEED * deltaTime;
  385. var yVel = yDir * PLAYER_SPEED * deltaTime;
  386. player.x += xVel;
  387. player.y += yVel;
  388. player.rotation += player.angularDirection * PLAYER_TURN_SPEED;
  389.  
  390. context.save();
  391. context.translate(player.x, player.y);
  392. context.rotate(player.rotation);
  393. context.drawImage(
  394. player.image, -player.width/2, -player.height/2);
  395. context.restore();
  396. }
  397.  
  398. //Wrapping Player Experiments
  399. if (player.x > SCREEN_WIDTH)
  400. {
  401. player.x = 0;
  402. }
  403. if (player.x < 0)
  404. {
  405. player.x = SCREEN_WIDTH;
  406. }
  407. if (player.y > SCREEN_HEIGHT)
  408. {
  409. player.y = 0;
  410. }
  411. if (player.y < 0)
  412. {
  413. player.y = SCREEN_HEIGHT;
  414. }
  415.  
  416. //Player to Asteroid Collision Checking
  417. for (var i = 0; i < asteroids.length; i++)
  418. {
  419. if (player.isDead == false)
  420. {
  421. if (intersects (player.x, player.y, player.height/2, player.width/2,
  422. asteroids[i].x, asteroids[i].y, asteroids[i].width, asteroids[i].height) == true)
  423. {
  424. asteroids.splice( i, 1);
  425. player.isDead = true;
  426. break;
  427. }
  428. }
  429. }
  430.  
  431.  
  432. //Bullet to Asteroid Collision Checking
  433. for (var i = 0; i < asteroids.length; i++)
  434. {
  435. for (var j = 0; j < bullets.length; j++)
  436. {
  437. if (intersects (bullets[j].x, bullets[j].y, bullets[j].width, bullets[j].height,
  438. asteroids[i].x, asteroids[i].y, asteroids[i].width, asteroids[i].height) == true)
  439. {
  440. asteroids.splice( i, 1);
  441. bullets.splice( j, 1);
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. //60FPS framework
  448. (function()
  449. {
  450. var onEachFrame;
  451.  
  452. if (window.requestAnimationFrame)
  453. {
  454. onEachFrame = function(cb)
  455. {
  456. var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
  457. _cb();
  458. };
  459.  
  460. }
  461. else if (window.mozRequestAnimationFrame)
  462. {
  463. onEachFrame = function(cb) {
  464. var _cb = function() { cb(); window.mozRequestAnimationFrame(_cb); }
  465. _cb();
  466. };
  467.  
  468. }
  469. else
  470. {
  471. onEachFrame = function(cb)
  472. {
  473. setInterval(cb, 1000 / 60);
  474. }
  475. }
  476.  
  477. window.onEachFrame = onEachFrame;
  478. })();
  479.  
  480. window.onEachFrame(run);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement