#TouhouDanmakufu[Single]
#System["./system/Default_System.txt"]
#Title["Hina Spell 4"]
#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(5300*3);}
	case(EV_REQUEST_TIMER){SetScriptResult(62);}
	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-100, 12, 4);
	Invincible(400);
	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);
	SetPlayerSpeed(4.5,2);
	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	loop(90){yield;}
	LoadMagicCircle(1);
	StartSpellMiddle("Mystic Crystal \"Stamina Consume\" ", current~"./img/HinaBase.png",current~"./img/HinaBase2.png", 0, 227, 53,520,509,1);
	
	loop(60){yield;}

let angle=-90;

let a =0;
	
	let D=140;
	let FixAngle=14;
	let AngleFix=4;
	let spd=1;
	
	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);
	
	PatternA1;
	wait(160);
	loop{
	controlfam;
	wait(1800);}
		
	}}

	task controlfam{
	let ang=rand(0,360);
	let rs=70;
	PlaySoundEffect("se_option");
	ascent(i in 0..3){
	let fami=fam(GetPlayerX,GetPlayerY,1800,1,false,ang);
	FamPatternA1(fami);
	Control1(fami,ang,rs,2.75);
	ang+=360/3;
	}
	
	wait(1100);
	
	PlaySoundEffect("se_option");
	ascent(i in -1..2){
	let fami=fam(GetPlayerX,GetPlayerY,1800,1,false,0);
	//FamPatternA1(fami);
	Control2(fami,100*i,70);
	FamPatternA2(fami);
	}
	
	task FamPatternA1(fami){
		wait(120);
		
		let angChange=0;
		let angE=rand(0,360);
		ascent(k in 0..6){
		ascent(i in 0..3){
			PlaySoundEffect("se_tan01");
		CreateShotGimmickA1(ObjMove_GetX(fami), ObjMove_GetY(fami), 0, atan2(ObjMove_GetY(fami)-EnemyY,ObjMove_GetX(fami)-EnemyX)+180, LARGE_CRYSTAL_RED, 25,BLEND_ADD_ARGB);
		wait(2);
		}
		wait(35);
		}
		
		ascent(i in 0..35){
			PlaySoundEffect("se_tan01");
		CreateShotGimmickA1(ObjMove_GetX(fami), ObjMove_GetY(fami), 0, atan2(ObjMove_GetY(fami)-EnemyY,ObjMove_GetX(fami)-EnemyX)+180, LARGE_CRYSTAL_RED, 25,BLEND_ADD_ARGB);
		wait(3);}
		
		let spd=[1.25,1.75];
		ascent(k in 0..26){
			CreateShotA4(ObjMove_GetX(fami), ObjMove_GetY(fami), spd[k%2], ang, ORB_RED, 15,BLEND_ADD_ARGB);
			ang+=360/26;
		}
		
		Obj_Delete(fami);
		
		wait(415);
		ExplosionA2(enm,"red");
		PlaySoundEffect("se_pldead01");
		
		ascent(i in 0..4){
		ascent(j in 0..37){
			CreateShotA4(EnemyX+90*cos(angE),EnemyY+30*sin(angE), spd[j%2], 180+angE+angChange, SMALL_RED, 15,BLEND_ADD_ARGB);
			angE+=360/37;
		}
		angChange+=45;
		}
		SetPlayerSpeed(4.5,2);
		
	}
	
	task FamPatternA2(fami){
		let angE=rand(0,360);
		let angChange=0;
		wait(90);
		ascent(j in 0..4){
		ascent(i in 0..5){
		PlaySoundEffect("se_tan01");
		CreateShotGimmickA1(ObjMove_GetX(fami), ObjMove_GetY(fami), 1.25+0.25*i, atan2(ObjMove_GetY(fami)-EnemyY,ObjMove_GetX(fami)-EnemyX)+180, LARGE_CRYSTAL_RED, 25,BLEND_ADD_ARGB);
		wait(1);
		}
		wait(65);
		}
		
		wait(30);
		
		let spd=[1,1.25];
		ascent(k in 0..25){
		CreateShotA4(ObjMove_GetX(fami), ObjMove_GetY(fami), spd[k%2], ang, ORB_RED, 15,BLEND_ADD_ARGB);
		ang+=360/25;
		}
		
		Obj_Delete(fami);
		
		wait(120);
		PlaySoundEffect("se_ch00");
		wait(60);
		ExplosionA2(enm,"red");
		PlaySoundEffect("se_pldead01");
		
		ascent(i in 0..4){
		ascent(j in 0..37){
		CreateShotA4(EnemyX+20*cos(angE),EnemyY+60*sin(angE), spd[j%2], 180+angE+angChange, SMALL_RED, 15,BLEND_ADD_ARGB);
		angE+=360/37;
		}
		angChange+=-45;
		}
		SetPlayerSpeed(4.5,2);

	}
		
	task Control1(ene,angle,rs,way){
	let r = 0;
	let r2= 0;
	let c = 0;
	let s = 1;	
	let acc = 3;
	let d = 0;
	let count = 0;
	
	loop(320){
	ObjMove_SetPosition(ene,GetPlayerX+r*cos(angle+c*3*way),GetPlayerY+r*sin(angle+c*3*way));
	if(r<rs ){r+=1.25;}
	c-=acc;
	angle++;
	if(acc>0.3){acc -= 0.04;}
	
	yield;
	}
	
	PlaySoundEffect("se_ch00");
	loop(160){
	ObjMove_SetPosition(ene,GetPlayerX+r*cos(angle+c*3*way),GetPlayerY+r*sin(angle+c*3*way));

	c-=acc;
	angle+=-1.5;
	r+=1.25;
	if(acc>0.2){acc -= 0.01;}
	
	yield;
	}
	
	}
	
	task Control2(ene,xx,yy){
	loop(320){
	ObjMove_SetDestAtWeight(ene,GetPlayerX+xx,GetPlayerY+yy, 6, 3);
	yield;}
	
	ObjMove_SetDestAtWeight(ene,GetPlayerX+xx,GetStgFrameHeight/2-140, 6, 6);
	}
}
	
function fam(x,y,life,type,collision,angle){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let shot;
	let c = 0;
	let wi=65;
	let he=49;

	ObjEnemy_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjEnemy_SetLife(obj,life);
	ObjEnemy_SetDamageRate(obj,100,100);
	Control;
	DrawFam;
	Trail(obj,angle,BLEND_ADD_ARGB,ORB_RED);
	//Fire;
	return obj;
	
		task Control{	
			//PlaySound("fam",100,0);
			while(!Obj_IsDeleted(obj)){
			c++;
			
			if(ObjEnemy_GetInfo(obj,INFO_LIFE) <=0){
			
			Obj_Delete(obj);
			}
		
			if(collision==true){ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),24);
			ObjEnemy_SetIntersectionCircleToPlayer(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),30);}
			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 wi=32;
			let he=32;
			let img=GetCurrentScriptDirectory~"./img/fairysprite.png";
			ObjPrim_SetTexture(fam,img);
			
			if(type==1){ObjSprite2D_SetSourceRect(fam,0,he*7,wi,he*8);}
			else if (type==2){ObjSprite2D_SetSourceRect(fam,wi,he*7,wi*2,he*8);}
			else if (type==3){ObjSprite2D_SetSourceRect(fam,wi*2,he*7,wi*3,he*8);}
			else if (type==4){ObjSprite2D_SetSourceRect(fam,wi*3,he*7,wi*4,he*8);}
			ObjRender_SetAngleXYZ(fam,0,0,c*2);
			ObjRender_SetPosition(fam,x,y,0);
			ObjRender_SetScaleXYZ(fam,2,2,0);
			ObjSprite2D_SetDestCenter(fam);

			ObjRender_SetBlendType(fam,BLEND_ADD_ARGB);
			
				
				while(!Obj_IsDeleted(obj)){
				
				ObjRender_SetAlpha(fam,255);
				ObjRender_SetPosition(fam,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
				ObjRender_SetAngleXYZ(fam,0,0,c*2);
				c++;
				
				yield;
				
				}

					PlaySoundEffect("se_enep00");
					let rand1=rand(0,360);
					
					
					let obj2= CreateShotOA1(fam,0,0,ARROWHEAD_PURPLE,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 Trail(shot,angle,blendtype,graph){ 
 while(!Obj_IsDeleted(shot)) {
CreateShotTrail(ObjMove_GetX(shot), ObjMove_GetY(shot), 0, angle, graph, 0);
wait(2);
}
task CreateShotTrail(x, y, speed, angle,graphic, delay){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 1, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,graphic, 0);
ObjShot_SetIntersectionEnable(shota1,false);
ObjRender_SetBlendType(shota1,blendtype);
rendercomet(shota1);
wait(16);
Obj_Delete(shota1);
}

task rendercomet(obj){
	descent(i in 0..20){
		ObjRender_SetScaleXYZ(obj,1/18*i,1/18*i,0);
		yield;
	}
}

	
	   }
	}

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

	task PatternA1{
	let v=3.25;
	let angle=90;
	let c=4;
	let c2=2;
	let cdr=10;
	let angChange=14;
	
		loop(8){
			PlaySoundEffect("se_kira00");
		ascent(j in 0..c2){
		ascent(i in -c..c+1){
		CreateShotA4(EnemyX,EnemyY,v-absolute(i*0.1), angle+j*360/c2+i*3, ORB_PURPLE,15,BLEND_ADD_ARGB);}
		}
		c2++;
		v-=0.25;
		cdr+=4;
		wait(cdr);}
		
		wait(260);
		
		while(!Obj_IsDeleted(enm)){
		loop(2){
		loop(5){
		c2=5;
		v=1.5;
		PlaySoundEffect("se_kira00");
		ascent(j in 0..c2){
		ascent(i in -c..c+1){
		CreateShotA4(EnemyX,EnemyY,v-absolute(i*0.1), angle+i*3, ORB_PURPLE,15,BLEND_ADD_ARGB);}
		angle+=360/c2;
		}
		angle+=angChange;
		v-=0.25;
		wait(100);
		}
		angChange*=-1;
		wait(400);
		}
		wait(10);
		}
	}
		
task CreateShotA4(x, y, speed, angle, graphic, delay,blendtype){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
loop(delay){yield;}
let obj=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle, graphic, 0);
ObjRender_SetBlendType(obj, blendtype);
ObjShot_SetAutoDelete(obj,false);
ObjShot_SetSpellResist(obj,true);
wait(400);
ObjShot_FadeDelete(obj);
}

let USpeed=3.75;
let FSpeed=1.75;

task CreateShotGimmickA1(x, y, speed, angle, graphic, delay,blendtype){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
loop(delay){yield;}
let objS= CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle, graphic, 0);
ObjRender_SetBlendType(obj, blendtype);
ObjShot_SetAutoDelete(objS,false);
ObjShot_SetSpellResist(objS,true);
ObjMove_AddPatternA2(objS,140,NO_CHANGE,NO_CHANGE,0.021,0,3.75);

	//SetPlayerSpeed(USpeed,FSpeed);
	let trig=0;
	let dist;
	let dist2;
	
		while(!Obj_IsDeleted(enm)){
			
		if(Obj_IsDeleted(objS)){return;}
			
		dist = (((ObjMove_GetX(objS)-EnemyX)^2)+((ObjMove_GetY(objS)-EnemyY)^2))^0.5;
		dist2 = (((ObjMove_GetX(objS)-GetPlayerX)^2)+((ObjMove_GetY(objS)-GetPlayerY)^2))^0.5;
		
		if(dist<=15){
			PlaySoundEffect("se_lgods2");
			if(USpeed<=1){USpeed=1;}
			if(FSpeed<=0.5){FSpeed=0.5;}
			SetPlayerSpeed(USpeed,FSpeed);
			USpeed+=-0.1;
			FSpeed+=-0.08;
			Obj_Delete(objS);
		}
		
		if(dist2<=15){
			PlaySoundEffect("se_lgods1");
			if(USpeed>=4.5){USpeed=4.5;}
			if(FSpeed>=2){FSpeed=2;}
			SetPlayerSpeed(USpeed,FSpeed);
			USpeed+=+0.1;
			FSpeed+=+0.08;
			Obj_Delete(objS);
		}
		
		
			yield;
		}
	
}



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