#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#System["./System/Default_System.txt"]
#Title["Unseasonal Mooning Daydream"]
#Text["Made by Vigor (NecroHeart)[r]Immortal-no Bomb mode[r]Graphic(some)&Shot:Terraformer"]
#Background["./background.txt"]
#Player["./player/Aya.txt"]
#Image["./lib/lib_img/title.png"]
#include"./function.txt"
#include"./lib/lib_sound/_PathSound.txt"
#include"./lib/lib_img/_PathImage.txt"

let bgmObj 		= ObjSound_Create;
let bgmVol		= 80;
ObjSound_Load	(bgmObj,bgmPreBattle);
ObjSound_SetLoopSampleCount	(bgmObj,0,5158656);
SetCommonData("bgmPreBattle",true);
SetCommonData("bgmBoss",false);
SetCommonData("autoSnap",false);
SetCommonData("photoCount",0);

let immortal	= true;

@Initialize
{
	SetCommonData("bgMode",0);
	SetCommonData("bgModeTime",180);
	SetCommonData("bgSpeed",0.5);
	SetPlayerClip(95,15,545,465);
	TPlural;
}

@Event
{
	alternative(GetEventType())
	case(EV_PAUSE_ENTER)	{ObjSound_Stop(bgmObj);}
	case(EV_PAUSE_LEAVE)	{ObjSound_Play(bgmObj);}
}

@MainLoop
{
	ObjSound_SetRestartEnable	(bgmObj,true);
	ObjSound_SetLoopEnable		(bgmObj,true);	
	ObjSound_SetVolumeRate		(bgmObj,bgmVol);
	if(GetCommonData("autoSnap",false) == true)
	{
		SetCommonData("photoCount",GetCommonData("photoCount",0)+1);
		Snapshot(GetCommonData("photoCount",0));
		SetCommonData("autoSnap",false);
	}
	yield;
}

@Finalize{}

task TPlural
{
	ObjSound_Play		(bgmObj);
	SetPlayerLife(9);
	playerHUD;
	bossHUD;
	bgmDelay;
	loop(60){yield;}
	stageTitle;
	loop(480){yield;}
	Boss;
}

task bgmDelay
{
	while(GetCommonData("bgmPreBattle",false) == true){yield;}
	loop(60)
	{
		bgmVol -= 80/60;
		yield;
	}
	ObjSound_Stop				(bgmObj);
	while(GetCommonData("bgmBoss",true) == false){yield;}
	bgmVol = 100;
	ObjSound_Load				(bgmObj,bgmBoss);
	ObjSound_SetLoopSampleCount	(bgmObj,0,11857920);
	ObjSound_Play				(bgmObj);
	while(GetCommonData("bgmBoss",false) == true){yield;}
	let speed = 3;
	loop(180)
	{
		bgmVol -= 100/180;
		speed -= 2.5/360;
		SetCommonData("bgSpeed",speed);
		yield;
	}
	ObjSound_Stop				(bgmObj);
	loop(180)
	{
		speed -= 2.5/360;
		SetCommonData("bgSpeed",speed);
		yield;
	}
	loop(180){yield;}
	bgmVol = 0;
	ObjSound_Load				(bgmObj,bgmFinal);
	ObjSound_SetLoopSampleCount	(bgmObj,0,7371776);
	ObjSound_Play				(bgmObj);
	loop(120)
	{
		bgmVol += 100/120;
		yield;
	}
	loop(120)
	{
		speed += 2.5/120;
		SetCommonData("bgSpeed",speed);
		yield;
	}
}

function Boss
{
	let obj = ObjEnemyBossScene_Create();
	ObjEnemyBossScene_Add		(obj,0,dir~"Attack0.txt");
	ObjEnemyBossScene_Add		(obj,1,dir~"Attack1.txt");
	ObjEnemyBossScene_Add		(obj,2,dir~"Attack2.txt");
	ObjEnemyBossScene_Add		(obj,3,dir~"Attack3.txt");
	ObjEnemyBossScene_Add		(obj,4,dir~"Attack4.txt");
	ObjEnemyBossScene_Add		(obj,5,dir~"Attack5.txt");
	ObjEnemyBossScene_Add		(obj,6,dir~"Attack6.txt");
	ObjEnemyBossScene_Add		(obj,7,dir~"Attack7.txt");
	ObjEnemyBossScene_Add		(obj,8,dir~"Attack8.txt");
	ObjEnemyBossScene_Regist	(obj);
	while(!Obj_IsDeleted(obj))	{yield;}
	loop(240){yield;}
	CloseStgScene();
}

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;
}

task stageTitle
{
	let pos = [320,120];
	stageIcon;
	stageLevel;
	loop(60){yield;}
	stageName;
	loop(90){yield;}
	stageDetail;
	task stageIcon
	{
		let dim = [513,788];
		let scale = 96;
		let obj 	= ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture				(obj,sfxCedar);
		ObjSprite2D_SetSourceRect		(obj,0,0,dim[0],dim[1]);
		ObjSprite2D_SetDestCenter		(obj);
		ObjRender_SetScaleXYZ			(obj,scale/dim[0],scale/dim[1],1);
		let a = 30;
		let yy = -45;
		let alpha = 0;
		let c = 1;
		loop(120)
		{
			a -= 0.5*c;
			c -= 1/420;
			yy += 0.5*c;
			alpha += 255/120;
			ObjRender_SetAlpha	(obj,alpha);
			ObjRender_SetColor	(obj,120+60*c,90+150*c,60+90*c);
			ObjRender_SetPosition			(obj,pos[0]-120,pos[1]+yy,0);
			ObjRender_SetAngleZ				(obj,a);
			yield;
		}
		loop(240)
		{
			a -= 0.5*c;
			c -= 1/420;
			yy += 0.5*c;
			ObjRender_SetColor	(obj,120+60*c,90+150*c,60+90*c);
			ObjRender_SetPosition			(obj,pos[0]-120,pos[1]+yy,0);
			ObjRender_SetAngleZ				(obj,a);
			yield;
		}
		loop(60)
		{
			a -= 0.5*c;
			c -= 1/420;
			yy += 0.5*c;
			alpha -= 255/60;
			scale += 64/60;
			ObjRender_SetAlpha	(obj,alpha);
			ObjRender_SetColor	(obj,120+60*c,90+150*c,60+90*c);
			ObjRender_SetScaleXYZ			(obj,scale/dim[0],scale/dim[1],1);
			ObjRender_SetPosition			(obj,pos[0]-120,pos[1]+yy,0);
			ObjRender_SetAngleZ				(obj,a);
			yield;
		}
		Obj_Delete(obj);
	}
	task stageLevel
	{
		let fontsize = 15;
		let obj = ObjText_Create;
		ObjText_SetFontSize				(obj,fontsize);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		ObjText_SetText					(obj,"STAGE UNSEASONAL Forest of Inhuman Dream");
		ObjText_SetFontBorderWidth		(obj,3);
		ObjText_SetFontBorderType		(obj,BORDER_FULL);
		ObjText_SetFontBorderColor		(obj,120,60,120);
		ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
		let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
		ObjRender_SetPosition			(obj,pos[0]-length[0]+10,pos[1]-length[1]-1-30,0);
		ObjRender_SetColor				(obj,240,120,240);
		ObjRender_SetScaleXYZ			(obj,0.8,1,1);
		let alpha = 0;
		loop(120)
		{
			alpha += 255/120;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		loop(240){yield;}
		loop(60)
		{
			alpha -= 255/60;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		Obj_Delete(obj);
	}
	task stageName
	{
		let fontsize = 25;
		let obj = ObjText_Create;
		ObjText_SetFontSize				(obj,fontsize);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		ObjText_SetText					(obj,"Out of Season Omen");
		ObjText_SetFontBold				(obj,true);
		ObjText_SetFontBorderWidth		(obj,3);
		ObjText_SetFontBorderType		(obj,BORDER_FULL);
		ObjText_SetFontBorderColor		(obj,120,60,120);
		ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
		let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
		ObjRender_SetPosition			(obj,pos[0]-length[0],pos[1]-length[1]-1,0);
		ObjRender_SetColor				(obj,240,240,240);
		//ObjRender_SetScaleXYZ			(obj,1.2,1,1);
		let alpha = 0;
		loop(120)
		{
			alpha += 255/120;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		loop(180){yield;}
		loop(60)
		{
			alpha -= 255/60;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		Obj_Delete(obj);
	}
	task stageDetail
	{
		let fontsize = 20;
		let obj = ObjText_Create;
		ObjText_SetFontSize				(obj,fontsize);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		ObjText_SetText					(obj,"Unseasonal Mooning Daydream");
		ObjText_SetFontBorderWidth		(obj,3);
		ObjText_SetFontBorderType		(obj,BORDER_FULL);
		ObjText_SetFontBorderColor		(obj,120,60,120);
		ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
		let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
		ObjRender_SetPosition			(obj,pos[0]-length[0],pos[1]-length[1]-1+30,0);
		ObjRender_SetColor				(obj,240,240,240);
		let alpha = 0;
		loop(120)
		{
			alpha += 255/120;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		loop(90){yield;}
		loop(60)
		{
			alpha -= 255/60;
			ObjRender_SetAlpha	(obj,alpha);
			yield;
		}
		Obj_Delete(obj);
	}
}

task playerHUD
{
	let midX 		= 595;
	let startY		= 210;
	let fontsize 	= 10;
	frame;
	time;
	death;
	fps;
	task frame
	{
		let obj = ObjPrim_Create	(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(obj,systemImg);
		Obj_SetRenderPriority		(obj,1);
		ObjSprite2D_SetSourceRect	(obj,0, 0, 640, 480);
		ObjSprite2D_SetDestRect		(obj,0, 0, 640, 480);
		ObjRender_SetAlpha			(obj,210);
	}
	task time
	{
		let obj = ObjText_Create;
		Obj_SetRenderPriority			(obj,52);
		ObjText_SetFontSize				(obj,fontsize);
		ObjRender_SetBlendType			(obj,BLEND_INV_DESTRGB);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		loop
		{
			let time = ObjEnemyBossScene_GetInfo (GetEnemyBossSceneObjectID,INFO_TIMERF) / 60;
			if(time > 0 && time <= 10 && time%1==0)	{playSE(seTimeout0,90);}
			let text = " ";
			if(time > 0){text = "time "~rtos("0.00",time);}
			if(time >= 10){text = "time "~rtos("00.00",time);}
			if(time >= 100){text = "time "~rtos("000.00",time);}
			ObjText_SetText					(obj,text);
			ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
			let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
			ObjRender_SetPosition			(obj,midX-length[0],startY+30-length[1],0);
			yield;
		}
	}
	task death
	{
		let obj = ObjText_Create;
		Obj_SetRenderPriority			(obj,52);
		ObjText_SetFontSize				(obj,fontsize);
		ObjRender_SetBlendType			(obj,BLEND_INV_DESTRGB);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		let alp = 0;
		let death = 0;
		loop
		{
			let life = GetPlayerLife;
			if(immortal == false && alp > 0){alp = 0;}
			if(immortal == true && alp < 256){alp = 256;}
			let text = rtos("#0",death);
			ObjText_SetText					(obj,"Death "~text);
			ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
			let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
			ObjRender_SetPosition			(obj,(midX-10)+10-length[0],startY+40-length[1],0);
			ObjRender_SetAlpha				(obj,alp);
			yield;
			if(life > GetPlayerLife)	{death += life - GetPlayerLife;}
			if(immortal == true)		{SetPlayerLife(life);}
		}
	}
	task fps
	{
		let obj = ObjText_Create;
		Obj_SetRenderPriority			(obj,52);
		ObjText_SetFontSize				(obj,fontsize);
		ObjRender_SetBlendType			(obj,BLEND_INV_DESTRGB);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		loop
		{
			let text = rtos("0.00",GetCurrentFps);
			if(GetCurrentFps >= 10){rtos("00.00",GetCurrentFps);}
			if(GetCurrentFps >= 100){rtos("000.00",GetCurrentFps);}
			if(IsReplay)
			{
				text = rtos("0.00",GetReplayFps);
				if(GetReplayFps >= 10){rtos("00.00",GetReplayFps);}
				if(GetReplayFps >= 100){rtos("000.00",GetReplayFps);}
			}
			ObjText_SetText					(obj,"fps "~text);
			ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
			let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
			ObjRender_SetPosition			(obj,midX-length[0],startY+50-length[1],0);
			yield;
		}
	}
}

task bossHUD
{
	let fontsize 	= 10;
	let midY = 17;
	lifegauge;
	lifenumber;
	task lifegauge
	{
		let dim = [460,14];
		let margin = 2;
		let obj1=ObjPrim_Create(OBJ_SPRITE_2D);
		let obj2=ObjPrim_Create(OBJ_SPRITE_2D);
		let obj3=ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(obj1,sfxSquare);
		ObjPrim_SetTexture			(obj2,sfxSquare);
		ObjPrim_SetTexture			(obj3,sfxSquare);
		Obj_SetRenderPriority		(obj1,1);
		Obj_SetRenderPriority		(obj2,1);
		Obj_SetRenderPriority		(obj3,1);
		ObjSprite2D_SetSourceRect	(obj1,0,0,dim[0],dim[1]);
		ObjSprite2D_SetSourceRect	(obj2,0,0,dim[0] - (margin * 2),dim[1] - (margin * 2));
		ObjSprite2D_SetSourceRect	(obj3,0,0,dim[0] - (margin * 2),dim[1] - (margin * 2));
		ObjSprite2D_SetDestCenter	(obj1);
		ObjSprite2D_SetDestRect		(obj2,0,-(dim[1] / 2 - margin),dim[0] - (margin * 2),dim[1] / 2 - margin);
		ObjSprite2D_SetDestRect		(obj3,0,-(dim[1] / 2 - margin),dim[0] - (margin * 2),dim[1] / 2 - margin);
		ObjRender_SetAngleZ			(obj2,180);
		ObjRender_SetAngleZ			(obj3,180);
		ObjRender_SetPosition		(obj1,320,midY,0);
		ObjRender_SetColor			(obj1,90,90,90);
		ObjRender_SetPosition		(obj2,320 + dim[0] / 2 - margin * 1.5,midY - margin /2,0);
		ObjRender_SetPosition		(obj3,320 + dim[0] / 2 - margin * 1.5,midY - margin /2,0);
		let alpha = 0;
		let rgb = [0,0,0];
		loop
		{
			let life1 = ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
			let life2 = ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_ACTIVE_STEP_TOTAL_LIFE);
			ObjRender_SetScaleXYZ(obj3,life2 / life1,1,0);
			if(alpha < 1){if(life2 > 0 && GetPlayerY > 20){alpha += 1/30;}}
			if(alpha > 0){if(life2 <= 0 || GetPlayerY <= 20){alpha -= 1/30;}}
			ObjRender_SetAlpha				(obj1,alpha*240);
			ObjRender_SetAlpha				(obj2,alpha*210);
			ObjRender_SetAlpha				(obj3,alpha*180);
			if(GetCommonData("bgMode",0) == 0)
			{
				if(rgb[0] < 120){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 120){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 120){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 120){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 120){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 120){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 1)
			{
				if(rgb[0] < 105){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 105){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 210){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 210){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 90){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 90){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 2)
			{
				if(rgb[0] < 240){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 240){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 60){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 60){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 3)
			{
				if(rgb[0] < 150){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 150){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 4)
			{
				if(rgb[0] < 210){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 210){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 135){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 135){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			ObjRender_SetColor				(obj2,rgb[0]-60,rgb[1]-60,rgb[2]-60);
			ObjRender_SetColor				(obj3,rgb[0],rgb[1],rgb[2]);
			yield;
		}
	}
	task lifenumber
	{
		let obj = ObjText_Create;
		ObjText_SetFontSize(obj,fontsize);
		ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
		ObjRender_SetScaleXYZ			(obj,1,1,1);
		Obj_SetRenderPriority			(obj,1);
		let alpha = 0;
		loop
		{
			let life1 = ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
			let life2 = ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_ACTIVE_STEP_TOTAL_LIFE);
			let text = IntToString(life2) ~ " / " ~ IntToString(life1);
			let digit = text;
			text = "";
			function GetNumSize(value)
			{
				if(value<=1)	{return 1;}
				else			{return truncate(log10(value))+1;}
			}
			let lp = GetNumSize(life2) + GetNumSize(life1) + 3;
            ascent(i in 0..lp)	{text = text ~ digit[i..i+1];if(i < lp){text = text ~ " ";}}
			ObjText_SetText					(obj,text);
			ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
			let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
			ObjRender_SetPosition			(obj,100-length[0]/2,midY-length[1]-1,0);
			if(life2 > 0 && alpha < 1 && life2 < life1/4*3){alpha += 1/30;}
			if(life2 <= 0 && alpha > 0)		{alpha -= 1/30;}
			ObjRender_SetAlpha				(obj,alpha*240);
			yield;
		}
	}
}
