#TouhouDanmakufu[Single]
#System["./system/Default_System.txt"]
#Title["Hina Spell 5"]
#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 CameraZoom = 1;

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


@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(9000*3);}
	case(EV_REQUEST_TIMER){SetScriptResult(110);}
	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, 50, 0);
renderHina(enm);
BossIndicator(enm);
TAura(enm, 6);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2, 12, 4);
	Invincible(650);
	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 angle=-90;
	 
	PerformActions;
	while(ObjEnemy_GetInfo(enm, INFO_LIFE) > 0){
		yield;
	}
	
	NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	ObjMove_SetDestAtWeight(enm,ObjMove_GetX(enm)+50,ObjMove_GetY(enm)-40,2,0.75);
	NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	StartSlow(TARGET_ALL,15);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(50);
	StopSlow(TARGET_ALL);
	NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(enm);

	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	
	loop(90){yield;}
	noshoot;
	zoom;
	wait(60);
	Obj_SetValue(enm,"cast",1);
	ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -6,3, angle, 90, 20, [255, 0, 0], "DARK");
	TCharge01(EnemyX,EnemyY,4,30,210, 3, 252);
	wait(30);
	LoadMagicCircle(1);
	StartSpellMiddle("\"Youkai Mountain's Journey's End\" ", current~"./img/HinaBase.png",current~"./img/HinaBase2.png", 0, 227, 53,520,509,1);
	CreateLifeBar(enm, 60, 55, 41, 0, [255,255,255], "Default" , [false,false] );
	PlaySoundEffect("se_enep02");
	MapleExplode(50,2,2,8);
	ExplosionA2(enm, "purple");
	SetForbidPlayerShot(false);
	unzoom;
	wait(90);
	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, "aqua");
	IJNFlag;
	Obj_SetValue(enm,"cast",1);
	TwinSide;
	wait(90);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-130, 6, 1.25);
	BubbleShot1;
	wait(100);
	
	PatternCheck;
	}}

	
task PatternCheck{
let cdr1=160;
let angle=90;
let angle2=rand(0,360);
let angle3=rand(0,360);
let angle4=rand(0,360);

	while(ObjEnemy_GetInfo(enm,INFO_LIFE)>=8000*3){
	
	Pattern1(angle,angle2,2);
	angle2+=18;
	wait(23);	
	}
	
	PlaySoundEffect("se_ch00");
	
	while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=8000*3 && ObjEnemy_GetInfo(enm,INFO_LIFE)>=6000*3){
	
	Pattern1(angle,angle2,3);
	angle+=-2.5;
	angle2+=6;
	wait(23);	
	
	}
	
	PlaySoundEffect("se_ch00");
	angle3=angle;
	angle4=angle2;
	
	while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=6000*3 && ObjEnemy_GetInfo(enm,INFO_LIFE)>=4000*3){
	
	Pattern1(angle,angle2,3);
	Pattern1(angle3,angle4,3);

	angle+=-2.5;
	angle2+=5;
	angle3+=2.5;
	angle4+=-5;
	
	wait(23);	

	}
	
	
	PlaySoundEffect("se_ch00");
	Pattern2(angle);

	while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=4000*3&& ObjEnemy_GetInfo(enm,INFO_LIFE)>=2000*3){
	
	Pattern1(angle,angle2,4);
	Pattern1(angle3,angle4,4);

	angle+=-3.5;
	angle2+=9;
	angle3+=3.5;
	angle4+=-9;
	
	wait(22);	

	
	}
	PlaySoundEffect("se_ch00");
	Pattern3;
	while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=2000*3 && ObjEnemy_GetInfo(enm,INFO_LIFE)>0 ){
	
	Pattern1(angle,angle2,4);
	Pattern1(angle3,angle4,4);
	angle+=-4.5;
	angle2+=12;
	angle3+=4.5;
	angle4+=-12;
	
	wait(22);	
	}
	
	
}

task Pattern1(angle,angle2,cShot){
	
	let r=0;
	
	ascent(j in 0..7){
		CreateShotPattern1(EnemyX,EnemyY,3,angle,angle2,-0.04,0,RICE_PURPLE,15,BLEND_ALPHA,cShot);
		angle+=360/7;
	}

task CreateShotPattern1(x, y, speed, angle,angle2,accel,maxspd,graphic, delay,blendtpye,cShot){
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);
if(Obj_IsDeleted(shota1)){return;}
ObjShot_SetAutoDelete(shota1,false);
ObjShot_SetSpellResist(shota1,true);
ObjRender_SetBlendType(shota1,blendtpye);
wait(120);
ascent(i in 0..cShot){
CreateShotA4(ObjMove_GetX(shota1)+10*cos(angle2), ObjMove_GetY(shota1)+10*sin(angle2), 0, angle2,0.03,2,graphic, 15,BLEND_ALPHA);
angle2+=360/cShot;
}
Obj_Delete(shota1);

}
	
}

task Pattern2(ang){
	let velo=0.2;
	let graph=[SMALL_RED,SMALL_BLUE];
while(!Obj_IsDeleted(enm)){
PlaySoundEffect("se_kira00");
ascent(j in 0..2){
ascent(i in 0..15){
CreateShotPattern2(EnemyX-90+180*j,EnemyY,3,ang,-0.04,velo,1,graph[j%2],15,BLEND_ALPHA);
ang+=360/15;}
velo*=-1;
ang+=15;
}
wait(90);
}

task CreateShotPattern2(x, y, speed, angle,accel,velo,maxspd,graphic, delay,blendtpye){
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);
let positionLock=GetAngleToPlayer(shota1);
ObjShot_SetSpellResist(shota1,true);
ObjRender_SetBlendType(shota1,blendtpye);
ObjMove_SetAngularVelocity(shota1,velo);

}
}

task Pattern3{
	let angle=rand(0,360);
	while(!Obj_IsDeleted(enm)){
	ascent(i in 0..20){
		CreateShotA4(EnemyX,EnemyY,1, angle,0,0,ARROWHEAD_GREEN, 15,BLEND_ALPHA);
		angle+=360/20;
	}
	angle+=7.5;
	wait(30);
	}
}

task CreateShotA4(x, y, speed, angle,accel,maxspd,graphic, delay,blendtpye){
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);
ObjRender_SetBlendType(shota1,blendtpye);
ObjShot_SetSpellResist(shota1,true);
}
	

task BubbleShot1{
	let ang=[85,95];
	let bShot=1;
	ascent(j in 0..4){
		PlaySoundEffect("se_kira00");
	ascent(i in -bShot..bShot+1){
		CreateShotA4(EnemyX,EnemyY,3.5,ang[j%2]+140/7*i,-0.05,1,BUBBLE_PURPLE,5,BLEND_ADD_ARGB);
	}
	bShot+=1;
	wait(30);
	}
}		
			
task TwinSide{
	let sideang=180;
	let posX=EnemyX;
	let Xchange=30;
	let posY=EnemyY;
	loop(60){
		ascent(j in 0..2){
	ascent(i in -4..3){
		CreateShotA4(posX-Xchange,posY+55*i,6,sideang+rand(-5,5),0,0,FIRE_PURPLE,5,BLEND_ADD_ARGB);
	}
	sideang+=180;
	Xchange*=-1;
		}
	wait(4);
	}
}

	task Invincible(frame){
		SetInvincibility(enm, frame);
		
	}
	
		task zoom{
		loop(90){
	
	CameraZoom+=1/50;
	Set2DCameraRatio(CameraZoom);
	Set2DCameraFocusX(EnemyX);
	Set2DCameraFocusY(EnemyY);
	yield;}
		
		
	}
	
		task unzoom{
		loop(90){
	
	CameraZoom+=1/-50;
	Set2DCameraRatio(CameraZoom);
	Set2DCameraFocusX(EnemyX);
	Set2DCameraFocusY(EnemyY);
	yield;}
		
	}
	
	task noshoot{
		loop(160){
			SetPlayerSpeed(0,0);
			SetForbidPlayerShot(true);
			yield;
		}
		SetForbidPlayerShot(false);
		SetPlayerSpeed(3.75,1.75);
	}
