#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#ID["Main"]
#Title["RaNGE 19 - Wingshooting by Awalys"]
#Text["bird is watching"]
#System["./data/system/Awalys_System.dnh"]
#Player["./data/player/Reisen/player.dnh"]
#Background["./data/background/back_miko.dnh"]

#include"./data/shot/CrispBright4/Shotconstants.dnh"
#include"./data/lib/lib_main.dnh"
#include"./data/lib/Boss_Anim.dnh"
#include"./data/lib/dialogue.dnh"

let dir = GetCurrentScriptDirectory();
let end = false;
let bgm = ObjSound_Create;
let bgm2 = ObjSound_Create;

@Event{
	alternative(GetEventType)
	case(EV_USER_STAGE) {
		let arg = GetEventArgument(0); // X and Y coordinates.
		CreateItemA1(ITEM_SPELL, arg[0], arg[1], 1500);
	}
	case(EV_USER_STAGE+1) {
		PlaySE(ExpSE3);
	}
	case(EV_USER_STAGE+2) {
		PlaySE(SpellGetSE);
	}
	case(EV_USER_STAGE+3) {
		ObjSound_Play(BGM);
	}
	case(EV_USER_STAGE+4) {
		let arg = GetEventArgument(0); // Spell score.
    	AddScore(arg);
	}
	/*case(EV_PAUSE_ENTER){
		ObjSound_SetVolumeRate(BGM, 10);
	}
    case(EV_PAUSE_LEAVE){
		ObjSound_SetVolumeRate(BGM, 100);
	}*/
}

@Initialize{
	TStage;
	
	SetCommonData("Main", GetOwnScriptID());
	SetPlayerLife(6);
}

@MainLoop{
	if(GetPlayerState() == STATE_END){
		FinalizeStage;
	}
	yield;
}

task TStage{
	let pluralboss = dir ~ "./PLR1.dnh";
	let boss = LoadScriptInThread(pluralboss);

	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);

	wait(60*1.5);

	let objText = ObjText_Create(); 
	let objImg1 = ObjPrim_Create(OBJ_SPRITE_2D); // HAHA IT DOESN'T DESYNC REPLAYS ANYMOER
	let objImg2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objImg3 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objBox = ObjPrim_Create(OBJ_SPRITE_2D);
	DialogueBox(objBox);
	wait(60);
	dialogue(objText, 1, "Stop creeping around [r] and show yourself already!");
	character(objImg1, objImg2, 1, UdonNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "I came here for something that's [r] rightfully mine!");
	character(objImg1, objImg2, 1, UdonClosed);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	objBoss = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objBoss);
	BossRender;
	MagicCircle(objBoss);
	ObjMove_SetDestAtWeight(objBoss, cx, 120, 16, 32); PlaySE(FlightSE);
	dialogue(objText, 2, "Ayayayayaya...");
	character(objImg1, objImg2, 2, AyaClosed);
	PlaySE(PlayerShotSE);
	wait(60*1.5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "You're very perceptive, I'll give you that!");
	character(objImg1, objImg2, 2, AyaNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "What business do you have, trespassing [r]on tengu territory, may I ask?");
	character(objImg1, objImg2, 2, AyaSmug);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "Don't act like you don't know why I'm here, birdbrain.");
	character(objImg1, objImg2, 1, UdonSerious);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "You were fast when you stole my Lunatic Gun, [r]though not fast enough for my eyes to recognize.");
	character(objImg1, objImg2, 1, UdonSerious);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "And what are you going to do about it?");
	character(objImg1, objImg2, 2, AyaNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "All you can manage at the moment is fire [r]some lousy beams from your eyes, [r]but I'm afraid that's not gonna do much for you.");
	character(objImg1, objImg2, 2, AyaSmug);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "I did not come here unprepared. [r]I got myself a replacement for the Lunatic Gun.");
	character(objImg1, objImg2, 1, UdonNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, " ");
	character(objImg1, objImg2, 1, UdonClosed); 
	PlaySE(ReloadSE); wait(30); PlaySE(ReloadSE);
	wait(60);
	PlaySE(GunSE);
	wait(60*2);
	dialogue(objText, 1, "A... revolver? I think that's what Eirin called it.");
	character(objImg1, objImg2, 1, UdonNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "A human weapon?");
	character(objImg1, objImg2, 2, AyaHappy);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "I hope you know that youkai [r]are immune to regular metals.");
	character(objImg1, objImg2, 2, AyaNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "Not completely immune.");
	character(objImg1, objImg2, 1, UdonNeut);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "It will just take a lot of bullets.");
	character(objImg1, objImg2, 1, UdonSmug);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "Fortunately, a certain moon rabbit \"acquaintance\" [r]has bestowed me with an infinite supply.");
	character(objImg1, objImg2, 1, UdonClosed);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 1, "Now, it's time to reclaim my stolen possession!!");
	character(objImg1, objImg2, 1, UdonAngery);
	PlaySE(PlayerShotSE);

	NotifyEvent(MyStageID,EV_USER_STAGE+3,NULL); // Music.
	Music;
	MusicText;
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	dialogue(objText, 2, "Ha! A flimsy toy like that is no match [r]for a tengu's swiftness! [r]Go ahead and take it if you can!!");
	character(objImg1, objImg2, 2, AyaSerious);
	PlaySE(PlayerShotSE);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	PlaySE(PlayerShotSE);

	Obj_Delete(objText); // You HAVE to delete these manually.
	Obj_Delete(objImg1);
	Obj_Delete(objImg2);
	Obj_Delete(objBox);

	wait(30);

	ShowInfo;
	wait(60);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	wait(60);

	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	StartScript(boss);
	Obj_Delete(objBoss);

	while(!IsCloseScript(boss)){yield;}

	STGClearScore(1000000000);

	FinalizeStage;
}

task FinalizeStage{
	wait(180);
	StopSound(BGM);
	if(GetScore > GetAreaCommonData("HighScore", "Score", 0)){ // Saves the high score.
		SetAreaCommonData("HighScore", "Score", GetScore);
		SaveCommonDataAreaA1("HighScore");
	}
	CloseStgScene();
}

task ShowInfo{
	let alpha = 0;

	PlaySE(PauseSE);

	let txt = ObjText_Create();
	ObjText_SetFontType(txt, "Russell Square Regular");
	ObjText_SetFontSize(txt, 12);
	ObjText_SetFontBold(txt, true);
	ObjText_SetFontColorTop(txt, 255, 205, 0);
	ObjText_SetFontColorBottom(txt, 64, 64, 64);
	ObjText_SetFontBorderType(txt, BORDER_FULL);
	ObjText_SetFontBorderColor(txt,0, 0, 0);
	ObjText_SetFontBorderWidth(txt, 1);
	Obj_SetRenderPriorityI(txt, 75);
	ObjRender_SetX(txt, GetStgFrameWidth/2-175);
	ObjRender_SetY(txt, GetStgFrameHeight/2-175);
	ObjText_SetHorizontalAlignment(txt, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(txt, 350);

	let img2 = GetCurrentScriptDirectory ~ "./data/background/back_img/darken.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj2, img2);
	ObjSprite2D_SetSourceRect(obj2, 0,0,450,450);
	ObjSprite2D_SetDestCenter(obj2);
	Obj_SetRenderPriorityI(obj2, 51);
	ObjRender_SetScaleXYZ(obj2, 1, 1, 0);
	ObjRender_SetAlpha(obj2, 200);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetPosition(obj2, GetStgFrameWidth/2,GetStgFrameHeight/2, 0);
	ObjRender_SetAngleXYZ(obj2, 0, 0, 0);

	ObjText_SetText(txt, "Instead of a regular shot, [r]Reisen is equipped with a revolver! [r] [r] [r]
	Although she has infinite ammo, [r]she still has to reload it with new bullets, [r]and the magazine can only hold [r]up to 6 bullets at a time. [r] [r] [r]
	To reload, simply [r]stop shooting for a few seconds. [r]You reload faster when you're focused. [r] [r] [r]
	Because this is a revolver, you can fire [r]as fast as you can pull the trigger! [r] [r] [r]
	Try to keep your gun full at all times, [r]then strike at full force [r]whenever an oppurtunity arises!");

	loop(60){
		ObjRender_SetAlpha(obj2, alpha-50);
		ObjRender_SetAlpha(txt, alpha);
		alpha += 255/60;
		yield;
	}
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	PlaySE(ChooseSE);
	loop(60){
		ObjRender_SetAlpha(obj2, alpha-50);
		ObjRender_SetAlpha(txt, alpha);
		alpha -= 255/60;
		yield;
	}
	Obj_Delete(txt);
	Obj_Delete(obj2);
}

task MusicText{
	let alpha = 0;

	let txt = ObjText_Create();
	ObjText_SetFontType(txt, "Andale Mono");
	ObjText_SetFontSize(txt, 12);
	ObjText_SetFontBold(txt, false);
	ObjText_SetFontColorTop(txt, 255, 255, 255);
	ObjText_SetFontColorBottom(txt, 0, 200, 100);
	ObjText_SetFontBorderType(txt, BORDER_FULL);
	ObjText_SetFontBorderColor(txt,0, 0, 0);
	ObjText_SetFontBorderWidth(txt, 1);
	Obj_SetRenderPriorityI(txt, 75);
	ObjRender_SetX(txt, 0);
	ObjRender_SetY(txt, GetStgFrameHeight-12);
	ObjText_SetHorizontalAlignment(txt, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(txt, 350);

	ObjText_SetText(txt, "Music: 1NF3$+@+!0N by DM Dokuro");

	loop(30){
		ObjRender_SetAlpha(txt, alpha);
		alpha += 255/30;
		yield;
	}
	wait(60*2);
	loop(15){
		ObjRender_SetAlpha(txt, alpha);
		alpha -= 255/15;
		yield;
	}
	Obj_Delete(txt);
}