Game for OLC Code Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const $ = (s) => document.querySelector(s);
  2. const $$ = (s) => Array.from(document.querySelectorAll(s));
  3. const ROOT = $('#root');
  4. const clouds = new Set();
  5. const bullets = new Set();
  6. let shootingPad = new ShootingPad();
  7. const X_BOUND = 0.05;
  8. let running = true;
  9. let direction = 1;
  10. let lives = 3;
  11. const initCloudField = () => {
  12. for (let y = 0; y < 6; y++) {
  13. for (let x = 0; x < 11; x++) {
  14. const cloud = new Cloud(X_BOUND + 0.06 * x, 0.04 + 0.04 * y, 0.03, 0.02);
  15. clouds.add(cloud);
  16. }
  17. }
  18. }
  19. const start = () => {
  20. $('.result')?.remove();
  21. shootingPad?.destroy();
  22. shootingPad = new ShootingPad();
  23. bullets.forEach(c => c.destroy());
  24. bullets.clear();
  25. clouds.forEach(c => c.destroy());
  26. clouds.clear();
  27. Cloud.reset();
  28. initCloudField();
  29. running = true;
  30. loop();
  31. }
  32. const gameOver = (result) => {
  33. const resultLabel = document.createElement('div');
  34. resultLabel.className = 'result';
  35. resultLabel.textContent = result;
  36. ROOT.appendChild(resultLabel);
  37. running = false;
  38. }
  39. const loop = () => {
  40. if (!running) return;
  41. let directionUpdated = false;
  42. Array.from(clouds).forEach(c => {
  43. c.update(direction);
  44. c.draw();
  45. if (c.x < X_BOUND - 0.01 || c.x > 1.01 - X_BOUND) {
  46. directionUpdated = true;
  47. }
  48. if (!c.alive) {
  49. clouds.delete(c);
  50. }
  51. });
  52. if (directionUpdated) {
  53. direction = -direction;
  54. }
  55. Array.from(bullets).forEach(b => {
  56. b.update();
  57. b.draw();
  58. if (!b.alive) {
  59. bullets.delete(b);
  60. }
  61. });
  62. shootingPad.update();
  63. shootingPad.draw();
  64. if (clouds.size === 0) {
  65. gameOver('WIN');
  66. }
  67. requestAnimationFrame(loop);
  68. };
  69. document.addEventListener('keypress', () => {
  70. if (!running) {
  71. start();
  72. }
  73. });
  74. start();