asteroid-field.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var asteroidField = [];
  2. sets.push({
  3. id: "asteroid-field",
  4. setup: () => {
  5. if(!setsLoaded['asteroid-field']) {
  6. buildAsteroidField();
  7. setsLoaded['asteroid-field'] = true;
  8. }
  9. },
  10. update: (delta) => {
  11. updateAsteroidField(delta);
  12. },
  13. draw: (context, delta) => {
  14. drawAsteroidField(context, delta);
  15. },
  16. });
  17. function buildAsteroidField() {
  18. var asteroidSprites = [
  19. `meteorBrown_big1.png`,
  20. `meteorBrown_big2.png`,
  21. `meteorBrown_big3.png`,
  22. `meteorBrown_big4.png`,
  23. `meteorBrown_med1.png`,
  24. `meteorBrown_med3.png`,
  25. `meteorBrown_small1.png`,
  26. `meteorBrown_small2.png`,
  27. `meteorBrown_tiny1.png`,
  28. `meteorBrown_tiny2.png`,
  29. `meteorGrey_big1.png`,
  30. `meteorGrey_big2.png`,
  31. `meteorGrey_big3.png`,
  32. `meteorGrey_big4.png`,
  33. `meteorGrey_med1.png`,
  34. `meteorGrey_med2.png`,
  35. `meteorGrey_small1.png`,
  36. `meteorGrey_small2.png`,
  37. `meteorGrey_tiny1.png`,
  38. `meteorGrey_tiny2.png`,
  39. ];
  40. asteroidField = [];
  41. for (let i = 0; i < 100; i++) {
  42. let asteroid = {};
  43. let spriteIndex = Math.floor(asteroidSprites.length * Math.random());
  44. asteroid.sprite = asteroidSprites[spriteIndex];
  45. asteroid.x = Math.floor(starfield.width * Math.random()) - starfield.width / 2;
  46. asteroid.y = Math.floor(starfield.height * Math.random()) - starfield.height / 2;
  47. asteroid.angle = Math.floor(360 * Math.random());
  48. asteroid.rotationSpeed = (2 * Math.random()) - 1;
  49. asteroid.velocity = {};
  50. asteroid.velocity.x = 2 * Math.random() - 1;
  51. asteroid.velocity.y = 2 * Math.random() - 1;
  52. asteroidField.push(asteroid);
  53. }
  54. }
  55. function updateAsteroidField(delta) {
  56. for (let i = 0; i < asteroidField.length; i++) {
  57. let asteroid = asteroidField[i];
  58. asteroid.x += asteroid.velocity.x;
  59. asteroid.y += asteroid.velocity.y;
  60. asteroid.angle += asteroid.rotationSpeed;
  61. if (asteroid.x > starfield.width / 2) {
  62. asteroid.x -= starfield.width;
  63. }
  64. if (asteroid.y > starfield.height / 2) {
  65. asteroid.y -= starfield.height;
  66. }
  67. if (asteroid.x < -(starfield.width / 2)) {
  68. asteroid.x += starfield.width;
  69. }
  70. if (asteroid.y < -(starfield.height / 2)) {
  71. asteroid.y += starfield.height;
  72. }
  73. if (asteroid.angle > 360) {
  74. asteroid.angle -= 360;
  75. }
  76. if (asteroid.angle < 0) {
  77. asteroid.angle += 360;
  78. }
  79. }
  80. }
  81. function drawAsteroidField(context, delta) {
  82. for (let i = 0; i < asteroidField.length; i++) {
  83. let asteroid = asteroidField[i];
  84. context.save();
  85. context.translate(asteroid.x, asteroid.y);
  86. context.rotate(asteroid.angle * Math.PI / 180);
  87. atlas.drawCentered(context, asteroid.sprite, { x: 0, y: 0 });
  88. context.restore();
  89. }
  90. }