const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(
    window.innerWidth / -2, window.innerWidth / 2,
    window.innerHeight / 2, window.innerHeight / -2,
    1, 1000
);
camera.position.z = 10;

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// プレイヤー召喚
const playerGeometry = new THREE.BoxGeometry(30, 30, 1);
const playerMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const player = new THREE.Mesh(playerGeometry, playerMaterial);
player.position.x = -200;
scene.add(player);

// 弾と敵のグループ
const bullets = [];
const enemies = [];

// 入力関連
const keys = { up: false, down: false,  left: false, right: false, space: false};

document.addEventListener('keydown', (event) => {
    if (event.key === 'w') {
        keys.up = true;
        console.log("w pressed - Move Up");
    }
    if (event.key === 's') {
        keys.down = true;
        console.log("s pressed - Move Down");
    }
    if (event.key === 'a') {
        keys.left = true;
        console.log("s pressed - Move Left");
    }
    if (event.key === 'd') {
        keys.right = true;
        console.log("s pressed - Move Right");
    }
    if (event.key === ' ') {  // スペースキー
        keys.space = true;
        console.log("Space pressed");
    }
});

document.addEventListener('keyup', (event) => {
    if (event.key === 'w') {
        keys.up = false;
    }
    if (event.key === 's') {
        keys.down = false;
    }
    if (event.key === 'a') {
        keys.left = false;
    }
    if (event.key === 'd') {
        keys.right = false;
    }
    if (event.key === ' ') {
        keys.space = false;
    }
});

// ループ処理
let lastShot = 0;
function animate() {
    requestAnimationFrame(animate);
    const time = performance.now();

    // プレイヤーの移動
    if (keys.up) { player.position.y += 6; }
    if (keys.down) { player.position.y -= 6; }
    if (keys.left) { player.position.x -= 6; }
    if (keys.right) { player.position.x += 6; }

    // 弾の発射
    if (keys.space && time - lastShot > 250) {
        const bulletGeometry = new THREE.BoxGeometry(5, 5, 1);
        const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
        const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
        bullet.position.set(player.position.x + 20, player.position.y, 0);
        bullets.push(bullet);
        scene.add(bullet);
        lastShot = time;
    }

    // 弾の移動
    bullets.forEach((bullet, index) => {
        bullet.position.x += 15;
        if (bullet.position.x > window.innerWidth / 2) {
            scene.remove(bullet);
            bullets.splice(index, 1);
        }
    });

    // 敵の生成
    if (Math.random() < 0.02) {
        const enemyGeometry = new THREE.BoxGeometry(30, 30, 1);
        const enemyMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
        const enemy = new THREE.Mesh(enemyGeometry, enemyMaterial);
        enemy.position.set(window.innerWidth / 2, (Math.random() - 0.5) * window.innerHeight, 0);
        enemies.push(enemy);
        scene.add(enemy);
    }

    // 敵の移動と弾との衝突判定
    enemies.forEach((enemy, enemyIndex) => {
        enemy.position.x -= 3;

        bullets.forEach((bullet, bulletIndex) => {
            if (bullet.position.distanceTo(enemy.position) < 20) {
                scene.remove(enemy);
                enemies.splice(enemyIndex, 1);
                scene.remove(bullet);
                bullets.splice(bulletIndex, 1);
            }
        });

        // 画面外の敵削除処理
        if (enemy.position.x < -window.innerWidth / 2) {
            scene.remove(enemy);
            enemies.splice(enemyIndex, 1);
        }
    });

    renderer.render(scene, camera);
}

animate();
