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.

shooting_pad.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const MAX_LIVES = 3;
  9. class ShootingPad extends Entity {
  10. constructor() {
  11. super(0.5, 0.95, 0.04, 0.03);
  12. this.element.className = 'shooting-pad';
  13. this.lastShootTime = Date.now();
  14. this.invulnerable = false;
  15. this.lives = MAX_LIVES;
  16. this.initLives();
  17. this.element.addEventListener('animationend', () => {
  18. this.element.style.animationName = null;
  19. this.invulnerable = false;
  20. });
  21. }
  22. update() {
  23. if (keys['ArrowLeft']) {
  24. this.x -= VELOCITY[0] * 2;
  25. }
  26. if (keys['ArrowRight']) {
  27. this.x += VELOCITY[0] * 2;
  28. }
  29. if (this.x < X_BOUND) {
  30. this.x = X_BOUND;
  31. }
  32. if (this.x > 1 - X_BOUND) {
  33. this.x = 1 - X_BOUND;
  34. }
  35. if (keys[' '] && Date.now() - this.lastShootTime >= 300) {
  36. this.lastShootTime = Date.now();
  37. const bullet = new Bullet(this.x, this.y - 0.009);
  38. bullets.add(bullet);
  39. }
  40. }
  41. initLives() {
  42. $('#lives').textContent = `× ${this.lives}`;
  43. }
  44. lifeOver() {
  45. if (this.invulnerable) return;
  46. this.lives--;
  47. this.initLives();
  48. if (this.lives === 0) {
  49. return gameOver('Game over');
  50. } else {
  51. this.invulnerable = true;
  52. this.element.style.animationName = 'blink';
  53. }
  54. }
  55. }