#TouhouDanmakufu[Single]
#System["./system/Default_System.txt"]
#Title["Hina Spell 3"]
#include"./Effects_Main.txt"

let current = GetCurrentScriptDirectory;
let enm = ID_INVALID;
let GetX = 0;
let GetY = 0;
let EnemyX = 0;
let EnemyY = 0;

let s=1;
let angle2=0;
let c=6; let c2=15; let c3=7;
let ang=0;

let objScene=GetEnemyBossSceneObjectID();
// your best friend, forever.


@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(4200*3);}
	case(EV_REQUEST_TIMER){SetScriptResult(80);}
	case(EV_REQUEST_SPELL_SCORE){
			SetScriptResult(1000000);
		}
	//case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
}

@Initialize{


SetAutoDeleteObject(true);
	enm = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(enm);
			ObjEnemy_SetDamageRate(enm, 75, 0);
	CreateLifeBar(enm, 60, 55, 41, 0, [255,255,255], "SpellCard" , [false,false] );
renderHina(enm);
BossIndicator(enm);
TAura(enm, 6);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-90, 12, 4);
	Invincible(480);
	BossActions;
}


@MainLoop{
	GetX = ObjMove_GetX(enm);
	GetY = ObjMove_GetY(enm);
EnemyX = ObjMove_GetX(enm);
angle2+=s;
	EnemyY = ObjMove_GetY(enm);
	ObjEnemy_SetIntersectionCircleToShot(enm, GetX, GetY, 28);
	ObjEnemy_SetIntersectionCircleToPlayer(enm, GetX, GetY, 32);
	yield;
}

function angleToBoss{
	let dir = atan2(GetY-GetPlayerY,GetX-GetPlayerX);
	return dir;
}

function angleToPlayer{
	let dir = atan2(GetPlayerY-GetY,GetPlayerX-GetX);
	return dir;
}



task BossActions{
	let dir = angleToPlayer;
	let angleT = rand(0,360);

	 
	PerformActions;
	while(ObjEnemy_GetInfo(enm, INFO_LIFE) > 0){
		yield;
	}
	

	//NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	Obj_Delete(enm);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);

	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	loop(90){yield;}
	LoadMagicCircle(1);
	StartSpellMiddle("Bewitched Weapon \"Cursed Decoy Doll\" ", current~"./img/HinaBase.png",current~"./img/HinaBase2.png", 0, 227, 53,520,509,1);
	
	loop(60){yield;}

let angle=-90;
if (GetCommonData("rank", "Normal")=="Normal"){c=6; c2=3; c3=10;}
else{c=8; c2=4; c3=12;}
let a =0;
	
	let D=140;
	
	let spd=1;
	let r=60;
	ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -9,3, angle, 50, 20, [255, 0, 0], "DARK");
	wait(40);
	TCharge01(EnemyX,EnemyY,4,30,182, 3, 252);
	wait(30);
	PlaySoundEffect("se_enep02");
	MapleExplode(50,2,2,8);
	ExplosionA2(enm, "purple");
	IJNFlag;
	Obj_SetValue(enm,"cast",1);
		
		let curseddoll= fam(EnemyX,EnemyY,999,true);
		ObjMove_SetDestAtWeight(curseddoll, GetStgFrameWidth/2, GetStgFrameHeight/2+20, 12, 4);
		wait(60);
		Effect(curseddoll, r);
		wait(130);
		Pattern1(curseddoll,r);
		wait(60);
		MoveDoll;
		DollPattern1(curseddoll);
		
		task MoveDoll{
			wait(120);
		while(!Obj_IsDeleted(enm)){
		ExplosionA2(curseddoll, "red");
		PlaySoundEffect("se_enep02");
		wait(55);
		ObjMove_SetDestAtWeight(curseddoll, GetPlayerX, GetPlayerY, 5, 3);
		wait(190);
		}
		}
	}}
	
let flagdanmaku=true;

task DollPattern1(obj){
	wait(160);
	let angle=0;
	let AngChange=16;
	while(!Obj_IsDeleted(enm)){
	ascent(i in 0..3){
		CreateShotA4(ObjMove_GetX(obj)+60*cos(angle),ObjMove_GetY(obj)+60*sin(angle),1.25,angle+180,0,0,OVAL_INV_RED,4,15);
	angle+=360/3;
	}
	angle+=AngChange;
	wait(20);
	}
}

task Pattern1(objEnemy,r){
		let AngChange=8;
	loop{
	let angle=rand(0,360);
	let directPos=8;
	
	ascent(j in 0..3){
	PlaySoundEffect("se_tan01");
	ascent(i in 0..90){
	Shot(objEnemy,r, EnemyX, EnemyY, 2.25, angle+360/90*i, AMULET_RED, 15);}
	wait(90);
	}
	
	wait(120);
	
	ascent(k in 0..60){
	PlaySoundEffect("se_tan01");
		ascent(j in 0..6){
		ascent(i in 0..3){
		Shot(objEnemy,r, EnemyX+rand(-3,3)+directPos, EnemyY+rand(-3,3), 2.25-0.15*i, angle+rand(-1,1)*i+360/6*j, AMULET_PURPLE, 25);}
		directPos*=-1;
		}
	directPos+=3;
	angle+=8;
	wait(9);
	}
	
	wait(60);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2, 6, 2);
	wait(60);
	ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -9,3, angle, 50, 20, [255, 0, 0], "DARK");
	wait(40);
	
	ascent(j in 0..5){
	PlaySoundEffect("se_tan01");
	ascent(i in 0..50){
	Shot(objEnemy,r, EnemyX, EnemyY, 1, angle+360/50*i, AMULET_RED, 15);}
	wait(80);
	}
	
	wait(90);
	
	ascent(k in 0..90){
	PlaySoundEffect("se_tan01");
	ascent(j in 0..7){
	Shot(objEnemy,r, EnemyX, EnemyY, 1+0.05*k, angle, AMULET_PURPLE, 25);
	angle+=360/7;}
	angle+=AngChange;
	wait(9);
	}
	AngChange*=-1;
	wait(60);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-90, 6, 2);
	wait(60);
	}
	
}

	function fam(x,y,life,collision){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let shot;
	let c = 0;
	let rect=99;
	ObjEnemy_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjEnemy_SetLife(obj,life);
	ObjEnemy_SetDamageRate(obj,5,5);
	DrawFam;
	//renderHinafam(obj);
	Control;
	
	return obj;

		task Control{	
			//PlaySound("fam",100,0);
			while(!Obj_IsDeleted(obj)){
			c++;
			
			if(ObjEnemy_GetInfo(obj,INFO_LIFE) <=0){
			//PlaySound("famdie",90,0);
			//TExplosionA(ObjMove_GetX(obj),ObjMove_GetY(obj),10,0.3);
			Obj_Delete(obj);
			}
		
			if(collision==true){ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),12);
			ObjEnemy_SetIntersectionCircleToPlayer(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),14);}
			yield;
			if(Obj_IsDeleted(enm)){Obj_Delete(obj);}
			}
		}
		
		task DrawFam{
			let fam=ObjPrim_Create(OBJ_SPRITE_2D);
			let px = 92;
			let py = 0;
			let c=0;
			let ang=0;
			let img=GetCurrentScriptDirectory~"./img/doll.png";
			ObjPrim_SetTexture(fam,img);
			ObjSprite2D_SetSourceRect(fam,0,0,rect,rect);
			ObjRender_SetAngleXYZ(fam,0,0,0);
			ObjRender_SetPosition(fam,x,y,0);
			Obj_SetRenderPriorityI(fam, 60);
			ObjRender_SetScaleXYZ(fam,0.5,0.5,0);
			ObjSprite2D_SetDestCenter(fam);
			ObjRender_SetBlendType(fam,BLEND_ALPHA);
				
				while(!Obj_IsDeleted(obj)){
				
				ObjRender_SetAlpha(fam,255);
				ObjRender_SetPosition(fam,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
				ObjRender_SetAngleXYZ(fam,0,0,c+3*sin(ang));
			//	ObjRender_SetPosition(obj, ObjRender_GetX(enm), ObjRender_GetY(enm)+2.5*sin(ang), 0);
				ang+=4;
				yield;
				
				}

					PlaySoundEffect("se_enep00");
					let rand1=rand(0,360);
					
					
					let obj2= CreateShotOA1(fam,0,0,ORB_RED,0);  // Create fake shot to create delay effect
					ObjRender_SetAlpha(obj2,0);
					ObjShot_SetIntersectionEnable(obj2,false);
					CallShot(obj2);
					
				
				Obj_Delete(fam);
				yield;
				Obj_Delete(obj2);
				
			
		}


	}
	
task Shot(objEnemy,r, x, y, speed, angle, grap, delay){
	
	if(speed < 0.1){return;}
	DelayShot(x, y, grap, angle, speed, BLEND_ADD_ARGB, 3, delay);
	wait(delay);
	let objS=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,grap, 0);
	
	//let objS = CreateShotA1(x, y, speed, angle, grap, delay);
	//ObjMove_ReflectCeiling(objS, 1);
	let trig=0;
	let dist;
	
	loop{
		
		while(!flagdanmaku){
			if(Obj_IsDeleted(objS)){return;}
			yield;
		}
		while(GetDistance2(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), ObjMove_GetX(objS), ObjMove_GetY(objS)) > r^2){
			if(!flagdanmaku){break;}
			if(Obj_IsDeleted(objS)){return;}
			yield;
		}
		loop{
			
			if(Obj_IsDeleted(objS)){return;}
			let L = GetDistance(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), ObjMove_GetX(objS), ObjMove_GetY(objS));
		ObjMove_SetAngle(objS,atan2(ObjMove_GetY(objS)-ObjMove_GetY(objEnemy),ObjMove_GetX(objS)-ObjMove_GetX(objEnemy))+180);			
		dist = (((ObjMove_GetX(objS)-ObjMove_GetX(objEnemy))^2)+((ObjMove_GetY(objS)-ObjMove_GetY(objEnemy))^2))^0.5;
		if(dist<=10){
			
			//ObjShot_SetDamage(objS,5);
			//ObjEnemy_AddLife(enm,-5);
			Obj_Delete(objS);

		}
			yield;
		}
	}
	

}

task Effect(objEnemy, r){
	let obj=CreateEffectObject(EnemyX,EnemyY,GetCurrentScriptDirectory~"./img/inv_circle.png","INVERT",0,0,512,512);
	let dist;
	let trig = 0;
	Obj_SetRenderPriorityI(obj, 50);
	ObjSprite2D_SetDestRect(obj, -r, -r, r, r);
	rendersize(obj);
	let alphaR = 0;
	flagdanmaku = true;
	
	while(!Obj_IsDeleted(objEnemy)){

		if(Obj_IsDeleted(objEnemy)){
			flagdanmaku = false;
			Obj_Delete(obj);
			return;
		}
		
		let objS = GetShotIdInCircleA2(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), r,TARGET_PLAYER);
		ascent(i in 0 .. length(objS)){
			//PlaySoundEffect("se_tan01");
			
			Shot(objEnemy,r, ObjMove_GetX(objS[i]), ObjMove_GetY(objS[i]), 2.75, atan2(ObjMove_GetY(objEnemy)-ObjMove_GetY(objS[i]),ObjMove_GetX(objEnemy)-ObjMove_GetX(objS[i])), ARROWHEAD_RED, 10);  //////ObjMove_GetAngle(objS[i])
			
			Obj_Delete(objS[i]);

			dist = (((ObjMove_GetX(objS)-ObjMove_GetY(objEnemy))^2)+((ObjMove_GetY(objS)-ObjMove_GetY(objEnemy))^2))^0.5;

		}
		
		alphaR = min(alphaR+0.05, 1.0);
		ObjRender_SetPosition(obj, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
		//ObjRender_SetColor(obj, 11*alphaR, 60*alphaR, 140*alphaR);
		yield;
	}

	task rendersize(obj){
		PlaySoundEffect("se_boon01");
		ascent(i in 0..20)
		{
			ObjRender_SetScale(obj,i/18);
			yield;
		}
	}
	

	
}

task CreateShotA4(x, y, speed, angle,accel,maxspd,graphic,graphvelo, delay){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA2(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,accel,maxspd,graphic, 0);
//ObjShot_SetSpellResist(shota1,true);
ObjShot_SetSpinAngularVelocity(shota1,graphvelo);
//ObjRender_SetBlendType(shota1,BLEND_ADD_ARGB);
}

function ObjRender_SetScale(obj,scale)
{
	return ObjRender_SetScaleXYZ(obj,scale,scale,scale);
}

function getDotAngle(x1,y1,x2,y2){
	let angle=atan2(y1-y2,x1-x2);
	return angle;
}


function CreateEffectObject(px,py,img,blendtype,left,top,right,bottom){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjSprite2D_SetDestCenter(obj);
	if(blendtype=="ALPHA"){
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	}
	if(blendtype=="RGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	}
	if(blendtype=="ARGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	}
	if(blendtype=="INVERT"){
	ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
	}
	ObjRender_SetPosition(obj,px,py,0);
	ObjMove_SetPosition(obj,px,py,);
	SetAutoDeleteObject(true);
	return obj;
}


function GetDistance(x, y, x2, y2){
	let d = ((x2-x)^2 + (y2-y)^2)^0.5;
	return d;
}

function GetDistance2(x, y, x2, y2){
	let d = ((x2-x)^2 + (y2-y)^2);
	return d;
}
		

	task Invincible(frame){
		SetInvincibility(enm, frame);
		
	}