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

#include"./Effects_Main.txt"
#include "./DialogueFunctions.dnh"


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(180);}

}

@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] );
	
renderOkinaHSiFS(Okina);
TAura(Okina,0);
	
renderJunko(Junko);
TAura(Junko,6);
	BossIndicator(Okina);
	BossIndicator(Junko);
	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));
		
ChargeUp(Okina, [GetStgFrameWidth/2-90,  GetStgFrameHeight/2-90], -6,3, angle, 50, 20, [0, 35, 142], "DARK");
ChargeUp(Junko, [GetStgFrameWidth/2+90,  GetStgFrameHeight/2-90], -6,3, angle, 50, 20, [255, 0, 0], "DARK");

	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(70);
	
	ObjMove_SetPosition(Junko, GetStgFrameWidth/2-90,  GetStgFrameHeight/2-90);
	ObjMove_SetPosition(Okina, GetStgFrameWidth/2+90,  GetStgFrameHeight/2-90);

	noshoot;
	
	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;
	//ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -2, 2, angle, 120, 20, [232, 0, 0], "DARK");
	
	wait(60);
	Talk;
	

}}

	task noshoot{
		while(!Obj_IsDeleted(enm)){
			SetForbidPlayerShot(true);
			SetForbidPlayerSpell(true);
			yield;
		}
		SetForbidPlayerSpell(false);
		SetForbidPlayerShot(false);
		
	}

	task Talk{
	
	yield;
	
    while(!Obj_IsDeleted(enm)) {
	    //this is where you program the attack
		IntiallizeEvent(1,1);
		Talking(0,LEFT);	
		//ObjEvent_SetGraphic(RIGHT,0,CSD ~ "img/KeikiShock.png",0.5);
		CreateDialog(450,150,"Hello Junko from[r]Legacy of Lunatic Kingdom.",18,false,BLACK,"FLAT",2,"RIGHT",70);
		//ObjEvent_SetGraphic(LEFT,0,CSD ~ "img/ReimuThink.png",0.5);
		Talking(1,RIGHT);
		CreateDialog(220,150,"Hello Okina from[r]Hidden Star in Four Seasons.",18,false,BLACK,"FLAT",2,"LEFT",70);
		//ObjEvent_SetGraphic(LEFT,0,CSD ~ "img/ReimuSmug.png",0.5);
		//ObjEvent_SetGraphic(RIGHT,0,CSD ~ "img/KeikiSweat.png",0.5);
	
		ExplosionA2(Okina, "white");
		renderOkinaExHSiFS(Okina);
		ExplosionA2(Junko, "white");
		renderEXJunkoLoLK(Junko);
		
		PlaySoundEffect("se_enep02");
	
		wait(10);
		SetCommonData("Duo","true");
		wait(90);
		FinallizeEvent;	
		ObjEnemy_SetLife(enm,0);
		yield;
	}
}



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