#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Practice-Stage"]
#Background["./../Functions/background.txt"]
#System["./../System/Miyako_System.txt"]

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

@Initialize{
	SetAreaCommonData("LIFE","Number",0);
	SetAreaCommonData("SPELL","Number",0);
	SetAreaCommonData("COINS","Number",0);
	SetCommonData("soundScriptID",GetOwnScriptID);
	SetCommonData("Main Stage Script ID", GetOwnScriptID());
	
	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	InstallFont(GetCurrentScriptDirectory ~ "./Fonts/Blogger Sans-Light.otf");
	StartItemScript(GetCurrentScriptDirectory ~ "./../Functions/item.txt");
	SetPlayerLife(0);
	SetPlayerSpell(0);
	TStage;}

@Event{
	alternative(GetEventType())
	case(EV_PAUSE_ENTER){
		StopMusic(GetAreaCommonData("MUSIC","Song",0));}
	case(EV_PAUSE_LEAVE){
		musicRestartEnable(GetAreaCommonData("MUSIC","Song",0),1);
		PlayMusic(GetAreaCommonData("MUSIC","Song",0));}
	if(GetEventType() == EV_USER+100) {
		let tempArgument = GetEventArgument(0);
		let isRestartEnable;
		if(tempArgument[1] == 0) { isRestartEnable = false; } 
		if(tempArgument[1] == 1) { isRestartEnable = true; } 
		ObjSound_SetRestartEnable(musicARRAY[tempArgument[0]],isRestartEnable);}
	if(GetEventType() == EV_USER+104){
		ObjSound_Play(musicARRAY[GetEventArgument(0)] );}
	if(GetEventType() == EV_USER+1040) {
		ObjSound_SetFade(GetEventArgument(0),0); 
		ObjSound_Stop(musicARRAY[GetEventArgument(0)] );}}

@MainLoop{
	CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID),ObjMove_GetY(GetPlayerObjectID),70);
	yield;}
	
@Finalize{}

task TStage{
	let path = GetCurrentScriptDirectory ~ "Practice_Plural.txt";
	let idScript = LoadScriptInThread(path);
	StartScript(idScript);
	if(GetCommonData("Spell",0) == 0 || GetCommonData("Spell",0) == 1 || GetCommonData("Spell",0) == 2 || 
	GetCommonData("Spell",0) == 13 || GetCommonData("Spell",0) == 14 || GetCommonData("Spell",0) == 15)
	{PlayMusic(1);}
	else{PlayMusic(2);
	SetCommonData("MiyakoBG",2);}
	
	FairyCheck;
	TAutoDeath;
	ObjMove_SetX(GetPlayerObjectID,GetStgFrameWidth/2);
	ObjMove_SetY(GetPlayerObjectID,GetStgFrameHeight-150);
	BlackBackground;
	SetCommonData("StartPr",1);
		while(!IsCloseScript(idScript)){
		if(GetCommonData("Item",0) == 1){
			let radius = 5;
			let shotarray = [];
			let bossarray = GetEnemyBossObjectID();
			let objScene = GetEnemyBossSceneObjectID();
			ascent(i in 0..length(bossarray)){
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +
			ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
				LifeP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
			else{SpellP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
			ascent(count in 0..33){
				let r = 100;
				PowerS(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));
				Point(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));}}
			loop(12){
			shotarray = GetShotIdInCircleA2( 192, 210, radius, TARGET_ENEMY);
			ascent( i in 0..length(shotarray)){
				if(ObjMove_GetX(shotarray[i])>0 && ObjMove_GetX(shotarray[i])<384 && ObjMove_GetY(shotarray[i])>0 && ObjMove_GetY(shotarray[i])<448){
					Item(ObjMove_GetX(shotarray[i]),ObjMove_GetY(shotarray[i]));}
				DeleteShotInCircle(TYPE_SHOT,TYPE_IMMEDIATE, 192, 210, radius/1.2);}
				radius+=20;yield;}
				loop(30){yield;}
				let r = 60;
				loop(40){
					CollectItemsInCircle(GetPlayerX, GetPlayerY, r);
					r+=12; yield;}
			SetCommonData("Item",0);}
		yield;}

	if(GetCommonData("Spell",0) == 0 || GetCommonData("Spell",0) == 1 || GetCommonData("Spell",0) == 2 || 
	GetCommonData("Spell",0) == 13 || GetCommonData("Spell",0) == 14 || GetCommonData("Spell",0) == 15)
	{MakeExplosionA2(192,192,"HEART",255,64,64);}
	else{MakeExplosionA2(192,192,"SAKURA",255,64,64);}

	loop(60){yield;}
	StopMusic(GetAreaCommonData("MUSIC","Song",0));
	CloseStgScene();}

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);
	let alpha = 0;
	loop(30){
	ObjRender_SetAlpha(object,255-alpha);
		alpha+=255/30; yield;}
	Obj_Delete(object);
	return;}

task TAutoDeath{
	while(GetPlayerState != STATE_END){yield;}
	loop(30){yield;}
	StopMusic(GetAreaCommonData("MUSIC","Song",0));
	yield;
	CloseStgScene;}