document.getElementById('start-button').addEventListener('click', startGame);

let currentBGM = '';
let bgmPosition = 0;
let bgmElement = document.getElementById('bgm');

// 変数管理オブジェクト
let gameVariables = {
    scene1VisitedCount: 0, //  変数名: 0,　と書く
};
// SBスイッチの状態管理オブジェクト
let switches = {
    'a': false, // 任意のスイッチ名で初期値は false
    'い': false
};

function startGame() {
    document.getElementById('title-screen').style.display = 'none';
    document.getElementById('game-screen').style.display = 'block';
    showScene(14);
}

const scenes = [
    {
        id: 0, 
        background: 'scene1.jpg',
        defaultLeftCharacter: 'character1.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character2.png',
        dialogue: '',
        bgm: './data/bgm/',
        sfx: './data/se/',
        choices: [
            { text: 'next', nextScene: 1 },
            // { text: 'インデックス番号0', nextScene: 0 }
        ],
        switches: {},
    },
    {
        id: 1,
        background: './data/img/background/校舎-EDIT.jpg',
        defaultLeftCharacter: 'character1.png',
        defaultCenterCharacter: './data/img/character/lira.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'これは、私がとある高校で働いていたときの話である。',
        bgm: './data/bgm/ベルとクワイヤ(コーラス)が中心の不気味で重たいホラー曲【ホラー 不気味 BGM】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            // { text: 'インデックス番号１', nextScene: 1 },
            { text: 'next', nextScene: 2 }
        ],
        switches: {}
    },
    {
        id: 2,
        background: './data/img/background/校舎-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'この学校は怪奇現象がよく起きていた。',
        bgm: './data/bgm/ベルとクワイヤ(コーラス)が中心の不気味で重たいホラー曲【ホラー 不気味 BGM】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 3 }
        ],
        switches: {}
    },
    {
        id: 3,
        background: 'scene3.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '',
        bgm: './data/bgm/',
        sfx: 'sfx3.mp3',
        choices: [
            { text: 'next', nextScene: 4 }
        ],
        switches: {}
    },
    {
        id: 4,
        background: 'scene3.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'それは、私が残業していた時だった',
        bgm: './data/bgm/',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 5 }
        ],
    },
    {
        id: 5,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '（あ〜、やっと採点終わった）',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Book2.ogg',
        choices: [
            { text: 'next', nextScene: 6 }
        ],
    },
    {
        id: 6,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'もう22時か、\nそろそろ帰るか…',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 7 }
        ],
    },
    {
        id: 7,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'あ、しまった。教室に忘れ物したな。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 8 }
        ],
    },
    {
        id: 8,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '当然だが真っ暗だ',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 9 }
        ],
    },
    {
        id: 9,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '.....めんどくさいけど、、、取りに行くか。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 10 }
        ],
    },
    {
        id: 10,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'えーっと、、、クラスがF22…\nあれ？',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 11 }
        ],
    },
    {
        id: 11,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '鍵が無い！？',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 12 }
        ],
    },
    {
        id: 12,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '職員室の中を探すも、鍵は見つからなかった。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 13 }
        ],
    },
    {
        id: 13,
        background: './data/img/background/職員室-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '、、、、探すか…',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 14 }
        ],
    },
    {
        id: 14,
        background: './data/img/background/本館マップ-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 15 }
        ],
    },
    {
        id: 15,
        background: './data/img/background/本館マップ-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '取りに行く鍵はF22、\nおそらく他のクラスには無いから、探すとしたら多目的室かソフトウェア室か。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート2.mp3',
        choices: [
            { text: 'next', nextScene: 16 }
        ],
    },
    {
        id: 16,
        background: './data/img/background/本館マップ-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'どっちに行こうか？',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'ソフトウェア室', nextScene: 17 },
            { text: '多目的室', nextScene: 0 }
        ],
    },
    {
        id: 17,
        background: './data/img/background/部室前1-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '本館3F：ソフトウェア室',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: '入室', nextScene: 18 },
            { text: '戻る', nextScene: 15 },
        ],
    },
    {
        id: 18,
        background: './data/img/background/部室1-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Open6.ogg',
        choices: [
            { text: 'next', nextScene: 19 },
        ],
    },
    {
        id: 19,
        background: './data/img/background/部室1-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '引き出しの中にありそうだな…\n多分生徒用の引き出しには無いから、探るなら棚か教員用くらいか',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート2.mp3',
        choices: [
            { text: '教員用の引き出しを探る', nextScene: 20 },
            { text: '棚を探る', nextScene: 21 },
        ],
    },
    {
        id: 20,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Open6.ogg',
        choices: [
            { text: 'next', nextScene: 22 },
        ],
    },
    {
        id: 21,
        background: './data/img/background/部室1-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '棚はロックが掛かっている',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Open2.ogg',
        choices: [
            { text: '戻る', nextScene: 19 },
        ],
    },
    {
        id: 22,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: 'うわ、散らかってる.......',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート0.mp3',
        choices: [
            { text: 'next', nextScene: 23 },
        ],
    },
    {
        id: 23,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '中には南館渡り廊下と書かれた鍵がある。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート1.mp3',
        choices: [
            { text: 'next', nextScene: 24 },
        ],
    },
    {
        id: 24,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '鍵を取りますか？',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: '取る', nextScene: 25 },
            { text: '今は取らない', nextScene: 26 },
        ],
    },
    {
        id: 25,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '鍵を入手した。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/Switch1.ogg',
        choices: [
            { text: 'next', nextScene: 19 },
        ],
    },
    {
        id: 26,
        background: './data/img/background/PC教員引き出し-EDIT.jpg',
        defaultLeftCharacter: 'character2.png',
        defaultCenterCharacter: 'character-center.png',
        defaultRightCharacter: 'character3.png',
        dialogue: '多分不要だろう。',
        bgm: './data/bgm/生暖かい空気 -Tepid air- 【ホラー⧸不気味】.mp3',
        sfx: './data/se/会話パート1.mp3',
        choices: [
            { text: 'next', nextScene: 19 },
        ],
    }
];

function setSwitch(switchName, state) {
    if (switches.hasOwnProperty(switchName)) {
        switches[switchName] = state;
    } else {
        console.error("エラー：SBスイッチが存在しません。　✕△✕", switchName);
        alert("エラー：SBスイッチが存在しません。　✕△✕", index);
    }
}
function showScene(index) {
    const scene = scenes.find(s => s.id === index);
    if (!scene) {
        console.error("エラー：インデックス番号のシーンが存在しません。　✕△✕", index);
        alert("エラー：インデックス番号のシーンが存在しません。　✕△✕", index);
        return;
    }

    // シーン訪問回数をカウント
    // scene.id === の後ろの値はカウントして、一定の回数で条件分岐を発動させ、そのシーンを書き換える。
    if (scene.id === 25) {
        // gameVariables.　の後に、変数管理オブジェクトに定義した変数名を書き入れ、 ++;　を書き込む
        gameVariables.scene1VisitedCount++;
    }

    // 分岐時に使える置き換えの変数
    let leftCharacter = scene.defaultLeftCharacter;
    let centerCharacter = scene.defaultCenterCharacter;
    let rightCharacter = scene.defaultRightCharacter;
    let dialogue = scene.dialogue;
    let choices = scene.choices;
    let background = scene.background;
    let bgm = scene.bgm;
    let sfx = scene.sfx;

    // 条件分岐でスイッチの状態を変更
    // gameVariables.変数管理オブジェクトに定義した変数名　演算子　値
    if (gameVariables.scene1VisitedCount >= 1) {
        setSwitch('a', true); // スイッチをtrueにする
        dialogue = '鍵を入手した。q';
        // gameVariables.変数管理オブジェクトに定義した変数名
        gameVariables.scene1VisitedCount = 0; // 訪問回数リセットするコード
    }

    // スイッチの状態を更新
    for (const [key, value] of Object.entries(scene.switches)) {
        setSwitch(key, value);
    }

    // 条件分岐で別の場面に書き換え
    if (scene.alternateScenes) {
        for (const [key, altScene] of Object.entries(scene.alternateScenes)) {
            if (switches[key]) {
                Object.assign(scene, altScene);
            }
        }
    }

    document.getElementById('background').style.backgroundImage = `url(${background})`;
    document.getElementById('left-character').style.backgroundImage = `url(${leftCharacter})`;
    document.getElementById('center-character').style.backgroundImage = `url(${centerCharacter})`;
    document.getElementById('right-character').style.backgroundImage = `url(${rightCharacter})`;
    changeBGM(bgm);
    playSFX(sfx);
    typeDialogue(dialogue, () => {
        if (choices.length > 0) {
            showChoices(choices);
        }
    });
}
//... (その他の関数は変更なし)
// 以下ver1.3同様関数。
// bgm再生
function changeBGM(bgm) {
    if (bgm === currentBGM) {
        bgmElement.currentTime = bgmPosition;
        bgmElement.play();
    } else {
        fadeOut(bgmElement, () => {
            bgmElement.src = bgm;
            bgmElement.currentTime = 0;
            bgmElement.play();
            fadeIn(bgmElement);
            currentBGM = bgm;
            bgmPosition = 0;
        });
    }
}
// フェードイン
function fadeIn(audioElement) {
    let volume = 0;
    audioElement.volume = volume;
    const fadeInInterval = setInterval(() => {
        if (volume < 1) {
            volume += 0.1;
            audioElement.volume = volume;
        } else {
            clearInterval(fadeInInterval);
        }
    }, 100);
}
// フェードアウト
function fadeOut(audioElement, callback) {
    let volume = audioElement.volume;
    const fadeOutInterval = setInterval(() => {
        if (volume > 0) {
            volume -= 0.1;
            audioElement.volume = volume;
        } else {
            clearInterval(fadeOutInterval);
            callback();
        }
    }, 100);
}
function playSFX(sfx) {
    const sfxElement = new Audio(sfx);
    sfxElement.play();
}
bgmElement.addEventListener('timeupdate', () => {
    bgmPosition = bgmElement.currentTime;
});
function typeDialogue(text, callback) {
    const dialogueText = document.getElementById('dialogue-text');
    dialogueText.textContent = '';
    let i = 0;
    const typingSpeed = 0;
    const interval = setInterval(() => {
        if (i < text.length) {
            dialogueText.textContent += text.charAt(i);
            i++;
        } else {
            clearInterval(interval);
            setTimeout(callback, 0);
        }
    }, typingSpeed);
}
function showChoices(choices) {
    const choicesContainer = document.getElementById('choices');
    choicesContainer.style.display = 'block';
    choicesContainer.innerHTML = '';
    choices.forEach(choice => {
        const button = document.createElement('button');
        button.className = 'choice-button';
        button.textContent = choice.text;
        button.addEventListener('click', () => {
            choicesContainer.style.display = 'none';
            showScene(choice.nextScene);
        });
        choicesContainer.appendChild(button);
    });
}
