import * as Phaser from 'phaser';
export class GameScene extends Phaser.Scene {
private joystick: any;
private rexVirtualJoystick: any;
private cursorKeys!: Phaser.Types.Input.Keyboard.CursorKeys;
private player!: Phaser.Physics.Arcade.Sprite;
private yuka?: Phaser.GameObjects.Rectangle;
private timeText!: Phaser.GameObjects.Text;
private joystickArea!: Phaser.Geom.Rectangle;
private enemy2!: Phaser.Physics.Arcade.Sprite & { body: Phaser.Physics.Arcade.Body };
private bullet2!: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;
private bullets2: any;
private isHit: boolean = false;
constructor() {
super({ key: 'GameScene' });
}
preload() {
this.load.image('frame1', 'assets/b01.png');
this.load.image('frame2', 'assets/b02.png');
this.load.image('frame3', 'assets/b03.png');
this.load.image('frame4', 'assets/b04.png');
this.load.spritesheet('enemy2', 'assets/enemy2.png',
{frameWidth: 32, frameHeight: 32});
this.load.image("bullet2", "assets/laser.png");
this.load.image('frame5', 'assets/b05.png');
this.load.image('frame6', 'assets/b06.png');
this.load.image('frame7', 'assets/b07.png');
this.load.image('frame8', 'assets/b08.png');
}
create() {
const textStyle = {
fontFamily: 'Arial',
fontSize: '25px',
fill: '#333333'
};
this.timeText = this.add.text(20, 20, '', textStyle);
this.timeText.setScrollFactor(0);
this.time.addEvent({
delay: 1000,
loop: true,
callback: () => {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const date = now.getDate();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
this.timeText.setText(`${year}/${month}/${date} ${hours}:${minutes}:${seconds}`);
}
});
this.anims.create({
key: "left2", frameRate: 2, repeat: -1,
frames: this.anims.generateFrameNumbers('enemy2', {start: 4, end: 5}),
});
this.anims.create({
key: "right2", frameRate: 2, repeat: -1,
frames: this.anims.generateFrameNumbers('enemy2', {start: 6, end: 7}),
});
this.enemy2 = this.physics.add.sprite(400, 300, 'enemy2').setScale(1.9);
this.enemy2.setCollideWorldBounds(true).setDepth(4);
this.anims.create({
key: 'walk',
frames: [
{ key: 'frame1' },
{ key: 'frame2' },
{ key: 'frame3' },
{ key: 'frame4' }
],
frameRate: 10, // 1秒間に切り替わるフレーム数
repeat: -1 // -1は無限ループ
});
this.anims.create({
key: 'die',
frames: [
{ key: 'frame5' },
{ key: 'frame6' },
{ key: 'frame7' },
{ key: 'frame8' }
],
frameRate: 4, // 1秒間に切り替わるフレーム数
repeat: 0,
});
this.anims.create({
key: "stop",
frames: [{key: 'frame1', frame: 1}],
frameRate: 10
});
this.bullet2 = this.physics.add.sprite(-20, -20, "bullet2");
this.bullets2 = this.physics.add.group({
defaultKey: 'bullet2',
maxSize: 70,
runChildUpdate: true
});
this.time.addEvent({
delay: 2200,
callback: this.fireBullet,
callbackScope: this,
loop: true
});
this.player = this.physics.add.sprite( 150, 250, 'frame1');
this.player.setScale(0.6);
this.player.setFlipX(true);
// --- A: 単色で塗りつぶす場合 ---
this.player.setTint(0x000080).setTintMode(Phaser.TintModes.FILL);
this.player.setCollideWorldBounds(true);
this.yuka = this.add.rectangle(0, 350, 950, 200, 0x00FFFF).setOrigin(0);
this.physics.add.existing(this.yuka, true);
this.physics.add.existing(this.player);
this.physics.add.collider(this.player, this.yuka);
this.physics.add.collider(this.enemy2, this.yuka);
this.joystickArea = new Phaser.Geom.Rectangle(0, 0, 770, 550);
// デバッグ用にエリアを表示(動作確認後削除してください)
const graphics = this.add.graphics();
graphics.lineStyle(2, 0x00ff00);
graphics.strokeRectShape(this.joystickArea);
// 1. ジョイスティックの作成
this.joystick = (this.plugins.get('rexVirtualJoystick') as any).add(this, {
x: 100,
y: 400,
radius: 100,
base: this.add.circle(0, 0, 100, 0x0000FF).setAlpha(0.1),
thumb: this.add.circle(0, 0, 50, 0xFFA500).setAlpha(0.5),
dir: '8dir', // 8方向入力
forceMin: 16,
fixed: false, // ここをfalseに設定
enable: true
}).setVisible(false); // 最初は非表示にしておく
this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
if (this.joystickArea.contains(pointer.x, pointer.y)) {
// 4. ジョイスティックをその位置に移動して表示
this.joystick.setPosition(pointer.x, pointer.y);
this.joystick.setVisible(true);
this.joystick.base.depth = 100; // 必要に応じて重なり順を調整
this.joystick.thumb.depth = 101;
}
});
/*
this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
this.joystick.x = pointer.x;
this.joystick.y = pointer.y;
this.joystick.setVisible(true); // タッチした場所で表示
this.joystick.base.depth = 100; // 必要に応じて重なり順を調整
this.joystick.thumb.depth = 101;
});
*/
// 指を離した時に非表示にする場合
this.input.on('pointerup', () => {
this.joystick.setVisible(false);
});
this.physics.add.overlap(this.player, this.bullets2, this.handleHit, undefined, this);
}
private handleHit(playerObj: any, bulletObj: any) {
if (this.isHit) return;
const player = playerObj as Phaser.Physics.Arcade.Sprite;
const bullet = bulletObj as Phaser.Physics.Arcade.Image;
bullet.destroy();
this.isHit = true;
player.setVelocity(0, 0);
player.play('die');
this.time.delayedCall(1000, () => {
this.isHit = false;
player.play('stop');
}, [], this);
}
fireBullet() {
let bullet2 = this.bullets2.get(this.enemy2.x, this.enemy2.y+10);
if (bullet2) {
bullet2.body.allowGravity = false;
bullet2.enableBody = true;
// bullet2.visible = false;
bullet2.setScale(0.5);
if (this.enemy2.flipX) {
bullet2.body.velocity.x = 300;
} else {
bullet2.body.velocity.x = -300;
}
}
}
update() {
if (this.isHit) {
this.player.setVelocity(0, 0);
return;
}
this.enemy2.anims.play('left2', true);
if (this.enemy2.x >= 750) {
this.enemy2.anims.play('left2', true);
this.enemy2.flipX = false;
this.enemy2.setVelocityX(-100);
} else if (this.enemy2.x <= 500) {
this.enemy2.flipX = true;
this.enemy2.setVelocityX(100);
}
const cursorKeys = this.joystick.createCursorKeys();
if (cursorKeys.left.isDown) {
this.player.setVelocityX(-200);
this.player.flipX = false;
this.player.anims.play('walk', true);
} else if (cursorKeys.right.isDown) {
this.player.setVelocityX(200);
this.player.flipX = true;
this.player.anims.play('walk', true);
} else {
this.player.setVelocityX(0);
this.player.anims.play("stop", true);
}
if (cursorKeys.up.isDown) {
this.player.setVelocityY(-200);
if (this.player.y < 100) {
this.player.setVelocityY(0);
}
}
}
}