#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 3"]
#Text["Patchouli combines Moon and Water."]
//#BGM["script/practice/resources/area2_boss.wav"]
#Player["./Reimu/ReimuB.dnh"]

#include"./include.txt"

let Patch;
let Koa;
let Koa1;
let frame=0;
let animation=0;
let angle=0;
let Variance = 5;
let ANG= 0;
let NUMBER = 8;
let speed=2;
let angle1 = 0;
let angle2 = 90;
let angle3 = 0;
let shot=0;
let allow=0;
let move=0;

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

@Initialize
{	
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	
	Patch = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Patch);
	
	Koa = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(Koa);
	Koa1 = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(Koa1);
	
	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, 100, 30);
	ObjMove_SetDestAtFrame(Koa, cx, GetStgFrameHeight/2, 30);
	ObjMove_SetDestAtFrame(Koa1, cx, GetStgFrameHeight/2, 30);

	let Stand = GetCurrentScriptDirectory ~ "/Patch_Stand.png";
	
	CutinS2("NAZRIN", "Moon and Water Sign \"Sea of Tranquility\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/The_Magic_Circle_by_08Spectre80.png";
	let path2 = GetCurrentScriptDirectory ~ "/Tex_Stage5BG_04.png";
	
	BG(path1, path2, Patch);
}

@MainLoop{
	animation++;
	
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	let px = GetPlayerX();
	let py = GetPlayerY();
	
	let Player = atan2(py - PatchY, px - PatchX);
	
	let FX = ObjMove_GetX(Koa);
	let FY = ObjMove_GetY(Koa);
	let F1X = ObjMove_GetX(Koa1);
	let F1Y = ObjMove_GetY(Koa1);
	
	if(animation==34){
		declare;
		}
		
	if(animation==70){
		cast3;
		Crosshair(Patch, DS_BEAM_WHITE);
		}
	
	if(animation>=60){
		
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

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

			death;
		}
	
		ANG++;
	
		if(ANG==100){
		
			Shot3SE;
			
			let angle2 = Player;
			
			loop(10){
				angle2+=360/10;
				Moonshot(PatchX, PatchY, angle2, DS_BALL_L_SKY, DS_LIGHT_SKY, 0);
				}
			
			//ANG=0;
			}
			
		if(ANG==150){
		
			Shot3SE;
			
			let angle3 = 45;
			
			loop(12){
				angle3+=360/12;
				Moonshot(PatchX, PatchY, angle3, DS_BALL_L_SKY, DS_LIGHT_SKY, 0.6);
				}
			
			//ANG=0;
			}
			
		if(ANG==200){
		
			Shot3SE;
			
			let angle3 = 45;
			
			loop(12){
				angle3+=360/12;
				Moonshot(PatchX, PatchY, angle3, DS_BALL_L_SKY, DS_LIGHT_SKY, -0.6);
				}
			
			//ANG=0;
			}
			
		if(ANG==200){
			ChargeSE;
			}
			
		if(ANG==300){
			Shot3SE;
			let angle3 = 90;
			
			loop(20){
				angle3+=360/20;
				Moonshot(PatchX, PatchY, angle3, DS_BALL_L_SKY, DS_LIGHT_SKY, 0);
				}
			
			//ANG=0;
			}
			
		if(ANG==310){
			let pos1 = rand(60, GetStgFrameWidth-60);
			
			if(pos1>=PatchX){
				leftmove;
				}
			
			if(pos1<=PatchX){
				rightmove;
				}
			ObjMove_SetDestAtFrame(Patch, pos1, rand(80, 110), 70);
			ANG=0;
			}
			
		
			
		if(frame==30){
			
			CreateShotA1(FX, FY, 1, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 1, angle1, DS_LIGHT_R_WHITE, 20);
			
			CreateShotA1(FX, FY, 2, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 2, angle1, DS_LIGHT_R_WHITE, 20);
			}
		if(frame==35){
			
			CreateShotA1(FX, FY, 1, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 1, angle1, DS_LIGHT_R_WHITE, 20);
			
			CreateShotA1(FX, FY, 2, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 2, angle1, DS_LIGHT_R_WHITE, 20);
			
			}
		if(frame==40){
			
			CreateShotA1(FX, FY, 1, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 1, angle1, DS_LIGHT_R_WHITE, 20);
			
			CreateShotA1(FX, FY, 2, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 2, angle1, DS_LIGHT_R_WHITE, 20);
		
			}
		if(frame==45){
			
			CreateShotA1(FX, FY, 1, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 1, angle1, DS_LIGHT_R_WHITE, 20);
			
			CreateShotA1(FX, FY, 2, angle, DS_LIGHT_WHITE, 20);
			CreateShotA1(F1X, F1Y, 2, angle1, DS_LIGHT_R_WHITE, 20);
			frame=0;
			}
			
				
		
	
		if(allow==0){
			Sea;
			}
		
		allow++;
	
		frame++;
		
		shot++;
		
		
			
		if(shot==20){
			WHOOOP;
			move=1;
			}
			
		if(shot==220){
			move=2;
			}
			
		if(shot==440){
			move=3;
			}
		
		if(shot==640){
			move=4;
			}
			
		if(shot==860){
			move=1;
			shot=21;
			}
		}
		
	yield;
	}
	
task Sea {
	move = 0;
	loop{
		
		if(move==0){
			ObjMove_SetDestAtFrame(Koa, 0, 0, 3);
			ObjMove_SetDestAtFrame(Koa1, GetStgFrameWidth, GetStgFrameHeight, 3);
			
			angle = 90;
			angle1 = 270;
			}
			
		if(move==1){
			ObjMove_SetDestAtSpeed(Koa, GetStgFrameWidth, 0, 2);
			ObjMove_SetDestAtSpeed(Koa1, 0, GetStgFrameHeight, 2);
			
			angle = 90;
			angle1 = 270;
			}
			
		if(move==2){
			ObjMove_SetDestAtSpeed(Koa, GetStgFrameWidth, GetStgFrameHeight, 2);
			ObjMove_SetDestAtSpeed(Koa1, 0, 0, 2);
			
			angle = 180;
			angle1 = 0;
			}
		
		if(move==3){
			ObjMove_SetDestAtSpeed(Koa, 0, GetStgFrameHeight, 2);
			ObjMove_SetDestAtSpeed(Koa1, GetStgFrameWidth, 0, 2);
		
			angle = 270;
			angle1 = 90;
			}
			
		if(move==4){
			ObjMove_SetDestAtSpeed(Koa, 0, 0, 2);
			ObjMove_SetDestAtSpeed(Koa1, GetStgFrameWidth, GetStgFrameHeight, 2);
			
			angle = 0;
			angle1 = 180;
			}
		
		yield;
		}
	}
	
task Moonshot(x, y, angle, graphic1, graphic2, angular) {

	let obj1 = CreateShotA1(x, y, 3, angle, graphic1, 20);
	ObjMove_SetAngularVelocity(obj1, angular);
				
	while(!Obj_IsDeleted(obj1)){
		let obj2 = CreateShotA1(ObjMove_GetX(obj1), ObjMove_GetY(obj1), 0, 0, graphic2, 30);
		ObjShot_SetDeleteFrame(obj2, 0);
					
		loop(5){
			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;
	
	}