#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YuukaSpell1"]
#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, "Vile Beauty: Bloody Lotus");
     	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);

	if(animation>=90){
		
		if(frame==11){
			angle+=6;
			angle1-=6;
			angle2+=6;
			angle3-=6;
			
			loop(4){
				angle+=360/4;
				angle1-=360/4;
				angle2+=360/4;
				angle3-=360/4;
				
				CreateShotA1(yx, yy, 2, rand(-5, 5)+angle, DZ_PELLET_BLUE, 5);
				CreateShotA1(yx, yy, 2, rand(-5, 5)+angle1, DZ_PELLET_BLUE, 5);
				CreateShotA1(yx, yy, 2, rand(-5, 5)+angle2, DZ_PELLET_RED, 5);
				CreateShotA1(yx, yy, 2, rand(-5, 5)+angle3, DZ_PELLET_RED, 5);
	
				}
			frame=0;
			}
			
		if(Wave==7){
			angle+=4;
			angle1-=4;
			angle2+=4;
			angle3-=4;
			
			loop(4){
				angle+=360/4;
				angle1-=360/4;
				angle2+=360/4;
				angle3-=360/4;
			
				CreateShotA1(yx, yy, 2.5, rand(-4, 4)+angle, DZ_PELLET_GREEN, 20);
				CreateShotA1(yx, yy, 2.5, rand(-4, 4)+angle1, DZ_PELLET_GREEN, 20);
				CreateShotA1(yx, yy, 2.5, rand(-4, 4)+angle2, DZ_PELLET_PURPLE, 20);
				CreateShotA1(yx, yy, 2.5, rand(-4, 4)+angle3, DZ_PELLET_PURPLE, 20);
				}
			Wave=0;
			}

		if(stem==200){
			ObjMove_SetDestAtSpeed(objYuuka, rand(12, 366), rand(60, 120), 1);
			stem=0;
			}
			
		if(leaf==50){
			Aim = rand(0,360);
			loop(100){
				Aim+=360/100;
				let obj = CreateShotA1(px+50*cos(Aim), py+50*sin(Aim), 2, Aim, BL_SMALL_RED+500, 10);
				
				
				ObjMove_SetAngularVelocity(obj, 7);
				
				ObjShot_SetDeleteFrame(obj, 120);
				
				}
			leaf=0;
			}
				
		frame++;
		Target++;
		leaf++;
		Wave++;
		//stem++;

		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;
		}
		}
	animation++;
	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_RGB);
	//ObjRender_SetAlpha(obj,50);
	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+=-2;
		
		if(conc<=0){
		Obj_Delete(obj)
			}
		yield;
		}
		}
	
	}
	
task DrawBG
{

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let path = GetCurrentScriptDirectory ~ "/resources/images/eff02b.png";
	let path2 = GetCurrentScriptDirectory ~ "/resources/images/eff02.png";
	ObjPrim_SetTexture(obj,path);
	ObjPrim_SetTexture(obj2,path2);
	
	Obj_SetRenderPriorityI(obj,20);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	let sx = (256-0); let sy = (256-0);
	ObjSprite2D_SetDestRect(obj,-sx,-sy,sx,sy);
	ObjRender_SetScaleXYZ(obj,1.5,1.5,0);
	
	ObjRender_SetScaleXYZ(obj,1,2,0);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	
	Obj_SetRenderPriorityI(obj2,20);
	ObjRender_SetBlendType(obj2,BLEND_ALPHA);
	ObjRender_SetAlpha(obj2,150);
	ObjSprite2D_SetSourceRect(obj2,0,0,256,256);
	let sx1 = (256-0)/2; let sy1 = (256-0)/2;
	ObjSprite2D_SetDestRect(obj2,-sx1,-sy1,sx1,sy1);
	ObjRender_SetScaleXYZ(obj2,2,2,0);
	ObjRender_SetPosition(obj2,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	
	while(ObjEnemy_GetInfo(objYuuka, INFO_LIFE)>=0){
		yield;
		
		}
		
	Obj_Delete(obj);
	Obj_Delete(obj2);
	
}