#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 4B"]
#Text["Patchouli combines Fire 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=80;
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", "Fire and Sun Sign \"Sky of Scarlet Mist\"", 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++;
	
	let PatchX = ObjMove_GetX(Patch);
	let PatchY = ObjMove_GetY(Patch);
	
	if(animation==20){
		declare;
		}
	
	if(animation==130){
		cast2;
		Mist;
		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++;
		allow++;
		
		if(frame==120){
			Wheel(PatchX, PatchY, 2.2, 0.9, angle3, 2, DS_BALL_M_RED, DS_BILL_RED, 10);
			Wheel(PatchX, PatchY, 2.2, 0.9, 180-angle3, 1, DS_BALL_M_RED, DS_BILL_RED, 10);
			angle3+=25;
			
			if(angle3>=50){
				angle3=-20;
				allow=-20;
				}
			else{
				frame=-80;
				}
			}
			
		if(allow==40){
			let pos = 0;
			LaserSE;
			loop(10){
				let obj = CreateStraightLaserA1(pos, 0, 90, 460, 60, 80, DS_BEAM_RED, 60);
				ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
				ObjRender_SetAlpha(obj, 255);
				pos+=GetStgFrameWidth/6;
				}
			frame=0;
			}
				

		
		if(shot==50){
			CreateShotA1(rand(0, GetStgFrameWidth), 0, 1.8, 90, DS_BALL_L_RED, 30);
			shot=0;
			}
			
		shot++;
			
		move++;
		
		}
	yield;
	}
	
task Mist {
	
	ChargeSE;
		
	loop{
	
		let PatchX = ObjMove_GetX(Patch);
		let PatchY = ObjMove_GetY(Patch);
	
		
		
		let angle = rand(0, 360);
		
		loop(280){
			angle+=360/300;
			let obj = CreateShotA1(PatchX+rand(100, 500)*cos(angle), PatchY+rand(100, 500)*sin(angle), 0, 0, DS_BALL_M_R_RED, rand(100, 160));
			ObjShot_SetDeleteFrame(obj, 0);
			Obj_SetRenderPriorityI(obj,75);
			}		
		
		loop(27){
		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;
	
	}
	
task Wheel(x, y, speed, speed1, angle, direction, graphic, graphic1, delay){

	let angle1 = angle+180;

	let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
	
	while(!Obj_IsDeleted(obj)){
	
		if(direction==1){
			angle1+=20;
			}
		else{
			angle1-=20;
			}
			
		Shot4SE;
		
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), speed1, angle1, graphic1, 0);
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), speed1-0.2, angle1, graphic1, 0);
		
		loop(2){
			yield;
			}
		}
	}
	
task Dew(x, y, speed, speed1, angle, direction, graphic, graphic1, delay){

	let angle1 = angle+180;

	let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
	
	while(!Obj_IsDeleted(obj)){
	
		if(direction==1){
			angle1+=20;
			}
		else{
			angle1-=20;
			}
		
		CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), speed1, GetAngleToPlayer(obj), graphic1, 0);
		
		loop(20){
			yield;
			}
		}
	}