#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Final"]
#Text["A sequential elemental assault!"]
//#BGM["./LockedGirl.mp3"]
#Player["./Reimu/ReimuB.dnh"]

#include"./include.txt"
#include"./PhiloStone.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=11;
let id = 34;
let DELAY=-200;

let type=round(rand(1, 7));

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

@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", "Limitless Magic \"Seven Days of Magic\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	let imgfrost = GetCurrentScriptDirectory ~ "/p_ef_11.png";
	//MCircle(GetPlayerObjectID, imgfrost);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/Fractal_and_Spiders_Web_Wallpaper_amca5.jpg";
	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();
	
	if(animation==20){
		declare;
		}
		
	if(animation>=80){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

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

			death;
		}
		
		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 7000){
			DELAY=-120;
			}
		
		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 4000){
			ObjEnemy_SetDamageRate(Patch, 60, 0);
			DELAY=-60;
			}
		
		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 1000){
			DELAY=-20;
			}
		
		if(allow==1){
			frame++;
			}
		
		if(frame==60){			
			ElementalScramble1;
			
			frame=DELAY;
			}
			
		shot++;
		if(shot==120){
			let pos1 = rand(160, GetStgFrameWidth-160);
			
			if(pos1>=PatchX){
				rightmove;
				}
			
			if(pos1<=PatchX){
				leftmove;
				}
			ObjMove_SetDestAtFrame(Patch, pos1, rand(80, 110), 100);
			shot=-40;
			}
			
		if(move==10){
			ElementalScramble2;
			move=0;
			}
		move++;
			
		}
		
	yield;
	}
	
task ElementalScramble1{
	//let type=round(rand(1, 5));
	let current = type;
	
	WHOOOP;
	
	if(type==current){
		type = round(rand(1, 7));
		}
	if(type==current){
		type = round(rand(1, 7));
		}
	if(type==current){
		type = round(rand(1, 7));
		}
		
	
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	if(type==1){
		SunSpell(PatchX, PatchY, PatchX, 40, 20, 4, rand(0, 360), 20, 60, DS_BALL_L_ORANGE, DS_LIGHT_ORANGE, 20, 120);
		}
		
	if(type==2){
		WaterSpell(PatchX, PatchY, PatchX, 40, 20, 40, 70, DS_BALL_L_BLUE, DS_BEAM_BLUE, 20, 120, 120);
		}
	if(type==3){
		EarthSpell(PatchX, PatchY, PatchX, 40, 20, 2, rand(90+45, 45), 10, rand(-0.5, 0.5), DS_BALL_L_YELLOW, DS_BALL_BS_YELLOW, 20, 120);
		}
	if(type==4){
		WoodSpell(PatchX, PatchY, PatchX, 40, 20, 5, rand(85, 95), 20, DS_BALL_L_GREEN, DS_SCALE_GREEN, 20, 120);
		}
	if(type==5){
		MoonSpell(PatchX, PatchY, PatchX, 40, 20, 4, 90, 20, DS_BALL_L_SKY, DS_NEEDLE_SKY, 20, 120);
		}
	if(type==6){
		MetalSpell(PatchX, PatchY, PatchX, 180, 20, 3, 0, 10, DS_BALL_L_WHITE, DS_LIGHT_WHITE, 20, 120);
		}
	if(type==7){
		FireSpell(PatchX, PatchY, PatchX-80, 120, 20, 1.5, rand(0, 360), 30, 0.4, DS_BALL_L_RED, DS_LIGHT_RED, 20, 120);
		FireSpell(PatchX, PatchY, PatchX+80, 120, 20, 1.5, rand(0, 360), 30, -0.4, DS_BALL_L_RED, DS_LIGHT_RED, 20, 120);
		}
	}
	
task ElementalScramble2{
	//let type=round(rand(1, 5));
	let current = type;
	
	WHOOOP;
	
	if(type==current){
		type = round(rand(1, 7));
		}
	if(type==current){
		type = round(rand(1, 7));
		}
		
	
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
		SunSpell(PatchX, PatchY, PatchX, 40, 20, 4, rand(0, 360), 20, 60, DS_BALL_L_ORANGE, DS_LIGHT_ORANGE, 20, 120);


		WaterSpell(PatchX, PatchY, PatchX, 40, 20, 40, 70, DS_BALL_L_BLUE, DS_BEAM_BLUE, 20, 120, 120);

		
		EarthSpell(PatchX, PatchY, PatchX, 40, 20, 2, rand(90+45, 45), 10, rand(-0.5, 0.5), DS_BALL_L_YELLOW, DS_BALL_BS_YELLOW, 20, 120);

		
		WoodSpell(PatchX, PatchY, PatchX, 40, 20, 5, rand(85, 95), 20, DS_BALL_L_GREEN, DS_SCALE_GREEN, 20, 120);

		
		MoonSpell(PatchX, PatchY, PatchX, 40, 20, 4, 90, 20, DS_BALL_L_SKY, DS_NEEDLE_SKY, 20, 120);

		
		MetalSpell(PatchX, PatchY, PatchX, 180, 20, 3, 0, 10, DS_BALL_L_WHITE, DS_LIGHT_WHITE, 20, 120);

		
		FireSpell(PatchX, PatchY, PatchX-80, PatchY, 20, 1.5, rand(0, 360), 30, 0.4, DS_BALL_L_RED, DS_LIGHT_RED, 20, 120);
		FireSpell(PatchX, PatchY, PatchX+80, PatchY, 20, 1.5, rand(0, 360), 30, -0.4, DS_BALL_L_RED, DS_LIGHT_RED, 20, 120);

	}
	
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;
	
	}