Add cooldown and bullet angle
This commit is contained in:
parent
cf67656766
commit
0b2f410824
4 changed files with 44 additions and 25 deletions
|
|
@ -1,25 +1,36 @@
|
||||||
import Phaser from 'phaser'
|
import Phaser from 'phaser'
|
||||||
|
|
||||||
export default class Bullet extends Phaser.Physics.Arcade.Sprite {
|
export default class Bullet extends Phaser.Physics.Arcade.Sprite {
|
||||||
|
readonly lifetime = 4000 //ms
|
||||||
|
readonly speed = 600
|
||||||
|
|
||||||
|
age = 0
|
||||||
|
|
||||||
constructor(scene, x, y) {
|
constructor(scene, x, y) {
|
||||||
super(scene, x, y, 'bullet');
|
super(scene, x, y, 'bullet')
|
||||||
}
|
}
|
||||||
|
|
||||||
fire(x, y) {
|
fire(x: number, y: number, rotation: number) {
|
||||||
this.body.reset(x, y);
|
this.body.reset(x, y)
|
||||||
|
this.rotation = rotation
|
||||||
|
|
||||||
this.setActive(true);
|
this.setActive(true)
|
||||||
this.setVisible(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) {
|
preUpdate(time: number, delta: number) {
|
||||||
super.preUpdate(time, delta);
|
super.preUpdate(time, delta)
|
||||||
|
|
||||||
if (this.y <= -32) {
|
this.age += delta
|
||||||
this.setActive(false);
|
|
||||||
this.setVisible(false);
|
if (this.age >= this.lifetime) {
|
||||||
|
this.setActive(false)
|
||||||
|
this.setVisible(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,32 @@
|
||||||
import Phaser from 'phaser'
|
import Phaser from 'phaser'
|
||||||
import Bullet from './Bullet'
|
import Bullet from './Bullet'
|
||||||
|
import Ship from './Ship'
|
||||||
|
|
||||||
export default class Bullets extends Phaser.Physics.Arcade.Group {
|
export default class Bullets extends Phaser.Physics.Arcade.Group {
|
||||||
constructor(scene: Phaser.Scene) {
|
readonly cooldown: number //ms
|
||||||
super(scene.physics.world, scene);
|
|
||||||
|
lastShoot = 0
|
||||||
|
|
||||||
|
constructor(scene: Phaser.Scene, fireRate: number) {
|
||||||
|
super(scene.physics.world, scene)
|
||||||
|
|
||||||
this.createMultiple({
|
this.createMultiple({
|
||||||
frameQuantity: 5,
|
frameQuantity: 30,
|
||||||
key: 'bullet',
|
key: 'bullet',
|
||||||
active: false,
|
active: false,
|
||||||
visible: false,
|
visible: false,
|
||||||
classType: Bullet
|
classType: Bullet
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.cooldown = 1000 / fireRate
|
||||||
}
|
}
|
||||||
|
|
||||||
fireBullet(x: number, y: number) {
|
fireBullet(time: number, shooter: Ship) {
|
||||||
let bullet = this.getFirstDead(false);
|
let bullet = this.getFirstDead(false)
|
||||||
|
|
||||||
if (bullet) {
|
if (bullet && this.lastShoot + this.cooldown <= time) {
|
||||||
bullet.fire(x, y);
|
bullet.fire(shooter.x, shooter.y, shooter.rotation)
|
||||||
|
this.lastShoot = time
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
||||||
readonly colliderRadiusRatio = 0.43
|
readonly colliderRadiusRatio = 0.43
|
||||||
readonly wrapMargin = 10
|
readonly wrapMargin = 10
|
||||||
|
|
||||||
|
readonly fireRate = 5 //shoots/s
|
||||||
|
|
||||||
bullets?: Bullets
|
bullets?: Bullets
|
||||||
|
|
||||||
constructor(scene: Phaser.Scene) {
|
constructor(scene: Phaser.Scene) {
|
||||||
|
|
@ -29,10 +31,10 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
||||||
this.setDrag(this.dragForce)
|
this.setDrag(this.dragForce)
|
||||||
this.body.setCircle(this.width * this.colliderRadiusRatio)
|
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)
|
super.preUpdate(time, delta)
|
||||||
|
|
||||||
const keyUp = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W)
|
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 keyLeft = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A)
|
||||||
const keyRight = this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D)
|
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)
|
const vec = new Phaser.Math.Vector2(0, 0)
|
||||||
|
|
||||||
if (keyUp.isDown) vec.y = -this.acceleration
|
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 (keyRight.isDown) vec.x = this.acceleration
|
||||||
if (keyLeft.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.body.velocity.add(vec)
|
||||||
this.scene.physics.world.wrap(this, this.wrapMargin)
|
this.scene.physics.world.wrap(this, this.wrapMargin)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ const config: Phaser.Types.Core.GameConfig = {
|
||||||
physics: {
|
physics: {
|
||||||
default: 'arcade',
|
default: 'arcade',
|
||||||
arcade: {
|
arcade: {
|
||||||
debug: true
|
debug: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
scene: [DefaultScene]
|
scene: [DefaultScene]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue