private canbullets!: Phaser.Physics.Arcade.Group;
private rectGroup!: Phaser.GameObjects.Group;
private rect2!: Phaser.GameObjects.Rectangle;
private myContainer!: Phaser.GameObjects.Container;
private bulletSpeed: number = 800;
private lastFired: number = 0;
private fireRate: number = 2000; // 発射間隔(ミリ秒単位。1000ms = 1秒)
create() {
const centerX = 400;
const centerY = 100;
// コンテナを基点座標に作成
this.myContainer = this.add.container(centerX, centerY).setDepth(3);
// 2. 1つ目のRectangleを作成 (赤色)
// コンテナの(0, 0)が回転中心になるよう、originを(0.5, 0.5)にする
const rect1 = this.add.rectangle(0, 0, 100, 100, 0xff0000).setOrigin(0.5, 0.5);
// 3. 2つ目のRectangleを作成 (青色)
// rect2.setAngle(45);
// コンテナに2つのRectangleを追加
this.myContainer.add(rect1);
//const rect2 = this.add.rectangle(0, 50, 40, 40, 0x0000ff);
const distance = 90;
const angleRad = Phaser.Math.DegToRad(45); // 45度をラジアンに変換
const x2 = Math.cos(angleRad) * distance;
const y2 = Math.sin(angleRad) * distance - 67;
this.rect2 = this.add.rectangle(x2, y2, 40, 30, 0x0000ff); // 緑色
this.rect2.setOrigin(0.5, 0.5);
// 2つ目のRectangle自体も45度回転させておくことで、円の接線方向に揃える
this.rect2.angle = 90;
this.myContainer.add(this.rect2);
// 4. 弾のグループを作成(物理エンジン対応)
this.canbullets = this.physics.add.group();
}
private fireBullet(angle: number) {
// 大砲と同じ位置に弾(黄色の小さな四角形)を作成
const bulletRect = this.add.rectangle(this.myContainer.x, this.myContainer.y, 10, 10, 0xffff00);
// グループに追加して物理ボディを有効化
this.canbullets.add(bulletRect);
const bulletBody = bulletRect.body as Phaser.Physics.Arcade.Body;
// 角度から X方向 と Y方向 の速度(ベクトル)を計算して弾にセット
const velocityX = Math.cos(angle) * this.bulletSpeed;
const velocityY = Math.sin(angle) * this.bulletSpeed;
bulletBody.setVelocity(velocityX, velocityY);
}
update(time: number, delta: number) {
// --- 大砲の回転制御 ---
// 大砲からプレイヤーへの角度を計算(ラジアン)
const angle = Phaser.Math.Angle.Between(this.myContainer.x, this.myContainer.y, this.player.x, this.player.y);
// 大砲の見た目の向きをプレイヤーに向ける
this.myContainer.rotation = angle;
// --- 大砲の発射タイマー制御 ---
if (time > this.lastFired) {
this.fireBullet(angle);
// 修正:現在時刻に連射間隔(ミリ秒など)を足す
this.lastFired = time + this.fireRate;
}
}