-- SE00.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_C03N2.png", 0)
        loadCharacter("res/characters/Sayaka Yukidoh/SY02CS.png", 0)
        loadCharacter("res/characters/Sayaka Yukidoh/SY06CL.png", 1)
        loadCharacter("res/characters/Sayaka Yukidoh/SY07CL.png", 2)
        loadMusic("res/music/07 - Delusive consciousness amb..mp3")
        -- Основной сценарий
        local currentTime = getTime()
        while getTime() - currentTime < 1.5 do
            drawText("0D", 100, 100, 30)
            coroutine.yield()
        end
        playMusic()
        dialogBox({
            "[???] Who am I?",
            "[???] I don't know.",
            "[???] Who are you?",
            "[???] I don't know.",
            "[???] Where am I?",
            "[???] I don't know.",
            "[???] It feels good. Warmth, calmness, happiness... Everything is here.",
            "[???] I don't want to leave.",
            "[???] ..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        
        drawBackground(getScreenWidth()/2, getScreenHeight()/2, 1.68, 0)
        
        dialogBox({
            "[???] Huh? Where am I?",
            "[???] This place feels... Nostalgic, i guess. But also there is sadness..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.2, 0)
        dialogBox({
            "[???] This is a hospital. In the corner of the room, engulfed in darkness... a young girl sat on the floor, hugging her knees.",
            "[???] Yukidoh Sayaka. 10 years old. Suffers from DID.",
            "[Sayaka] Hey, you...",
            "[???] Huh? Me?",
            "[Sayaka] Shadow.",
            "[???] What are you talking about? I don't get it.",
            "[Sayaka] You know everything, don't you?",
            "[???] (What is she talking about?...)",
            "[Sayaka] You'll understand everything soon.",
            "[???] Someone told me something similar not too long ago. But I still don't get it... Or maybe it's some strange effect of DID.",
            "[???] Argh..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        local currentTime = getTime()
        while getTime() - currentTime < 1.5 do
            coroutine.yield()
        end
        stopDrawCharacter(0)
        dialogBox({
            "[???] Eno... Enomoto?",
            "[Enomoto] ..."
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        local currentTime = getTime()
        while getTime() - currentTime < 1.5 do
            coroutine.yield()
        end
        dialogBox({
            "In the corner of the room, engulfed in darkness... a young girl sat on the floor, hugging her knees.",
            "Yukidoh Sayaka. 10 years old. Suffers from DID.",
            "Sayaka's tiny back trembled ever so slightly.",
            "[Sayaka] sniff... sniff...",
            "She was sobbing quietly.",
            "Enomoto put his hand on her shoulder.",
            "She glanced up at the boy with her tear-filled eyes and flung herself into his arms, embracing him tightly as if afraid he'd disappear the moment she let go.",
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 1, {r = 90, g = 90, b = 90})
        dialogBox({
            "[Sayaka] I'm scared...",
            "[Sayaka] I'm so scared...",
            "She kept crying as she held onto him with all her strength.",
            "Enomoto gently ran his fingers through her hair.",
            "[Enomoto] It's okay.",
            "He whispered softly into her ear.",
            "[Enomoto] There's nothing to be afraid of.",
            "[Enomoto] Because no matter what happens... I'll be here to protect you.",
            "[Enomoto] There's nothing to be afraid of.",
            "[Enomoto] Because no matter what happens... I'll be here to protect you.",
            "[Sayaka] No...",
            "[Sayaka] That's not it...",
            "The words left her trembling lips as she nervously shook her head.",
            "[Sayaka] What I'm afraid of is...",
            "[Sayaka] ...myself.",
            "[Sayaka] The person I'm afraid of is... myself.",
            "[Enomoto] Still... I'll protect you. Forever. You'll see!",
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        stopDrawCharacter(1)
        drawCharacter(getScreenWidth()/2, getScreenHeight()/2, 2.1, 2)
        dialogBox({
            "[Sayaka] That's impossible.",
            "[Sayaka] There's no way you could...",
            "[Sayaka] Because...",
            "[Sayaka] Because... Satoru-chan, I'll...",
            "[Sayaka] I'll kill you too.",
            "[???] What?... But... This is Enomoto... This isn't Satoru...",
            "[???] Stop... Satoru... Who is it?",
            "[???] Is it... Me?",
            "[???] No... but... who am I?",
        }, {}, -1)
        while isDialogExecuted() do 
            coroutine.yield()
        end
        stopMusic()
        stopDrawCharacter(2)
        stopDrawBackground(0)
        local currentTime = getTime()
        while getTime() - currentTime < 0.4 do
            coroutine.yield()
        end
        unloadMusic()
        unloadCharacter(0)
        unloadCharacter(1)
        unloadCharacter(2)
        unloadBackground(0)
        loadScript("scripts/SE01.lua")
    end)
end

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

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