#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["RaNGE Contest 16 by Zhan_Fox"]
#Background["./Functions/background.txt"]
#System["./System/Minoriko_System.txt"]
#Player["./MoF_Reimu/MoFReimuA.dnh","./MoF_Reimu/MoFReimuB.dnh","./MoF_Reimu/MoFReimuC.dnh"]

#include"./Functions/Enemy_Functions.txt"
#include"./Functions/Effects/Effects.txt"
#include"./Spells/LoadGraphics.txt"

	let music = ObjSound_Create;
	let dir = GetCurrentScriptDirectory();
	let end = dir ~ "./Sounds/se_enep01.wav";
	let setwind = dir ~ "./Sounds/se_wind.wav";
	ObjSound_Load(music,dir ~ "./Music/stage.mp3");

@Initialize{
	TStage();
	LoadGraphics;
	LoadSound(end);
	LoadSound(setwind);
	SetPlayerLife(15);
	SetCommonData("Music",0);
	CreateCommonDataArea("SpellFunctions");
	LoadCommonDataAreaA1("SpellFunctions");
	SetSkipModeKey(KEY_INVALID);
	StartItemScript(dir ~ "./Functions/item.txt");
	InstallFont(GetCurrentScriptDirectory ~ "./Fonts/Blogger Sans-Light.otf");}

@Event{
	alternative(GetEventType())
	case(EV_PAUSE_ENTER){
		ObjSound_SetRestartEnable(music,true);
		ObjSound_Stop(music);}
	case(EV_PAUSE_LEAVE){
		ObjSound_Play(music);
		ObjSound_SetRestartEnable(music,false);}}
		
@MainLoop{
	CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID),ObjMove_GetY(GetPlayerObjectID),70);
	yield;}
	
@Finalize{}

task TStage{
	let path = GetCurrentScriptDirectory ~ "./Spells/Shizuha Plural.txt";
	let idScript = LoadScriptInThread(path);
	StartScript(idScript);
	
	let path2 = GetCurrentScriptDirectory ~ "./Spells/Minoriko Plural.txt";
	let idScript2 = LoadScriptInThread(path2);
	StartScript(idScript2);

	FairyCheck;
	TAutoDeath;
	ObjSound_Play(music);
	ObjMove_SetX(GetPlayerObjectID,GetStgFrameWidth/2);
	ObjMove_SetY(GetPlayerObjectID,GetStgFrameHeight-150);

	TInfo;
	BlackBackground;
	ItemBorderText;
	ItemBorderLine;
	loop(240){yield;}
		TFairyA1;
	loop(480){yield;}
		TStageTitle;
	loop(330){yield;}

	loop(50){TFairyA2(40,-20,-2);
		TFairyA2(344,-20,2);
	loop(6){yield;}}
		TFairyA3(404,1);
	loop(90){yield;}
	loop(20){TFairyA2(40,-20,-2);
		TFairyA2(344,-20,2);
	loop(6){yield;}}
		TFairyA3(-20,-1);
	loop(90){yield;}
	loop(20){TFairyA2(40,-20,-2);
		TFairyA2(344,-20,2);
	loop(6){yield;}}
	loop(90){yield;}

	let x = 0;
	let y = 0;
	let u = 100;
	loop(2){
	ascent(i in 0..4){
		TFairyA4(-50+i*u+x,-50+y,30+i*u+x,80+y,150+i*u,-100);
		loop(30){yield;}}
	x+=20;y+=30;
		loop(30){yield;}}
	loop(210){yield;}

	SetCommonData("Start",1);
	while(!IsCloseScript(idScript)){yield;}
	MakeExplosionA2(192,192,"MAPLE",255,64,64);
	EndMidbossItems;
	PlaySE(end);
	loop(90){yield;}
	loop(2){
		TFairyA5(-50,80,50,90,150,-100,35);
		loop(40){yield;}
		TFairyA5(0,-50,120,50,450,100,35);
		loop(40){yield;}
		TFairyA5(433,50,320,80,150,-100,35);
		loop(90){yield;}
		TFairyB5(150,-50,100,100,50,-100,39);
		loop(20){yield;}
		TFairyB5(433,50,220,80,150,-100,39);
		loop(20){yield;}
		TFairyB5(0,-50,150,40,450,100,39);
		loop(60){yield;}}
	loop(60){yield;}
	TFairyA6(433,60,324,60,-100,0,0,1);
	TFairyA6(-50,60,60,60,484,0,180,-1);
	loop(300){yield;}
	TFairyA6(102,-50,152,80,-100,0,0,1);
	TFairyA6(282,-50,232,80,484,0,180,-1);
	TFairyA7();
	loop(120){yield;}
	SetCommonData("BG",1);
	loop(240){yield;}

	TFairyA8();
	loop(90){yield;}
	loop(30){
		let y = rand(50,90);
		TFairyA9(-50,y,484,y);
		TFairyA9(434,y,-100,y);
		loop(20){yield;}}

	loop(360){yield;}
	SetCommonData("BG2",1);
	SetCommonData("BG3",1);
	PlaySE(setwind);
	loop(120){yield;}
	loop(180){
		ObjSound_SetVolumeRate(music,ObjSound_GetVolumeRate(music)-5/30);
		yield;}
		waitForNoEnemy;
	loop(240){yield;}
	SetCommonData("Boss",1);
		while(!IsCloseScript(idScript2)){
			if(GetCommonData("Music",0) != 0){Obj_Delete(music);}
			yield;}

	WhiteBackground;
	CloseStgScene();}

function waitForNoEnemy{
	while(length(GetAllEnemyID) > 0){yield;}}

function BlackBackground{
	let object = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(object,black);
	Obj_SetRenderPriorityI(object, 100);
	ObjSprite2D_SetSourceRect(object, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(object, 0, 0, 640, 480);
	loop(60){yield;}
	let alpha = 0;
	loop(85){
	ObjRender_SetAlpha(object,255-alpha);
		alpha+=3; yield;}
	Obj_Delete(object);
	return;}

function WhiteBackground{
	let object = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(object,"");
	Obj_SetRenderPriorityI(object, 100);
	ObjSprite2D_SetSourceRect(object, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(object, 0, 0, 640, 480);
	let alpha = 255;
	loop(50){
	ObjRender_SetAlpha(object,alpha);
		alpha-=255/50; yield;}
	Obj_Delete(object);
	return;}

task TInfo{
	loop(180){yield;}
	let object = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(object,info);
	Obj_SetRenderPriorityI(object, 100);
	ObjSprite2D_SetSourceRect(object, 0, 0, 180, 60);
	ObjSprite2D_SetDestCenter(object);
	ObjRender_SetPosition(object, 350, 170, 0);
	let alpha = 0;
	loop(50){
	ObjRender_SetAlpha(object,alpha);
		alpha+=255/50; yield;}
	loop(90){yield;}
	loop(50){
	ObjRender_SetAlpha(object,alpha);
		alpha-=255/50; yield;}
	Obj_Delete(object);
	return;}

task TAutoDeath{
	while(GetPlayerState != STATE_END){yield;}
	loop(180){yield;}
	CloseStgScene;}

task ItemBorderText{
	let alpha = 0;
	let count = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,item);
	Obj_SetRenderPriorityI(obj, 30);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 180, 20);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetX(obj,GetStgFrameWidth/2);
	ObjRender_SetY(obj,120);
		loop(15){
			ObjRender_SetAlpha(obj,count);
			count+=10;yield;}
		loop(200){
			ObjRender_SetAlpha(obj,150+150*sin(alpha));
			alpha+=5;yield;}
		Obj_Delete(obj);
		return;}

task ItemBorderLine{
	let alpha = 0;
	let count = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,item);
	Obj_SetRenderPriorityI(obj, 30);
	ObjSprite2D_SetSourceRect(obj, 0, 25, 180, 40);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,2.5,0.5,0);
	ObjRender_SetX(obj,GetStgFrameWidth/2);
	ObjRender_SetY(obj,135);
		loop(15){
			ObjRender_SetAlpha(obj,count);
			count+=10;yield;}
		loop(200){
			ObjRender_SetAlpha(obj,150+150*sin(alpha));
			alpha+=5;yield;}
		Obj_Delete(obj);
		return;}

task TStageTitle{
	let scale = 0;
	let alpha = 0;

	let logo = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(logo,GetCurrentScriptDirectory ~ "./Images/logo.png");
	Obj_SetRenderPriorityI(logo,32);
	ObjSprite2D_SetSourceRect(logo,0,0,330,30);
	ObjSprite2D_SetDestCenter(logo);
	ObjRender_SetBlendType(logo,BLEND_ALPHA);
	ObjRender_SetPosition(logo,192,192,0);
	ObjRender_SetScaleXYZ(logo,1,0,0);
	ObjRender_SetAlpha(logo,255);

	let objtext1 = ObjText_Create();
	ObjText_SetFontType(objtext1,"Blogger Sans Light");
	ObjText_SetText(objtext1, "Harvest Crisis Season");
	ObjText_SetFontSize(objtext1, 30);
	ObjText_SetFontColorTop(objtext1, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext1, 255, 200, 255);
	Obj_SetRenderPriorityI(objtext1, 38);
	ObjRender_SetPosition(objtext1,45,172,0);
	ObjRender_SetAlpha(objtext1,0);

	let objtext2 = ObjText_Create();
	ObjText_SetFontType(objtext2,"Blogger Sans Light");
	ObjText_SetText(objtext2, "Final Stage");
	ObjText_SetFontSize(objtext2, 35);
	ObjText_SetFontColorTop(objtext2, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext2, 200, 200, 255);
	Obj_SetRenderPriorityI(objtext2, 33);
	ObjRender_SetPosition(objtext2,50,135,0);
	ObjRender_SetAlpha(objtext2,0);

	let objtext3 = ObjText_Create();
	ObjText_SetFontType(objtext3,"Blogger Sans Light");
	ObjText_SetText(objtext3, "Would she be able to be the [r] goddess of Gensokyo?");
	ObjText_SetFontSize(objtext3, 20);
	ObjText_SetFontColorTop(objtext3, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext3, 200, 200, 255);
	Obj_SetRenderPriorityI(objtext3, 33);
	ObjRender_SetPosition(objtext3,190,205,0);
	ObjRender_SetAlpha(objtext3,0);

	while(ObjRender_GetScaleY(logo)<1){
		ObjRender_SetScaleY(logo,scale);
		scale+=0.01;yield;}
	loop(85){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		ObjRender_SetAlpha(objtext1,alpha);
		ObjRender_SetAlpha(objtext2,alpha);
		ObjRender_SetAlpha(objtext3,alpha);
		alpha+=3;yield;}
	loop(120){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		yield;}
	while(ObjRender_GetScaleY(logo)>0){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		ObjRender_SetScaleY(logo,scale);
		ObjRender_SetAlpha(objtext1,alpha);
		ObjRender_SetAlpha(objtext2,alpha);
		ObjRender_SetAlpha(objtext3,alpha);
		alpha-=3;scale-=0.01;yield;}

	Obj_Delete(logo);
	Obj_Delete(objtext1);
	Obj_Delete(objtext2);
	Obj_Delete(objtext3);
	return;}