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

@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(6000);}
	case(EV_REQUEST_TIMER){SetScriptResult(62);}

}

@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], "Normal" , [false,false] );
	
	CreateLifeBar(Junko, 50, 54, 41, 0, [255,255,255], "Normal" , [false,false] );
	
renderOkinaExHSiFS(Okina);
TAura(Okina,0);
	
renderEXJunkoLoLK(Junko);
TAura(Junko,6);
	
	ObjRender_SetPosition(enm,-250,-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-60,  GetStgFrameHeight/2-70, 7, 4);
ObjMove_SetDestAtWeight(Junko, GetStgFrameWidth/2+60, GetStgFrameHeight/2-70, 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 angleToPlayer {
	let dir = atan2(GetPlayerY-GetY,GetPlayerX-GetX);
	return dir;
}


task BossActions{

	wait(20);
	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{

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 =0;

let array=GetShotIdInCircleA1(320, 240, 1000); //get all shots ID into an array
let D=150;
	wait(110);
	//ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -2, 2, angle, 120, 20, [232, 0, 0], "DARK");
	JunkoWork;
	OkinaWork;
	

}}

task JunkoWork{
	let ang=rand(0,360);
	let angB=5;
		while(!Obj_IsDeleted(Okina) && ObjEnemy_GetInfo(Junko,INFO_LIFE)>2 && ObjEnemy_GetInfo(Junko,INFO_LIFE)<=3100){
			CreateShotPatternB1;
			wait(120);
			TMovement2(Junko,45,15,2);
			wait(110);
		}
		
		ObjMove_SetDestAtWeight(Junko,GetStgFrameWidth/2,  GetStgFrameHeight/2-90,3,4);
		wait(90);
		
		while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=6000&& !Obj_IsDeleted(Junko)&& ObjEnemy_GetInfo(Junko,INFO_LIFE)>=2){
			CreateShotPatternB2;
			wait(60);
			TMovement2(Junko,45,25,2);
			wait(60);

		}
		
		task CreateShotPatternB1{
			let g = BL_SMALL_RED+500;
			ascent(j in 0..5){
			PlaySoundEffect("se_kira00");
			ascent(i in 0..50){
				CreateShotA4(JunkoX+5*cos(ang), JunkoY+5*sin(ang), 4-0.15*j, ang,-0.1,0,g, 15,70+30*j);
				ang+=360/50;
			}
			ang+=4;
			wait(6);}
		}
		
		task CreateShotPatternB2{
			ascent(j in 0..7){
					let g = BL_SMALL_RED+500;
			PlaySoundEffect("se_kira00");
			ascent(i in 0..60){
				CreateShotA4(JunkoX+5*cos(ang), JunkoY+5*sin(ang), 4-0.25*j, ang,-0.1,0,g+2*j, 15,120+25*j);
				ang+=360/60;
			}
			ang+=9;
			wait(6);}
		}


		
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);
ObjShot_SetSpellResist(shota1,true);
if(ObjEnemy_GetInfo(Junko,INFO_LIFE)<=2){Obj_Delete(shota1);}

ObjMove_AddPatternA2(shota1,cdr,NO_CHANGE,NO_CHANGE,0.07,0,3.75);

}

	
}

task OkinaWork{
	let graph=ICE_AQUA;
	let ang=270;
	let ang2=25;
	
	while(!Obj_IsDeleted(Junko) && ObjEnemy_GetInfo(Okina,INFO_LIFE)>2 && ObjEnemy_GetInfo(Okina,INFO_LIFE)<=3100){
		
		functionbullet(ang);
		functionbulletEX(ang);
		wait(15);
		TMovement2(Okina,45,15,1);
		wait(180);
		
	}
	
	ObjMove_SetDestAtWeight(Okina,GetStgFrameWidth/2,  GetStgFrameHeight/2-90,3,4);
	
	wait(90);
	
	while(ObjEnemy_GetInfo(enm,INFO_LIFE)<=6000&& !Obj_IsDeleted(Okina)&& ObjEnemy_GetInfo(Okina,INFO_LIFE)>=2){
		functionbullet2(ang);
		functionbulletEX2(ang);
		wait(15);
		TMovement2(Okina,55,15,1);
		wait(180);
		}
		

	task functionbullet(ang){
		ascent(j in 0..24){
			PlaySoundEffect("se_tan01");
			ascent(k in 0..3){
		ascent(i in 0..2){
			CreateShotA4(OkinaX+15*cos(ang), OkinaY+15*sin(ang), 2+0.15*k, ang,ICE_MAGENTA, 15);
			ang+=360/2;
		}
			}
		ang+=20;
		wait(6);}
		
	}
	
	task functionbulletEX(ang){
		ascent(j in 0..24){
			ascent(k in 0..3){
		ascent(i in 0..2){
			CreateShotA4(OkinaX+(20-2*j)*cos(ang), OkinaY+15*sin(ang), 2.25+0.15*k, ang+90,ICE_MAGENTA, 15);
			ang+=360/2;
		}
			}
		ang+=20;
		wait(6);}
		
	}
	
	task functionbullet2(ang){
		ascent(j in 0..50){
			PlaySoundEffect("se_tan01");
			ascent(k in 0..3){
		ascent(i in 0..3){
			CreateShotA4(OkinaX+15*cos(ang), OkinaY+15*sin(ang), 2+0.15*k, ang,ICE_RED, 15);
			ang+=360/3;
		}
		ang+=1;
			}
		ang+=16;
		wait(4);}
		
	}
	
	task functionbulletEX2(ang){
		ascent(j in 0..50){
			ascent(k in 0..3){
		ascent(i in 0..3){
			CreateShotA4(OkinaX+(15-2*j)*sin(ang), OkinaY+(15+2*j)*cos(ang), 2.25+0.15*k, ang+90,ICE_RED, 15);
			ang+=360/3;
		}
		ang+=-1;
			}
		ang+=-16;
		wait(4);}
		
	}
	
	
	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);
ObjShot_SetSpellResist(shota1,true);
if(ObjEnemy_GetInfo(Okina,INFO_LIFE)<=2){Obj_Delete(shota1);}

}
}


	
//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);
	ObjEnemy_SetLife(BaseObj,3100);
	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);
			ControlFlight(BaseObj);
			ObjEnemy_AddLife(BaseObj,1);
			ObjEnemy_AddLife(enm,-5000);
			Invincible(BaseObj,300);
			Invincible(Junko,450);
			Invincible(Okina,450);
			//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));
}
