#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 5"]
#Text["Patchouli combines Metal and Water."]
//#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=10000;
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", "Water and Metal Sign \"Yukimarimo Illusion\"", 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 ~ "/frost_flowers_by_fraktalise-d3gdu52.jpg";
	let path2 = GetCurrentScriptDirectory ~ "/Tex_Stage5BG_04.png";
	
	BG(path1, path2, Patch);
}

@MainLoop{
	animation++;
	
	if(animation==20){
		declare;
		}
		
	if(animation==130){
		
		WHOOOP;
		SetPlayerSpeed(0.9, 0.4);
		let imgfrost = GetCurrentScriptDirectory ~ "/p_ef_11.png";
		frost(GetPlayerObjectID,imgfrost);
		}
	
	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>=130){
	
		ObjEnemy_SetIntersectionCircleToShot(Patch, PatchX, PatchY, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(Patch, PatchX, PatchY, 24);

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

			death;
			}
			
		move++;
		
		if(move==40){
			let angle4 = rand(0, 360);
			loop(300){
				angle4+=360/300;
					
				let obj5 = CreateShotA1(PatchX, PatchY, 2.7, angle4, DS_BALL_S_WHITE, 0);
				ObjRender_SetBlendType(obj5, BLEND_ADD_ARGB);
				ObjMove_SetAcceleration(obj5, -0.03);
					
				ObjShot_SetDeleteFrame(obj5, 80);
				}
			move=0;
			}
		
		frame++;
		
		if(frame==60){
		
			Shot4SE;
		
			Yukimarimo(PatchX, PatchY, 0.7, Player, DS_BALL_M_WHITE, 0);
			
			if(allow>=1000){
				allow=0;
				}
				
			frame=-180;
			}		
			
		if(allow==26){
			let angle1 = rand(0, 360);
			loop(16){
				angle1+=360/16;
				
				CreateShotA1(PatchX, PatchY, 0.9, angle1, DS_BALL_S_WHITE, 30);
				}
			allow=0;
			}
			
		if(shot==80){
			Shot3SE;
			
			let angle2 = Player;
			let angle3 = angle2+45;
			
			loop(20){
				angle2+=360/20;
				angle3+=360/20;
				
				let obj3 = CreateShotA1(PatchX, PatchY, 6, angle2, DS_BALL_S_SKY, 0);
				//let obj4 = CreateShotA1(PatchX, PatchY, 3, angle2, DS_BALL_S_SKY, 5);
				
				ObjMove_SetAcceleration(obj3, -0.12);
				//ObjMove_SetAcceleration(obj4, -0.12);

				ObjMove_SetMaxSpeed(obj3, 0.8);
				//ObjMove_SetMaxSpeed(obj4, 0.8);

				}
			
			shot=0;
			}
		//shot++;
		allow++;
	
		}
		
	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 Yukimarimo(x, y, speed, angle, graphic, delay) {

	let size = 1;
	
	let obj10 = CreateShotA1(x, y, speed, angle, graphic, delay);
	Obj_SetRenderPriorityI(obj10,70);
	
	ObjShot_SetAutoDelete(obj10, false);
			
	while(!Obj_IsDeleted(obj10)){
		size+=0.017;
		
		ObjRender_SetScaleXYZ(obj10, size, size, 0);
		ObjShot_SetIntersectionCircleA1(obj10, size*10+10.4);
				
		yield;
		}
		
	}
	
task death {

	Obj_Delete(Patch);
	//Obj_Delete(Koa);
	//Obj_Delete(Koa1);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	loop(100){	
		DeleteShotAll(TYPE_CHILD, TYPE_IMMEDIATE);
		SetPlayerSpeed(4, 1.6);
		
		yield;
		}
		
	Obj_Delete(Patch);
		
	CloseScript(GetOwnScriptID());
	
	return;
	
	}