From 0b2f4108240c0829cf5e1488d805d0886a0fb6f7 Mon Sep 17 00:00:00 2001 From: KGrzeg Date: Thu, 14 Oct 2021 18:41:45 +0200 Subject: [PATCH] Add cooldown and bullet angle --- src/classes/Bullet.ts | 33 ++++++++++++++++++++++----------- src/classes/Bullets.ts | 22 +++++++++++++++------- src/classes/Ship.ts | 12 ++++++------ src/main.ts | 2 +- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/classes/Bullet.ts b/src/classes/Bullet.ts index afaa0b5..432d7cc 100644 --- a/src/classes/Bullet.ts +++ b/src/classes/Bullet.ts @@ -1,25 +1,36 @@ import Phaser from 'phaser' export default class Bullet extends Phaser.Physics.Arcade.Sprite { + readonly lifetime = 4000 //ms + readonly speed = 600 + + age = 0 + constructor(scene, x, y) { - super(scene, x, y, 'bullet'); + super(scene, x, y, 'bullet') } - fire(x, y) { - this.body.reset(x, y); + fire(x: number, y: number, rotation: number) { + this.body.reset(x, y) + this.rotation = rotation - this.setActive(true); - this.setVisible(true); + this.setActive(true) + this.setVisible(true) - this.setVelocityY(-300); + const v = new Phaser.Math.Vector2(0, -this.speed) + v.rotate(rotation) + this.setVelocity(v.x, v.y) + this.age = 0 } - preUpdate(time, delta) { - super.preUpdate(time, delta); + preUpdate(time: number, delta: number) { + super.preUpdate(time, delta) - if (this.y <= -32) { - this.setActive(false); - this.setVisible(false); + this.age += delta + + if (this.age >= this.lifetime) { + this.setActive(false) + this.setVisible(false) } } } diff --git a/src/classes/Bullets.ts b/src/classes/Bullets.ts index 5c559c6..59bb71b 100644 --- a/src/classes/Bullets.ts +++ b/src/classes/Bullets.ts @@ -1,24 +1,32 @@ import Phaser from 'phaser' import Bullet from './Bullet' +import Ship from './Ship' export default class Bullets extends Phaser.Physics.Arcade.Group { - constructor(scene: Phaser.Scene) { - super(scene.physics.world, scene); + readonly cooldown: number //ms + + lastShoot = 0 + + constructor(scene: Phaser.Scene, fireRate: number) { + super(scene.physics.world, scene) this.createMultiple({ - frameQuantity: 5, + frameQuantity: 30, key: 'bullet', active: false, visible: false, classType: Bullet }); + + this.cooldown = 1000 / fireRate } - fireBullet(x: number, y: number) { - let bullet = this.getFirstDead(false); + fireBullet(time: number, shooter: Ship) { + let bullet = this.getFirstDead(false) - if (bullet) { - bullet.fire(x, y); + if (bullet && this.lastShoot + this.cooldown <= time) { + bullet.fire(shooter.x, shooter.y, shooter.rotation) + this.lastShoot = time } } } diff --git a/src/classes/Ship.ts b/src/classes/Ship.ts index b334477..6e7352d 100644 --- a/src/classes/Ship.ts +++ b/src/classes/Ship.ts @@ -8,6 +8,8 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite { readonly colliderRadiusRatio = 0.43 readonly wrapMargin = 10 + readonly fireRate = 5 //shoots/s + bullets?: Bullets constructor(scene: Phaser.Scene) { @@ -29,10 +31,10 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite { this.setDrag(this.dragForce) this.body.setCircle(this.width * this.colliderRadiusRatio) - this.bullets = new Bullets(scene) + this.bullets = new Bullets(scene, this.fireRate) } - preUpdate(time, delta) { + preUpdate(time: number, delta: number) { super.preUpdate(time, delta) const keyUp = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W) @@ -40,8 +42,6 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite { const keyLeft = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A) const keyRight = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D) - const keyShoot = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE) - const vec = new Phaser.Math.Vector2(0, 0) if (keyUp.isDown) vec.y = -this.acceleration @@ -49,10 +49,10 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite { if (keyRight.isDown) vec.x = this.acceleration if (keyLeft.isDown) vec.x = -this.acceleration - if (keyShoot.isDown) this.bullets.fireBullet(this.x, this.y); + if (this.scene.input.activePointer.leftButtonDown()) + this.bullets!.fireBullet(time, this); this.body.velocity.add(vec) this.scene.physics.world.wrap(this, this.wrapMargin) } - } diff --git a/src/main.ts b/src/main.ts index 0269360..4118168 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,7 @@ const config: Phaser.Types.Core.GameConfig = { physics: { default: 'arcade', arcade: { - debug: true + debug: false } }, scene: [DefaultScene]