#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;
let position=0;
let pos = -50;

@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 Sun \"The Big Crunch -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 ~ "/Infinity_Star_Fractal_Stockies_by_zananeichan.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){
		//ObjMove_SetY(GetPlayerObjectID,ObjMove_GetY(GetPlayerObjectID)-1);
		}
	if(animation==40){
		//WHOOOP;
		//let imgfrost = GetCurrentScriptDirectory ~ "/p_ef_11.png";
		//frost(GetPlayerObjectID,imgfrost);
		}
		
	if(animation==620){
		ChargeSE;
		}
		
	if(animation>=650){
		shot++;
		move++;
		}
		
	if(animation>=180){
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);
		
		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 0){

			death;
		}
		}	
		
	if(animation>=100){
	
		frame++;
		
		if(pos<=530){
			pos++;
			}
		
		if(frame==70){
			angle+=1.1;
			//Wave3SE;
			loop(30){
				angle+=360/30;
				let obj = CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 1, angle+180, DS_STAR_S_YELLOW, 30);
				//CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 2, angle, DS_STAR_S_RED, 30);
				ObjShot_SetDeleteFrame(obj, 1000);
				ObjShot_SetAutoDelete(obj, false);
				}
			}
		if(frame==71){
			angle+=1.1;
			loop(30){
				angle+=360/30;
				let obj = CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 1, angle+180, DS_STAR_S_YELLOW, 30);
				//CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 2, angle, DS_STAR_S_RED, 30);
				ObjShot_SetDeleteFrame(obj, 1000);
				ObjShot_SetAutoDelete(obj, false);
				}
			}
		
		if(frame==72){
			angle+=1.1;

			loop(30){
				angle+=360/30;
				let obj = CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 1, angle+180, DS_STAR_S_YELLOW, 30);
				//CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 2, angle, DS_STAR_S_RED, 30);
				ObjShot_SetDeleteFrame(obj, 1000);
				ObjShot_SetAutoDelete(obj, false);
				}
			}
			
		if(frame==73){
			angle+=1.1;
			loop(30){
				angle+=360/30;
				let obj = CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 1, angle+180, DS_STAR_S_YELLOW, 30);
				//CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 2, angle, DS_STAR_S_RED, 30);
				ObjShot_SetDeleteFrame(obj, 1000);
				ObjShot_SetAutoDelete(obj, false);
				}
			}
		if(frame==74){
			angle+=1.1;
			loop(30){
				angle+=360/30;
				let obj = CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 1, angle+180, DS_STAR_S_YELLOW, 30);
				//CreateShotA1(GetStgFrameWidth/2+pos*cos(angle), 0+pos*sin(angle), 2, angle, DS_STAR_S_RED, 30);
				ObjShot_SetDeleteFrame(obj, 1000);
				ObjShot_SetAutoDelete(obj, false);
				}
				
			frame=0;
			
			angle+=100.1;
			}
		
		if(shot==200){
		
			Shot3SE;
			
			let angle2 = Player;
			let angle3 = angle2;
			
			loop(4){
				angle2+=360/4;
				let angle3 = angle2;
			
				loop(25){
					angle3+=360/25;
					CreateShotA1(PatchX+60*cos(angle2), PatchY+60*sin(angle2), 1.3, angle3, DS_BALL_BS_RED, 20);
					}
				}
			//angle2+=20;
			//shot=0;
			}
			
		if(shot==400){
		
			//Shot3SE;
			
			let angle2 = Player;
			let angle3 = angle2;
			
			SunShot(PatchX, PatchY, 80, 1.4, Player, 35, DS_BALL_BS_RED, 20);
			SunShot(PatchX, PatchY, 80, 1, Player+45, 35, DS_BALL_BS_RED, 20);
			SunShot(PatchX, PatchY, 80, 1, Player-45, 35, DS_BALL_BS_RED, 20);
			
			//angle2+=20;
			shot=0;
			}
			
		if(move==130){
			let pos = rand(100, GetStgFrameWidth-100);
			if(pos>=PatchX){
				rightmove;
				}
				
			if(pos<=PatchX){
				leftmove;
				}
				
			ObjMove_SetDestAtFrame(Patch, pos, rand(80, 120), 100);
			
			move=0;
			}
		}
	yield;
	}
	
task frost(tobj, ID){
	//let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let path = ID;
	//ObjPrim_SetTexture(obj,path);
	ObjPrim_SetTexture(obj1,path);
	
	//Obj_SetRenderPriorityI(obj,22);
	Obj_SetRenderPriorityI(obj1,22);
	//ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
	//ObjRender_SetAlpha(obj,230);
	ObjRender_SetAlpha(obj1,230);
	let sx = (300-0)/2; let sy = (300-0)/2; //taken from BG code, don't know what it does.
	//ObjSprite2D_SetSourceRect(obj,0,0,256, 256);
	ObjSprite2D_SetSourceRect(obj1,0,0,256, 256);
	
	//ObjRender_SetPosition(obj,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
	ObjRender_SetPosition(obj1,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
	//ObjSprite2D_SetDestRect(obj,-sx,-sy,sx,sy); //taken from BG code, don't know what it does.
	ObjSprite2D_SetDestRect(obj1,-sx,-sy,sx,sy); //taken from BG code, don't know what it does.
	
	spinBG;
 
	task spinBG{
		let spin = 0;
		let spin2 = 0;
		let flag=1;
		
		
		let size = 0;
		
		loop{
		//ObjRender_SetAngleXYZ(obj,0,0,spin);
		//ObjRender_SetPosition(obj,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
		//ObjRender_SetScaleXYZ(obj,0.9,0.9,0);
		ObjRender_SetAngleXYZ(obj1,0,0,spin2);
		ObjRender_SetPosition(obj1,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
		ObjRender_SetScaleXYZ(obj1,0.3, 0.3,0);
		
		if(ObjEnemy_GetInfo(Patch, INFO_LIFE) <= 0){
			//ObjRender_SetScaleXYZ(obj,0,0,0);
			ObjRender_SetScaleXYZ(obj1,0, 0,0);
			}
			
		spin+=-1;
		spin2+=0.3;
		yield;
			
		}
	}
	
	}

task SunShot(x, y, distance, speed, angle, num, graphic, delay) {

	Shot3SE;
	let angle3 = angle;
	
	loop(num){
	
		angle3+=360/num;
		CreateShotA1(x+distance*cos(angle3), y+distance*sin(angle3), speed, angle, graphic, delay);
		}

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