-- SA01.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/Backgrounds (SPHIA)/BG_B23E.png", 1)
        loadCharacter("res/characters/Kokoro Fuyukawa/CO05BS.png", 0)
        loadCharacter("res/characters/Kokoro Fuyukawa/CO06BS.png", 1)
        loadCharacter("res/characters/Kokoro Fuyukawa/CO08BS.png", 2)
        loadCharacter("res/characters/Seiji Yomogi/YO01BS.png", 3)
        loadCharacter("res/characters/Seiji Yomogi/YO03BS.png", 4)
        loadCharacter("res/characters/Seiji Yomogi/YO08BS.png", 5)
        loadCharacter("res/characters/Rin Mayuzumi/MA05AS.png", 6)
        loadCharacter("res/characters/Kali Utsumi/KA07BS.png", 7)
        loadMusic("res/music/03 - Chaining -beta-.mp3")
        -- Основной сценарий
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 0)
        playMusic()
        dialogBox({
            "[Satoru] (I should try to get into this cube...)"
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 1)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 0)
        dialogBox({
            "[Kokoro] Satoru! Wait! Where are you going?",
            "[Satoru] Kokoro, I'll try to get into this cube and return as soon as possible! Don't come with me!",
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(0)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 1)
        dialogBox({
            "[Kokoro] Satoru... What's wrong with you?... And I still don't know who you are...",
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawBackground(1)
        stopDrawCharacter(1)
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 0)
        --[[moveCamera(getScreenWidth()/2-200, getScreenHeight()/2-200, 2.0, 1.0)
        while isCameraMoving() do
            coroutine.yield()
        end]]--
        
        dialogBox({
            "[Satoru] Damn, it won't open! Why? I should have tried this before...",
            "[Satoru] (This is strange... But I should go back now.)",
            "[Satoru] Hm? There are some strange sounds inside... What is it?",
            "[Satoru] Doesn't matter."}, {}, -1)
        while isDialogExecuted() do coroutine.yield() end
        
        stopDrawBackground(0)
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.0, 1)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 1)
        dialogBox({
            "[Kokoro] What's going on with you? Are you okay, Satoru?",
            "[Satoru] Never mind. Everything's fine now.",
            "[Kokoro] Hey, Satoru.",
            "[Satoru] What is it?",
            "[Kokoro] I was thinking about you and Mayuzumi-chan. You were dating, right?...",
            "[Satoru] Yes, a few years ago.",
            "[Kokoro] I see. But why doesn't Mayuzumi talk to you now? I thought you were still important to her.",
            "[Satoru] She told me she didn't know who I was. And... it's strange. But it doesn't matter much right now. Right now we should find a way to get back to the city.",
            "[Kokoro] You're right..."
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        local currentTime = getTime()
        while getTime() - currentTime < 1.0 do
            coroutine.yield()
        end
        unloadMusic()
        loadMusic("res/music/09 - Old wise man.mp3")
        playMusic()
        stopDrawCharacter(1)
        drawCharacter(getScreenWidth()/3, getScreenHeight()/2, 2.1, 3)
        drawCharacter(getScreenWidth()-getScreenWidth()/3, getScreenHeight()/2, 2.1, 2)
        dialogBox({
            "[Yomogi] Hey, Kokoro-chan and Satoru-kun. So, you're really who you say you are?",
            "[Yomogi] ...and if so, neither of you has DID.",
            "[Yomogi] Satoru-kun, can you please explain this to me? I think you know something.",
            "[Satoru] Long story short, it's space-time travel with teleportation.",
            "[Yomogi] I see... Okay, then.",
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(2)
        stopDrawCharacter(3)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 4)
        dialogBox({
            "[Yomogi] Let's go, everyone!",
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(4)
        drawCharacter(getScreenWidth()/3, getScreenHeight()/2, 2.1, 3)
        drawCharacter(getScreenWidth()-getScreenWidth()/3, getScreenHeight()/2, 2.1, 6)
        dialogBox({
            "[Mayuzumi] Do you know where we are?",
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end
        
        stopDrawCharacter(3)
        drawCharacter(getScreenWidth()/3, getScreenHeight()/2, 2.1, 5)
        dialogBox({
            "[Yomogi] No...",
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        stopMusic()
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do
            coroutine.yield()
        end
        unloadMusic()
        loadMusic("res/music/14 - Self.mp3")
        playMusic()
        dialogBox({
            "[Satoru] We're near the Hotarubi mines near Moeda village in Hokkaido.",
            "[Utsumi] ...",
            "[Yomogi] ..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        
        stopDrawCharacter(6)
        drawCharacter(getScreenWidth()-getScreenWidth()/3, getScreenHeight()/2, 2.1, 7)
        dialogBox({
            "[Utsumi] Why... are we here?",
            "[Satoru] (W... What? How do I know this?)",
            "[Satoru] I don't know. But this place feels familiar.",
            "[Satoru] Wait. Where's Yuni?"
        }, {}, -1)
        while isDialogExecuted() do
            coroutine.yield()
        end

        -- Отгрузка перед загрузкой следующего скрипта
        stopDrawBackground(0)
        stopDrawBackground(1)
        stopDrawCharacter(0)
        stopDrawCharacter(1)
        stopDrawCharacter(2)
        stopDrawCharacter(3)
        stopDrawCharacter(4)
        stopDrawCharacter(5)
        stopDrawCharacter(6)
        stopDrawCharacter(7)
        stopMusic()
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do
            coroutine.yield()
        end
        unloadBackground(0)
        unloadBackground(1)
        unloadCharacter(0)
        unloadCharacter(1)
        unloadCharacter(2)
        unloadCharacter(3)
        unloadCharacter(4)
        unloadCharacter(5)
        unloadCharacter(6)
        unloadCharacter(7)
        unloadMusic()
        loadScript("scripts/SA02.lua")
    end)
end

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

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