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 913B

123456789101112131415161718192021222324252627282930313233343536
  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. class ShootingPad extends Entity {
  9. constructor() {
  10. super(0.5, 0.95, 0.04, 0.03);
  11. this.element.className = 'shooting-pad';
  12. this.lastShootTime = Date.now();
  13. }
  14. update() {
  15. if (keys['ArrowLeft']) {
  16. this.x -= VELOCITY[0] * 2;
  17. }
  18. if (keys['ArrowRight']) {
  19. this.x += VELOCITY[0] * 2;
  20. }
  21. if (this.x < X_BOUND) {
  22. this.x = X_BOUND;
  23. }
  24. if (this.x > 1 - X_BOUND) {
  25. this.x = 1 - X_BOUND;
  26. }
  27. if (keys[' '] && Date.now() - this.lastShootTime >= 300) {
  28. this.lastShootTime = Date.now();
  29. const bullet = new Bullet(this.x, this.y - 0.009);
  30. bullets.add(bullet);
  31. }
  32. }
  33. }