#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YuukaSpell4"]
#Text["A spell that's more intimidating than anything, but still difficult...Kind of like Yuuka, huh?"]
//#BGM["script/Kazami/resources/sounds/area4_boss.wav"]
#Background["./Background.txt"]

#include"script/Kazami/AllStars shot/AllStarShot_Const.dnh"
#include"script/default_system/Default_Effect.txt"

let objYuuka;
let bConcentrationMotion = false;
let frame=0;
let objPlayer;
let Target=0;
let Wave=0;
let leaf=0;
let animation=0;
let Aim=0;
let stem=0;
let objtext;
let spellname=1;
let angle=0;
let angle1=0;
let angle2=45;
let angle3=45;
let BG;

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

@Initialize
{	

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	
	objYuuka = ObjEnemy_Create(OBJ_ENEMY_BOSS);

	ObjEnemy_Regist(objYuuka);

	let imgYuuka = GetCurrentScriptDirectory ~ "resources/images/dot_yuka.png"; //file path to enemy image

	ObjPrim_SetTexture(objYuuka, imgYuuka); //Setting the above image file as a texture to the enemy object(objEnemy)

	ObjSprite2D_SetSourceRect(objYuuka, 39, 30, 95, 101); //Setting the rectangle coordinates in the enemy image to use(Left, Top, Right, Bottom).

	ObjSprite2D_SetDestCenter(objYuuka); //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(objYuuka, cx, 80, 40);

	//DrawBG;	
}

@MainLoop{

	if(spellname==1){

	objtext = ObjText_Create();
	ObjText_SetText(objtext, "Death: A Hell Flower Sheds its Petals");
     	Obj_SetRenderPriority(objtext, 0.74);//Render above cutin
     	ObjText_SetFontSize(objtext, 16);
     	ObjText_SetFontColorTop(objtext, 0, 136, 0);
     	ObjText_SetFontColorBottom(objtext, 0, 136, 0);
     	ObjText_SetFontBold(objtext, false);
     	ObjText_SetFontBorderType(objtext, BORDER_SHADOW);
    	ObjText_SetFontBorderColor(objtext, 232, 232, 232);
     	ObjText_SetFontBorderWidth(objtext, 1);
     	ObjRender_SetPosition(objtext, GetStgFrameLeft+5, 20, 0);
     	ObjMove_SetDestAtFrame(objtext, 267, 23, 6);

	}
	
	if(animation==45){
		//Concentration(objYuuka)
		}
		
	if(animation==85){
		MCircle(objYuuka);
		}

	spellname++;
	
	let yx = ObjMove_GetX(objYuuka);

	let yy = ObjMove_GetY(objYuuka);
	
	let px = GetPlayerX();
	let py = GetPlayerY();

	let Player = atan2(py - yy, px - yx);
	
	animation++;

	if(animation>=90){
		frame++;
		stem++;
		
		if(stem==120){
			let angle=Player;
			loop(6){
				angle+=360/6;
				FlowerShotA1(yx+20*cos(angle), yy+20*sin(angle), 2, angle, LARGE_ORANGE, PELLET_RED, PELLET_ORANGE, 10);
				}
			stem=0;
			}
		
		if(frame==200){
			let angle= rand(-25, 25);
			
			ObjMove_SetDestAtSpeed(objYuuka, rand(120,GetStgFrameWidth-120), rand(90, 130), 0.5);
			//RainShotA1(yx, yy, 5, angle+180, BL_SMALL_BLUE, rand(257, 261), 10);
			//RainShotA1(yx, yy, 5, angle, BL_SMALL_BLUE, DROP_BLUE, 10);
			frame=0;
			}
			
		ObjEnemy_SetIntersectionCircleToShot(objYuuka, yx, yy, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(objYuuka, yx, yy, 24);

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

			Obj_Delete(objYuuka);

			Obj_Delete(objtext);
	
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			
			CloseScript(GetOwnScriptID());
	
			return;
		}
		}
	
	yield;
	}

	task MCircle(tobj){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let path = "script/Kazami/resources/images/e_ef_61.png";
	ObjPrim_SetTexture(obj,path);
	
	Obj_SetRenderPriorityI(obj,20);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,200);
	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);
	
	ObjRender_SetPosition(obj,ObjMove_GetX(objYuuka),ObjMove_GetY(objYuuka),0);
	ObjSprite2D_SetDestRect(obj,-sx,-sy,sx,sy); //taken from BG code, don't know what it does.
	
	spinBG;
 
	task spinBG{
		let spin = 0;
		loop{
		let life = ObjEnemy_GetInfo(objYuuka, INFO_LIFE);
		ObjRender_SetAngleXYZ(obj,0,0,spin);
		ObjRender_SetPosition(obj,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
		ObjRender_SetScaleXYZ(obj,life/4000,life/4000,0);
		spin+=-0.5;
		yield;
		}
	}
	
	}
	
task Concentration(tobj){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let path = "script/Kazami/resources/images/e_ef_02.png";
	ObjPrim_SetTexture(obj,path);
	
	Obj_SetRenderPriorityI(obj,20);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,255);
	let sx = (300-0)/2; let sy = (300-0)/2;
	ObjSprite2D_SetSourceRect(obj,0,0,256, 256);
	
	ObjRender_SetPosition(obj,ObjMove_GetX(objYuuka),ObjMove_GetY(objYuuka),0);
	ObjSprite2D_SetDestRect(obj,-sx,-sy,sx,sy);
	
	spinBG;
 
	task spinBG{
		let spin = 0;
		let conc = 1.5;
		loop{
		let life = ObjEnemy_GetInfo(objYuuka, INFO_LIFE);
		ObjRender_SetAngleXYZ(obj,0,0,spin);
		ObjRender_SetPosition(obj,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
		ObjRender_SetScaleXYZ(obj,conc,conc,0);
		
		conc-=0.03;
		spin+=-1;
		
		if(conc<=0){
		Obj_Delete(obj)
			}
		yield;
		}
		}
	
	}
	
task RainShotA1(x, y, speed, angle, graphic1, graphic2, delay){
	let obj = CreateShotA1(x, y, speed, angle, graphic1, delay);
	
	let yx = ObjMove_GetX(objYuuka);

	let yy = ObjMove_GetY(objYuuka);
	
	let px = GetPlayerX();
	let py = GetPlayerY();

	let Player = atan2(py - yy, px - yx);
	
	while(!Obj_IsDeleted(obj)){
			loop(5){
				yield;
				}
			CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(1, 3), Player, graphic2, 10);
			CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(1, 3), Player-180, graphic2, 10);
			
		}
	}
	
task FlowerShotA1(x, y, speed, angle, graphic1, graphic2, graphic3, delay){
	let obj = CreateShotA1(x, y, speed, angle, graphic1, delay);
	
	let yx = ObjMove_GetX(objYuuka);

	let yy = ObjMove_GetY(objYuuka);
	
	let px = GetPlayerX();
	let py = GetPlayerY();

	let Player = atan2(py - yy, px - yx);
	
	if(ObjMove_GetX(obj)<=0){
		Obj_Delete(obj);
		}
	if(ObjMove_GetX(obj)>=GetStgFrameWidth){
		Obj_Delete(obj);
		}
	if(ObjMove_GetY(obj)<=0){
		Obj_Delete(obj);
		}
	if(ObjMove_GetY(obj)>=GetStgFrameHeight){
		Obj_Delete(obj);
		}

	
	while(!Obj_IsDeleted(obj)){
			loop(20){
				yield;
				}
			let obj1 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, angle+150, graphic2, 10);
			let obj2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, angle-150, graphic2, 10);
			let obj3 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, angle-110, graphic3, 10);
			let obj4 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, angle+110, graphic3, 10);
			let obj5 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, angle+180, graphic3, 10);
			let obj6 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, angle, graphic3, 10);
			
			if(ObjMove_GetX(obj1)==0){
				Obj_Delete(obj1);
				Obj_Delete(obj2);
				Obj_Delete(obj3);
				Obj_Delete(obj4);
				Obj_Delete(obj5);
				Obj_Delete(obj6);
				}
		}
	}