#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 1A"]
#Text["Patchouli combines Fire and Wood."]
//#Background["./fractal-art-08.jpg"]
#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 mod=270;

@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";
	
	//cutin(BYAKUREN,Cutin,0,0,500, 600);
	CutinS2("NAZRIN", "Wood and Water Sign \"Mysterious Fata Morgana\"", Stand, 240, 104, 104, "Normal");
	
	let imgcircle = GetCurrentScriptDirectory ~ "/e_ef_07.png";
	MCircle(Patch, imgcircle);
	
	Crosshair(Patch, DS_BEAM_WHITE);
	
	let path1 = GetCurrentScriptDirectory ~ "/elements-water.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==80){
		cast;
		}
	
	if(animation>=85){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

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

			death;
		}
		
		frame++;
		
		mod+=0.5;
		
		if(frame==12){
			let angle1= rand(0, 360);
			loop(8){
				angle1+=360/8;
				
				WindShot(PatchX-20, PatchY, 1.5, angle1, 0.3, DS_NEEDLE_BLUE, DS_SCALE_ORANGE, DS_SCALE_RED, 20);
				WindShot(PatchX+20, PatchY, 1.5, angle1, -0.3, DS_NEEDLE_SKY, DS_SCALE_ORANGE, DS_SCALE_RED, 20);
					
				}
			angle+=20;
			Variance+=20;
			frame=0;
			}		
			
		if(move==120){
			let pos = rand(100, GetStgFrameWidth-100);
			
			if(pos>=PatchX){
				rightmove;
				}
				
			if(pos<=PatchX){
				leftmove;
				}
				
			ObjMove_SetDestAtFrame(Patch, pos, rand(80, 120), 80);
			}
		
		if(move==220){
			cast2;
			move=0;
			}
		if(angle>=200){
			//angle+=rand(0, 360);
			//ANG=rand(-0.7, 0.7);
			NUMBER=10;
			//speed=rand(1.9, 2.5);
			allow++;
			move++;
			}
		}
	yield;
	}
	
task WindShot(x, y, speed, angle, ang, graphic1, graphic2, graphic3,delay){

	let obj = CreateShotA1(x, y, speed, angle, graphic1, delay);
	
	ObjMove_SetAngularVelocity(obj, ang);
	
	let graphic=0;
	
	let angle1=0;
	let explode=0;
	let ANGLE=mod;
		
	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)
			{
			Obj_Delete(obj);
			explode=1;
			graphic=graphic2;
			}
		if(ObjMove_GetX(obj) < 0)
			{
			Obj_Delete(obj);
			explode=1;
			graphic = graphic3;
			}
		yield;
		}
	if(explode==1){
		
		let obj2 = CreateShotA1(endX, endY, speed, ANGLE, graphic, 10);
		Obj_Delete(obj);
		}
	else{
		Obj_Delete(obj);
		}
	}
	
	

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