#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Hina Stage"]

#System["./system/Default_System.txt"]

#Background["./system/Default_Background.txt"]
	let csd			= GetCurrentScriptDirectory;
	SetAutoDeleteObject(true);
	Stage1;
	let directory="";
	let pause=false;
	let objSound=ObjSound_Create;
let time=0;
let volume=0;
let bgmBoss= GetCurrentScriptDirectory ~ "img/bgmtitle.png";
let bgmIntro= GetCurrentScriptDirectory ~ "img/bgmtitle2.png";

#include "./enemy_functions.txt"

	
@Initialize{
PlayMusic(GetCurrentScriptDirectory~"/system/bgm/hina.mp3", 0, 170);
}
@MainLoop{
yield;}

@Event{
alternative (GetEventType)
	case(EV_PAUSE_ENTER){
	ObjSound_Stop(objSound);
	}
	case(EV_PAUSE_LEAVE){
	PlayMusic(directory, time/60, 420);
	ObjSound_SetLoopEnable(objSound, true);
	}
	case(EV_USER+11){
	Obj_Delete(objSound);
	}
}

task PlayMusic(dir, start, end){
ObjSound_Load(objSound, dir);
directory=dir;
ObjSound_SetSoundDivision(objSound, SOUND_BGM);
ObjSound_SetRestartEnable(objSound, true);
ObjSound_SetVolumeRate(objSound,75);
ObjSound_SetLoopEnable(objSound,true);
ObjSound_SetLoopTime(objSound,start,end);
ObjSound_Play(objSound);
while(!Obj_IsDeleted(GetEnemyBossSceneObjectID)){
yield;}

}

task Stage1{

let x=0; let y=0; let pow=false; let t=0;
yield;
ObjSound_Stop(objSound);
BGM_Title(bgmIntro);
PlayMusic(GetCurrentScriptDirectory~"/system/bgm/stage.mp3", 0, 400);
wait(100);
StageIntro;
wait(600);

BGM_Title(bgmBoss);
ObjSound_Stop(objSound);
PlayMusic(GetCurrentScriptDirectory~"/system/bgm/hina.mp3", 25, 120);
let plural=LoadScript(csd~"plural.txt");
StartScript(plural);
while(!Obj_IsDeleted(GetEnemyBossSceneObjectID)&&GetPlayerLife>=0){
yield;}
if (GetPlayerLife<0){SetCommonData("gameover", true);}
else{SetCommonData("gameover", false);}
TEnd;
}


task TEnd{
loop(360){yield;}
CloseStgScene;
loop(600){yield;}
NotifyEventAll(EV_USER+11, 0);

NotifyEventAll(EV_USER+5, 0);
loop(360){yield;}
CloseScript(GetOwnScriptID);
}


task BGM_Title(img){
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(objText, img);
	ObjSprite2D_SetSourceRect(objText, 0, 0, 533, 59);
	
	ObjSprite2D_SetDestCenter(objText);
  	Obj_SetRenderPriority(objText, 0.7);
  	ObjRender_SetScaleXYZ(objText, 0.5, 0.5, 0);
  	ObjRender_SetX(objText, GetClipMaxX + 50);
  	ObjRender_SetY(objText, GetClipMaxY - 8 );

  	let x =  GetClipMaxX + 60;
  	while(x > GetClipMaxX - (510 - 0)/2){
  		ObjRender_SetX(objText, x);
  		x -= 8;
  		yield;
  	}
  	wait(180);

  	let a = 255;
  	while(a > 0){
  		ObjRender_SetAlpha(objText, a);
  		a -= 4;
  		yield;
  	}
  	Obj_Delete(objText);
}
task StageIntro{
	let stage = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory ~ "img/stagetitle.png";
	ObjPrim_SetTexture(stage, img);
	ObjSprite2D_SetSourceRect(stage, 0, 0, 412, 35);
	ObjSprite2D_SetDestCenter(stage);
	ObjRender_SetPosition(stage, GetCenterX, GetCenterY - 116, 0);
	ObjRender_SetAlpha(stage, 0);
	ObjRender_SetScaleXYZ(stage, 0.6, 0.6, 0.6);
	Obj_SetRenderPriority(stage, 0.6);

	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, img);
	ObjSprite2D_SetSourceRect(pic, 0, 94, 210, 288);				
	ObjSprite2D_SetDestCenter(pic);
	ObjRender_SetPosition(pic, GetCenterX, GetCenterY - 75, 0);
	ObjRender_SetAlpha(pic, 0);
	ObjRender_SetScaleXYZ(pic, 0.6, 0.6, 0.6);
	ObjRender_SetAngleZ(pic, -90);
	Obj_SetRenderPriority(pic, 0.59);

	let japtext = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(japtext, img);
	ObjSprite2D_SetSourceRect(japtext, 0, 39, 415, 65);			
	ObjSprite2D_SetDestCenter(japtext);
	ObjRender_SetScaleXYZ(japtext, 0.7, 0.7, 0.7);
	ObjRender_SetPosition(japtext, GetCenterX, GetCenterY - 92, 0);
	ObjRender_SetAlpha(japtext, 0);
	Obj_SetRenderPriority(japtext, 0.6);

	let engtext = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(engtext, img);
	ObjSprite2D_SetSourceRect(engtext, 0, 65, 160, 92 );
	ObjSprite2D_SetDestCenter(engtext);
	ObjRender_SetScaleXYZ(engtext, 0.7, 0.7, 0.7);
	ObjRender_SetPosition(engtext, GetCenterX + 70, GetCenterY - 70, 0);
	ObjRender_SetAlpha(engtext, 0);
	Obj_SetRenderPriority(engtext, 0.6);

	let fadeout = false;
	renderalltexts(stage);
	renderpic;
	wait(30);
	renderalltexts(japtext);
	wait(100);
	renderalltexts(engtext);
	wait(180);
	fadeout = true;

	task renderalltexts(object){
		let alpha = 0;
		while(alpha < 255){
			ObjRender_SetAlpha(object, alpha);
			alpha += 2;
			yield;
		}
		while(!fadeout){
			yield;
		}
		while(alpha > 0){
			ObjRender_SetAlpha(object, alpha);
			alpha -= 4;
			yield;
		}
		Obj_Delete(object);
	}

	task renderpic{
		let alpha = 0;
		let x = GetCenterX;
		let z = -90;
		while(alpha < 255){
			ObjRender_SetAlpha(pic, alpha);
			ObjRender_SetX(pic, x);
			ObjRender_SetAngleZ(pic, z);
			alpha += 2;
			x += 48/128;
			z += 90/128;
			yield;
		}
		while(!fadeout){
			yield;
		}
		while(alpha > 0){
			ObjRender_SetAlpha(pic, alpha);
			alpha -= 4;
			yield;
		}
		Obj_Delete(pic);
		Obj_Delete(stage);
	}
}


function GetClipMaxX(){
	return GetCommonData("STGFrame_R", 512);

}

function GetClipMaxY(){
	return GetCommonData("STGFrame_D", 464);
}

function GetCenterX() {
	let calc = GetStgFrameWidth / 2;
	return calc;
}
function GetCenterY() {
	let calc = GetStgFrameHeight / 2;
	return calc;
}
	
