Add bullets
This commit is contained in:
parent
f1689ba839
commit
cf67656766
5 changed files with 85 additions and 25 deletions
BIN
public/assets/img/bullet.png
Normal file
BIN
public/assets/img/bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 610 B |
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 Phaser from 'phaser'
|
||||||
|
import Bullets from './Bullets'
|
||||||
|
|
||||||
export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
||||||
readonly acceleration = 5
|
readonly acceleration = 5
|
||||||
|
|
@ -7,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
|
||||||
|
|
||||||
|
bullets?: Bullets
|
||||||
|
|
||||||
constructor(scene: Phaser.Scene) {
|
constructor(scene: Phaser.Scene) {
|
||||||
super(
|
super(
|
||||||
scene,
|
scene,
|
||||||
|
|
@ -25,6 +28,8 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
|
||||||
this.setDamping(true)
|
this.setDamping(true)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
preUpdate(time, delta) {
|
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 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
|
||||||
|
|
@ -42,7 +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);
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,38 +2,39 @@ import Phaser from 'phaser'
|
||||||
import Ship from '../classes/Ship'
|
import Ship from '../classes/Ship'
|
||||||
|
|
||||||
export default class DefaultScene extends Phaser.Scene {
|
export default class DefaultScene extends Phaser.Scene {
|
||||||
player?: Ship
|
player?: Ship
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super('default-scene')
|
super('default-scene')
|
||||||
}
|
}
|
||||||
|
|
||||||
preload() {
|
preload() {
|
||||||
this.load.image('sky', 'assets/img/nebula10.png')
|
this.load.image('sky', 'assets/img/nebula10.png')
|
||||||
this.load.image('logo', 'assets/img/phaser3-logo.png')
|
this.load.image('logo', 'assets/img/phaser3-logo.png')
|
||||||
this.load.image('ship', 'assets/img/ship.png')
|
this.load.image('ship', 'assets/img/ship.png')
|
||||||
this.load.spritesheet('particles', 'assets/img/boom.png', { frameWidth: 192, frameHeight: 192 })
|
this.load.image('bullet', 'assets/img/bullet.png')
|
||||||
}
|
this.load.spritesheet('particles', 'assets/img/boom.png', { frameWidth: 192, frameHeight: 192 })
|
||||||
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
this.add.image(400, 300, 'sky')
|
this.add.image(400, 300, 'sky')
|
||||||
|
|
||||||
const particles = this.add.particles('particles', 21)
|
const particles = this.add.particles('particles', 21)
|
||||||
|
|
||||||
const emitter = particles.createEmitter({
|
const emitter = particles.createEmitter({
|
||||||
speed: 300,
|
speed: 300,
|
||||||
scale: { start: 1, end: 0 },
|
scale: { start: 1, end: 0 },
|
||||||
blendMode: 'ADD',
|
blendMode: 'ADD',
|
||||||
})
|
})
|
||||||
|
|
||||||
const logo = this.physics.add.image(400, 100, 'logo')
|
const logo = this.physics.add.image(400, 100, 'logo')
|
||||||
|
|
||||||
logo.setVelocity(100, 200)
|
logo.setVelocity(100, 200)
|
||||||
logo.setBounce(1, 1)
|
logo.setBounce(1, 1)
|
||||||
logo.setCollideWorldBounds(true)
|
logo.setCollideWorldBounds(true)
|
||||||
|
|
||||||
emitter.startFollow(logo)
|
emitter.startFollow(logo)
|
||||||
|
|
||||||
this.player = new Ship(this)
|
this.player = new Ship(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue