import * as Phaser from 'phaser';
export class BossBullet extends Phaser.Physics.Arcade.Sprite {
constructor(scene: Phaser.Scene, x: number, y: number) {
super(scene, x, y, 'bossbullet');
scene.add.existing(this);
scene.physics.add.existing(this);
}
// 弾を発射するメソッド(プレイヤーの現在位置と移動速度から未来位置を予測して狙う)
public fire(targetX: number, targetY: number, targetVx: number, targetVy: number, speed: number = 300, angle: number) {
this.setActive(true);
this.setVisible(true);
// this.setAngle(Phaser.Math.RadToDeg(angle));
// 簡易的な偏差射撃(弾が届くまでの時間を予測して着弾点を補正)
const distance = Phaser.Math.Distance.Between(this.x, this.y, targetX, targetY);
const timeToTarget = distance / speed;
// プレイヤーの移動方向(速度)を加味した予測位置
const predictX = targetX + targetVx * (timeToTarget * 0.5); // 補正率0.5で適度に狙う
const predictY = targetY + targetVy * (timeToTarget * 0.5);
// 予測位置に向けて角度を計算
//const angle = Phaser.Math.Angle.Between(this.x, this.y, predictX, predictY);
// 物理体に速度を設定
if (this.body) {
this.scene.physics.velocityFromAngle(Phaser.Math.RadToDeg(angle), speed, this.body.velocity);
}
}
update() {
// 画面外に出たら自動的に非アクティブ化
if (this.y < 0 || this.y > this.scene.scale.height || this.x < 0 || this.x > this.scene.scale.width) {
this.setActive(false);
this.setVisible(false);
}
}
}