-- SA02.lua
local colors = {
    blue = {r = 0, g = 0, b = 255},
    red = {r = 255, g = 0, b = 0},
    green = {r = 0, g = 255, b = 0}
}

function EventLoopCoroutine()
    dialogCoroutine = coroutine.create(function()
        loadBackground("res/backgrounds/Backgrounds (Other)/BG_C04A.png", 0)
        loadBackground("res/backgrounds/white.png", 1)
        loadCharacter("res/characters/Yuni Kusuda/UN01BS.png", 0)
        loadCharacter("res/characters/Yuni Kusuda/UN08BS.png", 1)
        loadCharacter("res/characters/Yuni Kusuda/UN11BS.png", 2)
        loadCharacter("res/characters/Naoya Enamoto/EN01AS.png", 3)
        loadCharacter("res/characters/Kokoro Fuyukawa/CO08BS.png", 4)
        loadMusic("res/music/13 - Trickstar.mp3")
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 0)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 0)
        playMusic()
        dialogBox({
            "[Yuni] Hey, everyone! Come here! Quick!",
            "[Satoru] (He decided to go into this cube... Huh.)",
            "[Yuni] Satoru, you have a key with you, right?",
            "[Satoru] Yes... here it is.",
            "[Yuni] Try to open this door.",
            "[Satoru] I've already tried. It doesn't work.",
            "[Satoru] Um, okay...",
            "[Satoru] (I have a strange feeling that I shouldn't open this door.)"
        }, {"Pretend the door won't open", "Open the door"}, 7)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        
        answerValue = getAnswerValue()
        if answerValue == 0 then
            dialogBox({
                "[Yuni] Give it to me, please.",
                "[Satoru] Okay.",
                "[Satoru] (No...)"
            }, {}, -1)
            while isDialogExecuted() do 
                coroutine.yield()
            end
            
            stopDrawCharacter(0)
            drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 2)
            dialogBox({
                "[Yuni] You're so weak, Satoru!"
            }, {}, -1)
            while isDialogExecuted() do 
                coroutine.yield()
            end
        end
        stopDrawCharacter(0)
        stopDrawCharacter(2)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 1)
        dialogBox({
            "[Yuni] It's just white inside... Strange... What is it?",
            "[Satoru] Yuni! Don't go inside, please! I have a bad feeling about this."
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(1)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 2)
        dialogBox({
            "[Yuni] But you know what's inside, right?",
            "[Satoru] ...",
            "[Satoru] I... I don't know.",
            "[Satoru] (What the hell am I talking about?)"
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(0)
        stopDrawCharacter(2)
        stopDrawBackground(0)
        local currentTime = getTime()
        while getTime() - currentTime < 1.0 do
            coroutine.yield()
        end
        stopMusic()
        local currentTime = getTime()
        while getTime() - currentTime < 0.2 do
            coroutine.yield()
        end
        unloadMusic()
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 1)
        dialogBox({
            "[Yuni] ...",
            "[Satoru] ...",
            "[Kokoro] ...",
            "[Everyone] ...",
            "[Everyone] Where are we?..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        
        local currentTime = getTime()
        while getTime() - currentTime < 1.0 do 
            coroutine.yield()
        end
        loadMusic("res/music/15 - Communication.mp3")
        playMusic()
        dialogBox({
            "[Enomoto] ...YUKIDOH's plan is going very, very well, ha-ha-ha."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do 
            coroutine.yield()
        end
        
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 3)
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do 
            coroutine.yield()
        end
        
        dialogBox({
            "[Satoru] Enomoto?! Why are you alive?!"
        }, {}, -1)
        while isDialogExecuted() do coroutine.yield() end
        
        stopDrawCharacter(3)
        drawCharacter(getScreenWidth()/3, getScreenHeight()/2, 2.1, 3)
        drawCharacter(getScreenWidth()-getScreenWidth()/3, getScreenHeight()/2, 2.1, 4)
        dialogBox({
            "[Kokoro] But... You were killed by Satoru, right?... How can this be?...",
            "[Enomoto] Right now it doesn't matter. All you need to know is that everything is going according to plan.",
            "[Satoru] Wait! What do you mean by \"according to plan\"?",
            "[Enomoto] You'll learn everything, but not now.",
            "[Enomoto] Self is beginning to show himself... *giggle*"
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield() 
        end

        stopDrawBackground(0)
        stopDrawBackground(1)
        stopDrawCharacter(0)
        stopDrawCharacter(1)
        stopDrawCharacter(2)
        stopDrawCharacter(3)
        stopDrawCharacter(4)
        stopMusic()
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do
            coroutine.yield()
        end
        unloadMusic()
        unloadBackground(0)
        unloadBackground(1)
        unloadCharacter(0)
        unloadCharacter(1)
        unloadCharacter(2)
        unloadCharacter(3)
        unloadCharacter(4)
        loadScript("scripts/SE00.lua")
    end)
end

function EventLoop()
    if dialogCoroutine and coroutine.status(dialogCoroutine) ~= "dead" then
        coroutine.resume(dialogCoroutine)
    end
end

setFont("res/InterTight-Regular.ttf")
EventLoopCoroutine()