#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#System["./System/Default_System.txt"]
#Title["Doppelter Vormund"]
#Text["Made by Vigor[r]Player by Vigor[r]Hint: left is easier than right[r]Easy is highly recommended for first-timer"]
#Player["./player/DVPlayer/Player.txt"]
#Background["./background.txt"]
#include"./function.txt"

let song = ObjSound_Create;
let vol=0;

let DeathCount=true;
let playerdeath=0;

let midboss=true;
SetCommonData("sevol",80);
TPlayerDeath;

let menudif=true;

@Initialize
{
	TPlural;
}

@Event
{
	alternative(GetEventType())
	case(EV_PAUSE_ENTER)
	{
		ObjSound_Stop(song);
	}
	case(EV_PAUSE_LEAVE)
	{
		if(menudif==false){ObjSound_Play(song);}
	}
}

@MainLoop
{
	ObjSound_SetRestartEnable(song,true);
	ObjSound_SetLoopEnable(song,true);
	ObjSound_SetVolumeRate(song,vol);
	yield;
}

@Finalize
{
}

task TPlural
{
	let delay=180;
	let xo=GetStgFrameWidth/2;
	let yo=GetStgFrameHeight/2;
	SetPlayerLife(2);
	SetPlayerSpell(3);
	let spd = GetPlayerSpeed;
	SetPlayerSpeed(0,0);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	ObjSound_Load(song,dir ~ "./bgm/NaBD,bMiS.ogg");
	ObjSound_Play(song);
	SongDisplay(2,delay);
	ScreenCover(delay,1);
	loop(delay){vol+=100/delay;yield;}
	TMenuDif;
	while(menudif==true){yield;}
	DifDisplay(GetCommonData("diff",1));
	SetPlayerSpeed(spd[0],spd[1]);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	otherdrop(xo-80,yo,500,"s");
	otherdrop(xo,yo,500,"p");
	otherdrop(xo+80,yo,500,"l");
	loop(delay){vol-=100/delay;yield;}
	SongDisplay(1,delay/2);
	ObjSound_Stop(song);
	ObjSound_Load(song,dir ~ "./bgm/PWDE.ogg");
	ObjSound_Play(song);
	loop(delay){vol+=100/delay;yield;}
	Boss;
	if(GetCommonData("nextboss","tenkai")=="senri"){BossS;}
	if(GetCommonData("nextboss","tenkai")=="tenkai"){BossT;}
	if(GetCommonData("scriptend",false)==true)
	{
		SEPlay(dir ~ "se/boss_end.wav");
		loop(60)
		{
			Charge(GetStgFrameWidth/2,GetStgFrameHeight/2-150,rand(60,120),255,255,255,1,BLEND_SUBTRACT);
			Charge(GetStgFrameWidth/2,GetStgFrameHeight/2-150,rand(60,120),255,255,255,1,BLEND_ADD_ARGB);
		}
	}
	loop(120){vol-=100/240;yield;}
	if(GetCommonData("scriptend",false)==true)
	{
		ScreenCover(120,3);
		loop(120){vol-=100/240;yield;}
		ObjSound_Stop(song);
		ScreenCover(120,2);
		loop(120){yield;}
		Ending;
		loop(960){yield;}
	}
	if(GetCommonData("scriptend",false)==false)
	{
		loop(120){vol-=100/240;yield;}
		ObjSound_Stop(song);
	}
	
	CloseStgScene();
}

task TPlayerDeath
{
	TText;
	task TText
	{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=dir~"BG/SystemPicture.png";
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,416,96,512,128);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj,1);
		ObjRender_SetColor(obj,255,128,128);
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0);
		ObjRender_SetPosition(obj,595,230,0);
		DrawPicture;
		task DrawPicture
		{
			let path = dir ~ "BG/SystemPicture.png";

			let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
			ObjPrim_SetTexture(obj, path);
			Obj_SetRenderPriority(obj, 1);
			ObjRender_SetY(obj,250);
			ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
			let count = 3;

			let objScene = ID_INVALID;
			loop
			{
				while(GetPlayerState==STATE_DOWN)
				{
					yield;
					if(GetPlayerState==STATE_NORMAL){playerdeath++;}
				}
				RenderTimer();
				yield;
			}

			function RenderTimer()
			{
				let timer = playerdeath;
				timer = min(timer, 99.99);
				let listNum = DigitToArray(timer, count);

				ObjSpriteList2D_ClearVertexCount(obj);
				ascent(iObj in 0 .. count)
				{
					let num = listNum[iObj];
					ObjRender_SetX(obj,(550+25)+iObj*10);
					ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
					ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
					ObjSpriteList2D_AddVertex(obj);
				}
			}
		}
	}
}

task Ending
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let objx = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,3);
	Obj_SetRenderPriority(objx,3);
	ObjPrim_SetTexture(obj,dir ~ "BG/ending.png");
	ObjPrim_SetTexture(objx,dir ~ "BG/end1.png");
	if(playerdeath<3){ObjPrim_SetTexture(objx,dir ~ "BG/end2.png");}
	ObjSprite2D_SetSourceRect(objx,0,0,640,480);
	ObjSprite2D_SetDestCenter(objx);
	ObjRender_SetPosition(objx,GetScreenWidth/2,GetScreenHeight/2-55,0);
	ObjRender_SetScaleXYZ(objx,460/640,460/640,1);
	ObjRender_SetAlpha(objx,0);
	let sr=0;
	loop(120)
	{
		sr+=512/120;
		ObjSprite2D_SetSourceRect(obj,0,0,sr,38);
		ObjRender_SetPosition(obj,GetScreenWidth/2,GetScreenHeight/2+170,0);
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
	loop(120){yield;}
	sr=0;
	loop(120)
	{
		sr+=512/120;
		ObjSprite2D_SetSourceRect(obj,0,38,sr,68);
		ObjRender_SetPosition(obj,GetScreenWidth/2,GetScreenHeight/2+170,0);
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
	loop(120){yield;}
	sr=0;
	loop(120)
	{
		sr+=512/120;
		if(playerdeath>=3){ObjSprite2D_SetSourceRect(obj,0,68,sr,96);}
		if(playerdeath<3){ObjSprite2D_SetSourceRect(obj,0,208,sr,232);}
		ObjRender_SetPosition(obj,GetScreenWidth/2,GetScreenHeight/2+170,0);
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
	loop(120){yield;}
	let al=0;
	loop(120)
	{
		al+=256/120;
		if(playerdeath>=3){ObjSprite2D_SetSourceRect(obj,0,96,512,208);}
		if(playerdeath<3){ObjSprite2D_SetSourceRect(obj,0,232,512,344);}
		ObjRender_SetPosition(obj,GetScreenWidth/2,GetScreenHeight/2+170,0);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetAlpha(objx,al);
		yield;
	}
	loop(120){yield;}
}

task DifDisplay(dif)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,1);
	ObjPrim_SetTexture(obj,dir ~ "BG/diff.png");
	ObjSprite2D_SetSourceRect(obj,128,96*(dif-1)+30,512-128,96*dif-28);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetScaleXYZ(obj,0.2,0.2,0);
	ObjRender_SetPosition(obj,GetScreenWidth-45,GetScreenHeight/2-225,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	let al=0;
	loop
	{
		if(al<256){al+=256/120;}
		ObjRender_SetAlpha(obj,al);
		yield;
	}
	Obj_Delete(obj);
}

task SongDisplay(code,delay)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.8);
	ObjPrim_SetTexture(obj,dir ~ "BG/bgm.png");
	ObjSprite2D_SetSourceRect(obj,0,64*(code-1),512,64*code);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.6,0.6,0);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2-210,0);
	let al=0;
	loop(delay)
	{
		al+=256/delay;
		ObjRender_SetAlpha(obj,al);
		yield;
	}
	loop(delay)
	{
		al-=256/delay;
		ObjRender_SetAlpha(obj,al);
		yield;
	}
	Obj_Delete(obj);
}

task ScreenCover(delay,type)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.79);
	ObjPrim_SetTexture(obj,dir ~ "BG/spellbg.png");
	ObjSprite2D_SetSourceRect(obj,0,0,480,480);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,GetStgFrameWidth/480,GetStgFrameHeight/480,0);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	let al;
	if(type==1)
	{
		ObjRender_SetColor(obj,0,0,0);
		al=256;
		loop(delay){yield;}
		loop(delay-60)
		{
			al-=256/(delay-60);
			ObjRender_SetAlpha(obj,al);
			yield;
		}
		Obj_Delete(obj);
	}
	if(type==2)
	{
		ObjRender_SetColor(obj,255,255,255);
		al=0;
		loop(delay)
		{
			al+=256/delay;
			ObjRender_SetAlpha(obj,al);
			yield;
		}
	}
	if(type==3)
	{
		ObjRender_SetColor(obj,0,0,0);
		al=0;
		loop(delay)
		{
			al+=256/delay;
			ObjRender_SetAlpha(obj,al);
			yield;
		}
	}
}

function Boss
{
	let dir=GetCurrentScriptDirectory();
	let obj=ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add(obj,0,dir~"Attack1.txt");
	ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj)){yield;}
}
	
function BossT
{
	let dir=GetCurrentScriptDirectory();
	let obj=ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add(obj,0,dir~"Attack2T.txt");
	ObjEnemyBossScene_Add(obj,1,dir~"Attack3T.txt");
	ObjEnemyBossScene_Add(obj,2,dir~"Attack4S.txt");
	ObjEnemyBossScene_Add(obj,3,dir~"Attack5S.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"Attack6.txt");
	ObjEnemyBossScene_Add(obj,5,dir~"Attack7.txt");
	ObjEnemyBossScene_Add(obj,6,dir~"Attack8.txt");
	ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj)){yield;}
}
	
function BossS
{
	let dir=GetCurrentScriptDirectory();
	let obj=ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add(obj,0,dir~"Attack2S.txt");
	ObjEnemyBossScene_Add(obj,1,dir~"Attack3S.txt");
	ObjEnemyBossScene_Add(obj,2,dir~"Attack4T.txt");
	ObjEnemyBossScene_Add(obj,3,dir~"Attack5T.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"Attack6.txt");
	ObjEnemyBossScene_Add(obj,5,dir~"Attack7.txt");
	ObjEnemyBossScene_Add(obj,6,dir~"Attack8.txt");
	ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj)){yield;}
}

task TMenuDif
{
	let texts = ["Easy","Normal","Hard"];
	let mx = GetStgFrameWidth/2;
	let my = GetStgFrameHeight/2;
	let obj = [];
	let c=0;
	let w=0;
	let delay=15;
	let i = 0;
	loop(length(texts)+1)
	{
		obj= obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		Obj_SetRenderPriority(obj[i],0.7);
		ObjPrim_SetTexture(obj[i],dir ~ "BG/diff.png");
		ObjSprite2D_SetSourceRect(obj[i],0,0,512,96);
		ObjSprite2D_SetDestCenter(obj[i]);
		ObjRender_SetPosition(obj[i],mx,my,0);
		ObjRender_SetScaleXYZ(obj[i],420/512,420/512,1);
		fade(obj[i],i);
		
		task fade(obj,i)
		{
			let al=0;
			loop
			{
				if(i==0){ObjSprite2D_SetSourceRect(obj,0,0,512,96);}
				if(i==1){ObjSprite2D_SetSourceRect(obj,0,96,512,192);}
				if(i==2){ObjSprite2D_SetSourceRect(obj,0,192,512,288);}
				if(c==i && al<256){al+=256/delay;}
				if(c!=i && al>0){al-=256/delay;}
				ObjRender_SetAlpha(obj,al);
				w++;
				yield;
				if(menudif==false){Obj_Delete(obj);}
			}
		}
		i++;
	}
	ObjRender_SetBlendType(obj[length(texts)],BLEND_ADD_ARGB);
	while(menudif==true)
	{
		yield;
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			SEPlay(dir ~ "se/cursor_01.wav");
			c-=1;
			w=0;
			yield;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			SEPlay(dir ~ "se/cursor_01.wav");
			c+=1;
			w=0;
			yield;
		}
		if(c<0){c=2;}
		if(c>2){c=0;}
		ObjRender_SetPosition(obj[length(texts)], mx,my,0);
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH && w>delay)
		{
			SEPlay(dir ~ "se/cursor_04.wav");
			alternative(c)
			case(0)
			{
				SetCommonData("diff",1);
				menudif=false;
			}
			case(1)
			{
				SetCommonData("diff",2);
				menudif=false;
			}
			case(2)
			{
				SetCommonData("diff",3);
				menudif=false;
			}
		}
	}
}

function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res))){res = [0] ~ res;}

	return res;
}