Add cooldown and bullet angle

This commit is contained in:
KGrzeg 2021-10-14 18:41:45 +02:00
parent cf67656766
commit 0b2f410824
4 changed files with 44 additions and 25 deletions

View file

@ -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)
}
}
}

View file

@ -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
}
}
}

View file

@ -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)
}
}

View file

@ -11,7 +11,7 @@ const config: Phaser.Types.Core.GameConfig = {
physics: {
default: 'arcade',
arcade: {
debug: true
debug: false
}
},
scene: [DefaultScene]