#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./system/Default_System.txt"]

#include"./Effects_Main.txt"

let current = GetCurrentScriptDirectory;
let enm = ID_INVALID;
let GetX = 0;
let GetY = 0;
let EnemyX = 0;
let EnemyY = 0;
let OkinaX=0;
let OkinaY=0;
let JunkoX=0;
let JunkoY=0;

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


let objScene=GetEnemyBossSceneObjectID();
let Okina;
let Junko;

// your best friend, forever.


@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(6000);}
	case(EV_REQUEST_TIMER){SetScriptResult(45);}
	case(EV_REQUEST_SPELL_SCORE){
			SetScriptResult(2500000);
		}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
}

@Initialize{
ObjEnemy_SetDamageRate(enm, 100, 100);
Okina=Clone;
Junko=Clone;
	
	SetAutoDeleteObject(true);
	enm = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(enm);
	//CreateLifeBar(enm, 50, 54, 41, 0, [255,255,255], "Normal" , [false,false] );
	
	//CreateLifeBar(Okina, 50, 54, 41, 0, [255,255,255], "Default" , [false,false] );
	
	//CreateLifeBar(Junko, 50, 54, 41, 0, [255,255,255], "Default" , [false,false] );
	
renderOkinaExHSiFS(Okina);
TAura(Okina,0);
	
renderEXJunkoLoLK(Junko);
TAura(Junko,6);
	BossIndicator(Okina);
	BossIndicator(Junko);
	ObjRender_SetPosition(enm,GetStgFrameWidth/2,-300,0);	
	
ObjMove_SetPosition(Okina, Obj_GetValueD(objScene,"OkinaX", 0), Obj_GetValueD(objScene,"OkinaY", 0));
ObjMove_SetPosition(Junko, Obj_GetValueD(objScene,"JunkoX", 0), Obj_GetValueD(objScene,"JunkoY", 0));
		
ObjMove_SetDestAtWeight(Okina, GetStgFrameWidth/2-170,  GetStgFrameHeight/2+170, 7, 4);
ObjMove_SetDestAtWeight(Junko, GetStgFrameWidth/2+170, GetStgFrameHeight/2-170, 7, 4);
	
	BossActions;
}


@MainLoop{
	GetX = ObjMove_GetX(enm);
	GetY = ObjMove_GetY(enm);
	EnemyX = ObjMove_GetX(enm);
	EnemyY = ObjMove_GetY(enm);
	
	JunkoX=ObjMove_GetX(Junko);
	JunkoY= ObjMove_GetY(Junko);
	
	OkinaX=ObjMove_GetX(Okina);
	OkinaY= ObjMove_GetY(Okina);
	
	yield;
}

function angleToPlayerO {
	let dir = atan2(GetPlayerY-ObjMove_GetY(Okina),GetPlayerX-ObjMove_GetX(Okina));
	return dir;
}


task BossActions{

	wait(50);
	ExplosionA2(Junko, "white");
	JunkoBG(Junko);
	PerformActions;
	
	while(ObjEnemy_GetInfo(enm, INFO_LIFE) > 0){
		yield;
	}
	
	Obj_Delete(enm);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_SetValue(objScene,"JunkoX", ObjMove_GetX(Junko));//To pass on starting values
	Obj_SetValue(objScene,"JunkoY", ObjMove_GetY(Junko));
	
	Obj_SetValue(objScene,"OkinaX", ObjMove_GetX(Okina));//To pass on starting values
	Obj_SetValue(objScene,"OkinaY", ObjMove_GetY(Okina));
	
	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	loop(30){yield;}
	LoadMagicCircle(1);
	StartSpellTrioLeft("\"Pristine Anarchy World\" ", GetCurrentScriptDirectory~"./img/JunkoEX.png", 255, 155, 10,653,800,0.6);
	//StartSpellTrioMiddle(GetCurrentScriptDirectory~"./img/ClownpieceCutin.png", 255, 155, 10,485,650,0.7);
	StartSpellTrioRight(GetCurrentScriptDirectory~"./img/OkinaEX.png", 255, 155, 10,512,540,1);
	
	loop(30){yield;}
	
let angle = -90;
let x=0; let y=0; 
let s=1;
let t=0;
if (GetCommonData("rank", "Normal")=="Normal"){c=6; c2=3; c3=10;}
else{c=8; c2=4; c3=12;}
let a =1;

	wait(100);

	OkinaShot;
	JunkoShot;
	wait(30);
	MovingMover;
	wait(280);
	AgreeShot;
}}

task MovingMover{
	
	loop{
	
ObjMove_SetDestAtWeight(Okina, GetStgFrameWidth/2+280,  GetStgFrameHeight/2+190, 7, 2);
ObjMove_SetDestAtWeight(Junko, GetStgFrameWidth/2-280, GetStgFrameHeight/2-190, 7, 2);

wait(280);

ObjMove_SetPosition(Okina, GetStgFrameWidth/2+280, GetStgFrameHeight/2-190);
ObjMove_SetPosition(Junko, GetStgFrameWidth/2-280, GetStgFrameHeight/2+190);

ObjMove_SetDestAtWeight(Okina, GetStgFrameWidth/2-280,  GetStgFrameHeight/2-190, 7, 2);
ObjMove_SetDestAtWeight(Junko, GetStgFrameWidth/2+280, GetStgFrameHeight/2+190, 7, 2);

wait(280);

ObjMove_SetPosition(Okina, GetStgFrameWidth/2-280, GetStgFrameHeight/2+190);
ObjMove_SetPosition(Junko, GetStgFrameWidth/2+280, GetStgFrameHeight/2-190);


	}

}

task AgreeShot{
	let g = BL_SMALL_RED+500;
	let spd=1.75;		
			loop{
	
	ascent(i in 0..30){
	CreateShotA4(JunkoX+5*cos(ang), JunkoY+5*sin(ang), spd, ang,-0.5,0,BUBBLE_RED+500, 15,160);
	CreateShotA4(OkinaX+5*cos(ang), OkinaY+5*sin(ang), spd, ang,-0.5,0,BUBBLE_GREEN+500, 15,160);
	ang+=360/30;
			}
			spd+=0.04;
			wait(20);
			}
	
	task CreateShotA4(x, y, speed, angle,accel,maxspd,graphic, delay,cdr){
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(ObjEnemy_GetInfo(Junko,INFO_LIFE)<=2){Obj_Delete(shota1);}
wait(cdr);
//CallShot(shota1);
ObjShot_FadeDelete(shota1);

}
}

task OkinaShot{
	let ang=0;
	let a=90;
	loop{
			PlaySoundEffect("se_tan01");
	ascent(i in 0..15){
		
		CreateShotA4(OkinaX+35*cos(ang), OkinaY+35*sin(ang), 0, ang,LARGE_YELLOW, 15);
		ang+=360/15;
	}
	if(a<=60){a=60;}
	wait(a);
	a+=-3;}
	
task CreateShotA4(x, y, speed, angle,graphic, delay){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,graphic, 0);
ObjMove_AddPatternA2(shota1,60,NO_CHANGE,angleToPlayerO,0.07,0,3);
ObjShot_SetSpellResist(shota1,true);

}	
		}
		

task JunkoShot{
	let ang=rand(0,360);
	let a=60;
	loop{
			PlaySoundEffect("se_kira00");
		ascent(i in 0..35){
				CreateShotA4(JunkoX+5*cos(ang), JunkoY+5*sin(ang), 0.25, ang,0.02,2.5,BL_SMALL_RED,25);
				ang+=360/35;
			}
		ang+=4;
		
		if(a<=40){a=40;}
		wait(a);
		a+=-2;
	}
	
task CreateShotA4(x, y, speed, angle,accel,maxspd,graphic, 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);
}		
}

task CreateShotA5(x, y, speed, angle,accel,maxspd,graphic, 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);
ObjRender_SetBlendType(shota1,BLEND_ALPHA_INV);

}

		
//BulletBreak([ObjMove_GetX(obj2), ObjMove_GetY(obj2)],rand(0,360),ObjShot_GetImageID(obj2),BLEND_ALPHA);

function Clone{
	let BaseObj = ObjEnemy_Create(OBJ_ENEMY);
	let c = 0;	
	let color = [0,0,0];
	ObjEnemy_Regist(BaseObj);
	ObjRender_SetAlpha(BaseObj,255/2);
	ObjEnemy_SetLife(BaseObj,4100);
	ObjEnemy_SetDamageRate(BaseObj,100,100);
	Control(BaseObj);
	Invincible(BaseObj,120);
	return BaseObj;

	
		task Control(BaseObj)
	{	
			while(!Obj_IsDeleted(BaseObj)){
			c++;
			//ObjEnemy_SetIntersectionCircleToShot(BaseObj,ObjMove_GetX(BaseObj),ObjMove_GetY(BaseObj),24);
			//ObjEnemy_SetIntersectionCircleToPlayer(BaseObj,ObjMove_GetX(BaseObj),ObjMove_GetY(BaseObj),30);
			yield;
			
			if(ObjEnemy_GetInfo(BaseObj,INFO_LIFE) <=0){

			//NotifyEventAll(EV_USER+10, [ObjMove_GetX(BaseObj), ObjMove_GetY(BaseObj)]);
			//Obj_Delete(BaseObj);
			
			ObjEnemy_AddLife(enm,-6000);
			ObjEnemy_AddLife(Okina,-6000);
			ObjEnemy_AddLife(Junko,-6000);
			//wait(10);
			
			DeleteShotAll(TYPE_ALL,TYPE_ITEM);
			wait(90);
			
			Obj_Delete(BaseObj);
			}

			if(Obj_IsDeleted(enm)){Obj_Delete(BaseObj);}
			}
			
			task ControlFlight(BaseObj){
			while(!Obj_IsDeleted(BaseObj)){
			ObjMove_SetDestAtWeight(BaseObj, 0-250, 0-250,4,9);
			
			yield;
			}
		}
			
			
	}
}

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


task BulletCommands(obj){
   
    ObjMove_SetAngle(obj, GetAngleToPlayer(obj));
}
