Add bullets
This commit is contained in:
parent
f1689ba839
commit
cf67656766
5 changed files with 85 additions and 25 deletions
25
src/classes/Bullet.ts
Normal file
25
src/classes/Bullet.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import Phaser from 'phaser'
|
||||
|
||||
export default class Bullet extends Phaser.Physics.Arcade.Sprite {
|
||||
constructor(scene, x, y) {
|
||||
super(scene, x, y, 'bullet');
|
||||
}
|
||||
|
||||
fire(x, y) {
|
||||
this.body.reset(x, y);
|
||||
|
||||
this.setActive(true);
|
||||
this.setVisible(true);
|
||||
|
||||
this.setVelocityY(-300);
|
||||
}
|
||||
|
||||
preUpdate(time, delta) {
|
||||
super.preUpdate(time, delta);
|
||||
|
||||
if (this.y <= -32) {
|
||||
this.setActive(false);
|
||||
this.setVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/classes/Bullets.ts
Normal file
24
src/classes/Bullets.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import Phaser from 'phaser'
|
||||
import Bullet from './Bullet'
|
||||
|
||||
export default class Bullets extends Phaser.Physics.Arcade.Group {
|
||||
constructor(scene: Phaser.Scene) {
|
||||
super(scene.physics.world, scene);
|
||||
|
||||
this.createMultiple({
|
||||
frameQuantity: 5,
|
||||
key: 'bullet',
|
||||
active: false,
|
||||
visible: false,
|
||||
classType: Bullet
|
||||
});
|
||||
}
|
||||
|
||||
fireBullet(x: number, y: number) {
|
||||
let bullet = this.getFirstDead(false);
|
||||
|
||||
if (bullet) {
|
||||
bullet.fire(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import Phaser from 'phaser'
|
||||
import Bullets from './Bullets'
|
||||
|
||||
export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
||||
readonly acceleration = 5
|
||||
|
|
@ -7,6 +8,8 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
|||
readonly colliderRadiusRatio = 0.43
|
||||
readonly wrapMargin = 10
|
||||
|
||||
bullets?: Bullets
|
||||
|
||||
constructor(scene: Phaser.Scene) {
|
||||
super(
|
||||
scene,
|
||||
|
|
@ -25,6 +28,8 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
|||
this.setDamping(true)
|
||||
this.setDrag(this.dragForce)
|
||||
this.body.setCircle(this.width * this.colliderRadiusRatio)
|
||||
|
||||
this.bullets = new Bullets(scene)
|
||||
}
|
||||
|
||||
preUpdate(time, delta) {
|
||||
|
|
@ -35,6 +40,8 @@ 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
|
||||
|
|
@ -42,7 +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);
|
||||
|
||||
this.body.velocity.add(vec)
|
||||
this.scene.physics.world.wrap(this, this.wrapMargin)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue