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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const keys = {};
  2. document.addEventListener('keydown', (e) => {
  3. keys[e.key] = true;
  4. });
  5. document.addEventListener('keyup', (e) => {
  6. keys[e.key] = false;
  7. });
  8. let blink = false;
  9. setInterval(() => blink = !blink, 200);
  10. const MAX_LIVES = 3;
  11. const INVULNERABILITY_INTERVAL = 2000;
  12. class ShootingPad extends Entity {
  13. constructor() {
  14. super(0.5, 0.95, 0.04, 0.03);
  15. const image = new Image();
  16. image.src = 'img/shooting-pad.svg';
  17. image.onload = () => this.image = image;
  18. this.lastShootTime = Date.now();
  19. this.invulnerable = false;
  20. this.lives = MAX_LIVES;
  21. this.initLives();
  22. }
  23. update() {
  24. if (keys['ArrowLeft'] || keys['a']) {
  25. this.x -= VELOCITY[0] * 2;
  26. }
  27. if (keys['ArrowRight'] || keys['d']) {
  28. this.x += VELOCITY[0] * 2;
  29. }
  30. if (this.x < X_BOUND) {
  31. this.x = X_BOUND;
  32. }
  33. if (this.x > 1 - X_BOUND) {
  34. this.x = 1 - X_BOUND;
  35. }
  36. if (keys[' '] && Date.now() - this.lastShootTime >= 300) {
  37. this.lastShootTime = Date.now();
  38. const bullet = new Bullet(this.x, this.y - 0.009);
  39. bullets.add(bullet);
  40. }
  41. }
  42. draw() {
  43. if (!this.invulnerable || blink) {
  44. super.draw();
  45. }
  46. }
  47. initLives() {
  48. $('#lives').textContent = `× ${this.lives}`;
  49. }
  50. lifeOver() {
  51. if (this.invulnerable) return;
  52. this.lives--;
  53. this.initLives();
  54. if (this.lives === 0) {
  55. return gameOver('Game over');
  56. } else {
  57. this.invulnerable = true;
  58. setTimeout(() => {
  59. this.invulnerable = false;
  60. }, INVULNERABILITY_INTERVAL);
  61. }
  62. }
  63. }