diff --git a/script.js b/script.js index 293a6c0..454755e 100644 --- a/script.js +++ b/script.js @@ -9,21 +9,38 @@ constructor(game) { this.game = game; window.addEventListener('keydown', e => { - if ((e.key === 'ArrowUp') && (this.game.keys.indexOf(e.key) === -1)) { + if (((e.key === 'ArrowUp') || (e.key === 'ArrowDown') ) + && (this.game.keys.indexOf(e.key) === -1)) { this.game.keys.push(e.key); + } else if ( e.key === ' ' ) { + this.game.player.shootTop(); } - console.log(this.game.keys); }); window.addEventListener('keyup', e => { if (this.game.keys.indexOf(e.key) > -1) { this.game.keys.splice(this.game.keys.indexOf(e.key), 1); } - console.log(this.game.keys); }); } } class Projectile { - + constructor(game, x, y) { + this.game = game; + this.x = x; + this.y = y; + this.width = 10; + this.height = 3; + this.speed = 3; + this.markedForDeletion = false; + } + update() { + this.x += this.speed; + if (this.x > this.game.wdith * 0.8) { this.markedForDeletion = true; } + } + draw(context) { + context.fillStyle = 'yellow'; + context.fillRect(this.x, this.y, this.width, this.height); + } } class Particle { @@ -36,12 +53,33 @@ this.x = 20; this.y = 100; this.speedY = 0; + this.maxSpeed = 2; + this.projectiles = []; } update() { + if (this.game.keys.includes('ArrowUp')) { this.speedY = -this.maxSpeed} + else if (this.game.keys.includes('ArrowDown')) { this.speedY = this.maxSpeed} + else { this.speedY = 0; } this.y += this.speedY; + // handle projectiles + this.projectiles.forEach(projectile => { + projectile.update(); + }); + this.projectiles = this.projectiles.filter(projectile => !projectile.markedForDeletion); } draw(context) { + context.fillStyle = 'black'; context.fillRect(this.x, this.y, this.width, this.height); + this.projectiles.forEach(projectile => { + projectile.draw(context); + }); + } + shootTop() { + if ( this.game.ammo > 0 ) { + this.projectiles.push(new Projectile(this.game, this.x + 80, this.y + 30)); + this.game.ammo--; + } + } } class Enemy { @@ -63,6 +101,7 @@ this.player = new Player(this); this.input = new InputHandler(this); this.keys = []; + this.ammo = 20; } update() { this.player.update();