import * as Phaser from 'phaser';
export class Bullet extends Phaser.Physics.Arcade.Sprite {
constructor(scene: Phaser.Scene, x: number, y: number) {
super(scene, x, y, 'bullet');
scene.add.existing(this);
scene.physics.add.existing(this).setDepth(3);
scene.time.delayedCall(2000, this.destroyBullet, [], this);
}
private destroyBullet(): void {
this.destroy();
this.setVisible(false);//bulletをシーンから削除し、メモリを解放する
}
// 弾を発射するメソッド
fire(x: number, y: number, direction: Phaser.Math.Vector2) {
this.enableBody(true, x, y, true, true);
const speed = 900; // 弾の速度
this.setVelocity(direction.x * speed, direction.y * speed);
}
update() {
if (this.y < 0 || this.y > this.scene.scale.height ||
this.x < 0 || this.x > this.scene.scale.width) {
this.disableBody(true, true);
}
}
}