#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 2"]
#Text["Patchouli steals Marisa's shtick and combines Moon and Earth"]
//#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=0;
let move=0;
let id = 34;

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

@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";
	
	CutinS2("NAZRIN", "Earth and Moon \"Shoot the Moon -High Level-\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/stardome.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);
	
	let px = GetPlayerX();
	let py = GetPlayerY();
	
	let Player = atan2(py - PatchY, px - PatchX);
	
	if(animation==40){
	
		let angle = 45;
	
		loop(8){
			angle+=360/4;
			ShootA1(PatchX, PatchY, 2, angle, DS_STAR_M_BLUE, DS_BEAM_BLUE, 50, 10, 0);
			}
		
		}
		
	if(animation>=200){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

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

			death;
		}
		
		frame++;
			
		if(frame==60){
			cast3;
			ShootSweep;
			ChargeSE;
			}
			
		if(frame==760){
			let angle3 = Player;
		
			

			frame=700;
			}
			
		allow++;
			
		if(allow==350){
			ShootSpread;
			}
			
		
		}
		
	yield;
	}
	
task ShootA1(x, y, speed, angle, graphic, graphic1, width, delay, delay1) {

	let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
	
	let angle1=0;
	let explode=0;
		
	ObjShot_SetAutoDelete(obj, false);
	ObjShot_SetSpellResist(obj, true);
		
	let endX = 0;
	let endY = 0;
		
	while(!Obj_IsDeleted(obj))
	{
		
		endX = ObjMove_GetX(obj);
		endY = ObjMove_GetY(obj);
		
		let obj10 = CreateShotA1(endX, endY, 0, 0, graphic, 10);
		ObjShot_SetDeleteFrame(obj10, 0);
		
		if(ObjMove_GetX(obj) > GetStgFrameWidth-10)
			{
			ObjMove_SetSpeed(obj, speed+2);
			ObjMove_SetAngle(obj, 90+45);
			Wave3SE;
			explode=1;
			}
		if(ObjMove_GetX(obj) < 10)
			{
			ObjMove_SetSpeed(obj, speed+2);
			ObjMove_SetAngle(obj, 45);
			Wave3SE;
			explode=1;
			}
		if(ObjMove_GetY(obj) < 5)
			{
			ObjMove_SetSpeed(obj, speed+2);
			ObjMove_SetAngle(obj, 90);
			Wave3SE;
			explode=1;
			}
		if(ObjMove_GetY(obj) > GetStgFrameHeight){
			Obj_Delete(obj);
			angle1=270;
			explode=1;
			}
		yield;
		}
	if(explode==1){
		let obj2 = CreateStraightLaserA1(endX, endY+100, angle1, 600, width, 40, graphic1, delay1);
		
		loop(10){
			angle1+=360/10;
			let obj50 = CreateShotA1(endX, endY, 0, angle1, graphic, 80);
			
			ObjShot_SetDeleteFrame(obj50, 0);
			
			}
			
		Obj_Delete(obj);
		}
	else{
		Obj_Delete(obj);
		}
	}
	
task ShootSweep {

	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	let angle2=0;
	
	loop{
		//Wave3SE;
		
		ShootA1(PatchX, PatchY, 2, angle2, DS_STAR_M_GREEN, DS_BEAM_GREEN, 30, 10, 40);
		//ShootA1(PatchX, PatchY, 1, angle2+180, DS_STAR_M_GREEN, DS_BEAM_GREEN, 30, 10, 40);
		
		angle2+=35.1;
		
		loop(30){
			yield;
			}
		}
	}
	
task ShootSpread {

	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	let angle4 = 90;
	
	loop{
		
		loop(20){
			angle4+=360/20;
			let obj6 = CreateShotA1(PatchX+40*cos(angle4), PatchY+20*sin(angle4), 2, angle4+45, DS_STAR_S_WHITE, 20);
			ObjMove_SetAngularVelocity(obj6, 0.53);
			}
			
		angle4+=25.1;
		
		loop(30){
			yield;
			}
		loop(20){
			angle4+=360/20;
			let obj6 = CreateShotA1(PatchX+20*cos(angle4), PatchY+40*sin(angle4), 2, angle4-45, DS_STAR_S_R_WHITE, 20);
			ObjMove_SetAngularVelocity(obj6, -0.53);
			}
			
		loop(30){
			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;
	
	}