#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 6"]
#Text["Patchouli destroys the elements around her in preperation for her grand finale!"]
//#BGM["script/practice/resources/area2_boss.wav"]
#Player["./Reimu/ReimuB.dnh"]

#include"./include.txt"

let Patch;
let frame=0;
let animation=0;
let angle=0;
let Variance = 5;
let ANG= 0;
let NUMBER = 8;
let speed=2;
let angle2 = 0;
let angle3 = 0;
let shot=40;
let allow=1;
let move=0;
let id = 34;
let count = 2;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(1)
		}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50);
		}
}

@Initialize
{	

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	
	Patch = ObjEnemy_Create(OBJ_ENEMY_BOSS);

	//Patch_Regist(objfamiliar1);

	ObjEnemy_Regist(Patch);

	let imgExRumia = GetCurrentScriptDirectory ~ "/dot_patchouli.png"; //file path to enemy image

	ObjPrim_SetTexture(Patch, imgExRumia); //Setting the above image file as a texture to the enemy object(Patch)

	ObjSprite2D_SetSourceRect(Patch, 0, 0, 128, 128); //Setting the rectangle coordinates in the enemy image to use(Left, Top, Right, Bottom).

	ObjSprite2D_SetDestCenter(Patch); //Positioning the center of the rectangle(w/texture) at (0, 0) on the stage(top left corner).

	//Moving to the coordinate (cx, 60) in 60 frames

	let cx = GetStgFrameWidth() / 2;

	ObjMove_SetDestAtFrame(Patch, cx, GetStgFrameHeight/2, 10);
	
	let Stand = GetCurrentScriptDirectory ~ "/Patch_Stand.png";
	
	//cutin(BYAKUREN,Cutin,0,0,500, 600);
	CutinS2("NAZRIN", "Elementless \"The Yawning Void\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/1snx2x.jpg";
	let path2 = GetCurrentScriptDirectory ~ "/Tex_Stage5BG_04.png";
	
	BG(path1, path2, Patch);
}

@MainLoop{

	ObjEnemy_SetDamageRate(Patch, 0, 0);
	
	animation++;
	
	if(animation==20){
		declare;
		}
		
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
		
	if(animation==40){
		//CreateShotA1(PatchX, PatchY, 0, 0, DS_LIGHT_R_WHITE, 1200);
		}
	
	if(animation>=130){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

		frame++;
		
		if(frame==20){
			//Shot5SE;
			
			loop(count){
				angle+=360/count;
				
				let obj = CreateShotA1(PatchX+30*cos(angle), PatchY+30*sin(angle), 1.5, angle, DS_LIGHT_R_WHITE, 10);
				let obj2 = CreateShotA1(PatchX+30*cos(angle), PatchY+30*sin(angle), 1.5, -angle, DS_LIGHT_R_WHITE, 10);
				ObjShot_SetAutoDelete(obj, false);
				ObjShot_SetAutoDelete(obj2, false);
				
				ObjShot_SetSpellResist(obj, true);
				ObjShot_SetSpellResist(obj2, true);
				
				//ObjMove_SetAcceleration(obj, ANG);
				//ObjMove_SetMaxSpeed(obj, 0.4);
				
				ObjMove_AddPatternA3(obj, 230, 0.5, angle+rand(160, 200), ANG, 0, 0.4, DS_BALL_M_R_WHITE);
				ObjMove_AddPatternA3(obj2, 230, 0.5, angle+rand(160, 200), ANG, 0, 0.4, DS_BALL_M_R_WHITE);
				
				}
			
				
			if(allow==1){
				angle+=10.1;
				}
			else if(allow==2){
				angle+=rand(2, 20);
				}
			else{
				count=0;
				}
			frame=0;
			}
			
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)==22){
			ChargeSE;
			}
			
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=20){
			allow=2;
			count = 6;
			ANG=-0.05;
			}
			
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)==8){
			frame=1000;
			DeleteShotAll(TYPE_CHILD, TYPE_FADE);
			}
			
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)==7){
			allow=0;
			shot=0;
			ChargeSE;
			}
		
		shot++;
			
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)==0){
			death;
			}
			
		if(shot==10){
			let angle5=rand(0, 360);
			Shot4SE;
			loop(300){
				angle5+=360/300;
				let obj50 = CreateShotA1(PatchX, PatchY, 1, angle5, DS_BALL_M_R_WHITE, 28);
				let obj51 = CreateShotA1(PatchX, PatchY, 0.9, angle5, DS_BALL_M_R_WHITE, 28);
				let obj52 = CreateShotA1(PatchX, PatchY, 0.8, angle5, DS_BALL_M_R_WHITE, 28);
				let obj53 = CreateShotA1(PatchX, PatchY, 0.7, angle5, DS_BALL_M_R_WHITE, 28);
				let obj54 = CreateShotA1(PatchX, PatchY, 0.6, angle5, DS_BALL_M_R_WHITE, 28);
				let obj55 = CreateShotA1(PatchX, PatchY, 0.5, angle5, DS_BALL_M_R_WHITE, 28);
				let obj56 = CreateShotA1(PatchX, PatchY, 0.4, angle5, DS_BALL_M_R_WHITE, 28);
				let obj57 = CreateShotA1(PatchX, PatchY, 0.3, angle5, DS_BALL_M_R_WHITE, 28);
				ObjMove_SetAcceleration(obj50, -0.0012);
				ObjMove_SetAcceleration(obj51, -0.0012);
				ObjMove_SetAcceleration(obj52, -0.0012);
				ObjMove_SetAcceleration(obj53, -0.0012);
				ObjMove_SetAcceleration(obj54, -0.0012);
				ObjMove_SetAcceleration(obj55, -0.0012);
				ObjMove_SetAcceleration(obj56, -0.0012);
				ObjMove_SetAcceleration(obj57, -0.0012);
				}
			}
			
		
		}
		
	yield;
	}
	
task death {

	Obj_Delete(Patch);
	//Obj_Delete(Koa);
	//Obj_Delete(Koa1);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	loop(100){	
		DeleteShotAll(TYPE_CHILD, TYPE_IMMEDIATE);
		
		yield;
		}
		
	Obj_Delete(Patch);
		
	CloseScript(GetOwnScriptID());
	
	return;
	
	}