#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Tutorial: Dialogue System"]
#Text["Tutorial: Dialogue System"]
let CSD = GetCurrentScriptDirectory;

let objEnemy;





//--------------------------------




@Initialize {
    objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objEnemy);

	TEvent;
    TEnd;
	
	
}
@MainLoop {
    yield;
}
@Event {
    alternative(GetEventType())
    case(EV_REQUEST_LIFE) {
        SetScriptResult(1500);
    }
}
//----------------------------------------------------------------------------------------------
function CreateDialogueEvent {
    // We will want the portraits to render first, as the text will render above the portraits.
    // We will use Render Priorities 81 and 82 for portraits (82 to be used in the case of multiple portraits)
    let imgpathL = GetCurrentScriptDirectory() ~ "./sprite/empittzBig.png";
    let objImgLeft = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objImgLeft, 81);
    ObjPrim_SetTexture(objImgLeft, imgpathL);
     ObjSprite2D_SetSourceRect(objImgLeft, 0, 0, 600, 752);
	ObjRender_SetScaleXYZ(objImgLeft,0.6,0.6,0);
    ObjSprite2D_SetDestCenter(objImgLeft);
    ObjRender_SetX(objImgLeft, GetStgFrameLeft() + GetStgFrameWidth()/2 - 100);
    ObjRender_SetY(objImgLeft, 288);
    let imgpathR = GetCurrentScriptDirectory() ~ "./sprite/lightgirl1.png";
    let objImgRight = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objImgRight, 81);
    ObjPrim_SetTexture(objImgRight, imgpathR);
    ObjSprite2D_SetSourceRect(objImgRight, 0, 0, 280, 390);
	ObjRender_SetScaleXYZ(objImgRight,0.8,0.8,0);
    ObjSprite2D_SetDestCenter(objImgRight);
    ObjRender_SetX(objImgRight, GetStgFrameLeft() + GetStgFrameWidth()/2 + 100);
    ObjRender_SetY(objImgRight, 288);
    // Prepare text background. We will use render priority 83.
    let TEXTBOX_BUFFER = 16; // Buffers on edges
    let TEXTBOX_WIDTH = GetStgFrameWidth - TEXTBOX_BUFFER*2;
    let TEXTBOX_HEIGHT = 48;
    let objTextBack = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(objTextBack, 83);
    ObjSprite2D_SetSourceRect(objTextBack, 0, 0, 1, 1); // Use default white texture
    ObjSprite2D_SetDestRect(objTextBack, 0, 0, TEXTBOX_WIDTH, TEXTBOX_HEIGHT);
    ObjRender_SetX(objTextBack, GetStgFrameLeft() + TEXTBOX_BUFFER);
    ObjRender_SetY(objTextBack, GetStgFrameTop() + GetStgFrameHeight() - TEXTBOX_HEIGHT - TEXTBOX_BUFFER);
    ObjRender_SetColor(objTextBack, 0, 0, 0);
    ascent(i in 0..30){
        ObjRender_SetAlpha(objTextBack, 192/30*i);
        yield;
    }
    ObjRender_SetAlpha(objTextBack, 192);
    // Prepare text object. We will use render priority 83.
    let TEXT_MARGIN_LR = 8; // Left and Right Margins.
    let TEXT_MARGIN_TOP = 6; // Top Margin
    let TEXT_MAXWIDTH = TEXTBOX_WIDTH - TEXT_MARGIN_LR*2;
    let objText = ObjText_Create();
    ObjText_SetText(objText, "Default[r]Default");
    ObjText_SetFontSize(objText, 15);
    ObjText_SetFontBold(objText, false);
    ObjText_SetFontType(objText, "Helvetica");
    Obj_SetRenderPriorityI(objText, 83);
    ObjText_SetFontColorTop(objText, 255, 255, 255); //Will be altered inside the event
    ObjText_SetFontColorBottom(objText, 255, 255, 255);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 255, 255, 255);
    ObjText_SetFontBorderWidth(objText, 1);
    ObjText_SetMaxWidth(objText, TEXT_MAXWIDTH);
    ObjRender_SetX(objText, GetStgFrameLeft() + TEXTBOX_BUFFER + TEXT_MARGIN_LR);
    ObjRender_SetY(objText, GetStgFrameTop() + GetStgFrameHeight() - TEXTBOX_HEIGHT - TEXTBOX_BUFFER + TEXT_MARGIN_TOP);
    return [objTextBack, objText, objImgLeft, objImgRight];
}

//-------------------------------------------------------------------------------------------

// Given an array of object IDs, destroys all of them
function DestroyDialogueEvent(objects) {
    ascent(i in 0..length(objects)) {
        Obj_Delete(objects[i]);
    }
}




// On player key press, wait specified number of frames
function EventWaitA1(frames) {
    while(GetVirtualKeyState(VK_OK) == KEY_FREE || GetVirtualKeyState(VK_OK) == KEY_HOLD){yield;}
    loop(frames){yield;}
}
// Timed wait for specified number of frames
function EventWaitA2(frames) {
    loop(frames){yield;}
}
// At start of event, wait for key to be released, wait for key to be pressed, and then wait specified number of frames
function EventWaitA3(frames) {
    while(GetVirtualKeyState(VK_OK) == KEY_HOLD){yield;}
    while(GetVirtualKeyState(VK_OK) != KEY_PUSH){yield;}
    loop(frames){yield;} 
}
// Sets portrait to background
function SetPortraitToBackground(portrait) {
    ObjRender_SetAlpha(portrait, 192);
    ObjRender_SetColor(portrait, 192, 192, 192);
}
// Sets portrait to foreground
function SetPortraitToForeground(portrait) {
    ObjRender_SetAlpha(portrait, 255);
    ObjRender_SetColor(portrait, 255, 255, 255);
}

task TEvent {
    // Obtain relevant objects
    let dialogueobjects = CreateDialogueEvent();
    let textobj = dialogueobjects[1];
    let leftportrait = dialogueobjects[2];
    let rightportrait = dialogueobjects[3];
    SetForbidPlayerSpell(true);
    SetForbidPlayerShot(true);
    let pathempitts1 = GetCurrentScriptDirectory() ~ "./sprite/empittsbig.png";
    let pathempitts2 = GetCurrentScriptDirectory() ~ "./sprite/protagsmile.png";
    let pathlight1 = GetCurrentScriptDirectory() ~ "./sprite/lightgirl1.png";
	let pathlight2 = GetCurrentScriptDirectory() ~ "./sprite/lightgirl2.png";
    
    // Set initial textures
    ObjPrim_SetTexture(leftportrait, pathempitts2);
    ObjPrim_SetTexture(rightportrait, pathlight1 );
    ObjText_SetText(textobj, "Oh, here we are! Thank you sweetie, I'll buy you [r]some treats while I'm at the surface");
    ObjText_SetFontBorderColor(textobj, 255, 128, 192);
    SetPortraitToForeground(leftportrait); SetPortraitToBackground(rightportrait);
    EventWaitA1(15);
    ObjText_SetText(textobj, ".....<3");
    ObjText_SetFontBorderColor(textobj, 0, 0, 200);
    ObjPrim_SetTexture(rightportrait, pathlight2);
    SetPortraitToForeground(rightportrait); SetPortraitToBackground(leftportrait);
     EventWaitA1(15);
	 ObjPrim_SetTexture(leftportrait, pathempitts2);
    ObjPrim_SetTexture(rightportrait, pathlight1 );
    ObjText_SetText(textobj, "And to you, thank you for playing this demo!! I hope [r]to see you again~");
    ObjText_SetFontBorderColor(textobj, 255, 128, 192);
    SetPortraitToForeground(leftportrait); SetPortraitToBackground(rightportrait);
    EventWaitA1(15);
    DestroyDialogueEvent(dialogueobjects);
    SetForbidPlayerSpell(false);
    SetForbidPlayerShot(false);
    ObjEnemy_SetLife(objEnemy, -1); // Trigger script end
}

function wait(w){
       loop(w) {
	     yield;
		 }
	}
	
task TEnd {
   
    while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) {
        yield;
    }
	
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    Obj_Delete(objEnemy);
    loop(30){yield;}
    CloseScript(GetOwnScriptID());
}