#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 4"]
#Text["Patchouli combines Metal and Sun."]
//#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.5;
let NUMBER = 8;
let speed=2;
let angle2 = 0;
let angle3 = 0;
let shot=0;
let allow=1;
let move=0;
let id = 34;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(6800)
	}
}

@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, 120, 10);
	
	let Stand = GetCurrentScriptDirectory ~ "/Patch_Stand.png";
	
	//cutin(BYAKUREN,Cutin,0,0,500, 600);
	CutinS2("NAZRIN", "Metal and Sun Sign \"Luminous Aether\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/Infinity_Star_Fractal_Stockies_by_zananeichan.jpg";
	let path2 = GetCurrentScriptDirectory ~ "/Tex_Stage5BG_04.png";
	
	BG(path1, path2, Patch);
}

@MainLoop{
	animation++;
	
	if(animation==20){
		declare;
		}
	
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	if(animation==130){
		cast2;
		Aether;
		Crosshair(Patch, DS_BEAM_WHITE);
		}
		
	if(animation>=120){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 0){

			death;
		}
		
		frame++;
		
		if(frame==10){
			//Wave3SE;
			loop(9){
				angle+=360/8;
				
				let obj1 = CreateLooseLaserA1(PatchX+40*cos(angle), PatchY+40*sin(angle), 2, angle+160, 180, 15, id, 60);
				ObjMove_SetAcceleration(obj1, 0.022);
				ObjMove_SetMaxSpeed(obj1, 5);
				Obj_SetRenderPriorityI(obj1,70);
				}
				
			if(allow==1){
				angle+=rand(4.2, 16);
				}
			else{
				angle-=rand(4.2, 16);
				}
			frame=0;
			loop(20){
				//yield;
				}
			
			}
			
		shot++;
		
		if(shot==430){
			ChargeSE;
			}
		if(shot==450){
			if(allow==1){
				allow=2;
				id=40;
				
				}
			else{
				allow=1;
				id = 34;
				}
			shot=0;
			}
			
		move++;
		
		if(move==200){
			let posx = rand(140, GetStgFrameWidth-140);
			let posy = rand(80, 140);
			
			if(posx>=PatchX){
				rightmove;
				}
			if(posx<=PatchX){
				leftmove;
				}
			
			ObjMove_SetDestAtFrame(Patch, posx, posy, 60);
			//move=0;
			}
		if(move==260){
			cast2;
			move=0;
			}
		}
	yield;
	}
	
task Aether {
	
	loop(150){
	
		ChargeSE;
	
		let px = GetPlayerX();
		let py = GetPlayerY();
		
		let StgX = rand(0, GetStgFrameWidth);
		let StgY = rand(0, GetStgFrameHeight);
	
		let obj = CreateShotA1(px+rand(100, 450)*cos(rand(0, 360)), py+rand(100, 450)*sin(rand(0, 360)), 0.05, rand(0, 360), DS_LIGHT_WHITE, 60);
		let obj1 = CreateShotA1(px+rand(100, 450)*cos(rand(0, 360)), py+rand(100, 450)*sin(rand(0, 360)), 0.05, rand(0, 360), DS_LIGHT_ORANGE, 60);
		
		ObjMove_SetAcceleration(obj, 0.001);
		ObjMove_SetMaxSpeed(obj, 0.1);
		ObjShot_SetSpellResist(obj, true);		
		ObjMove_SetAcceleration(obj1, 0.001);
		ObjMove_SetMaxSpeed(obj1, 0.1);
		ObjShot_SetSpellResist(obj1, true);
		
		}
		
	loop(60){
		yield;
		}
		
	loop{
		let StgX = rand(0, GetStgFrameWidth);
		let StgY = rand(0, GetStgFrameHeight);
		
		loop(1){
		let Stg1X = rand(0, GetStgFrameWidth);
		let Stg1Y = rand(0, GetStgFrameHeight);
		
		let StgX = rand(0, GetStgFrameWidth);
		let StgY = rand(0, GetStgFrameHeight);
		
		let obj = CreateShotA1(StgX, StgY, 0.05, rand(0, 360), DS_LIGHT_ORANGE, 60);
		
		ObjMove_SetAcceleration(obj, 0.001);
		ObjMove_SetMaxSpeed(obj, 0.1);
		//ObjShot_SetSpellResist(obj, true);
		
		let obj1 = CreateShotA1(Stg1X, Stg1Y, 0.05, rand(0, 360), DS_LIGHT_WHITE, 60);
		
		ObjMove_SetAcceleration(obj1, 0.001);
		ObjMove_SetMaxSpeed(obj1, 0.1);
		//ObjShot_SetSpellResist(obj1, true);
		}
		
		loop(20){
			yield;
			}
		}
	}

task death {

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