//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["LB Spell 6 Hard"]
#Text[""]
#Background["./Background.dnh"]
#System["System/System Prototype.dnh"]
#Player["Player/Thief Team/Thief Team.dnh"]

#include "Script/include_single.dnh"

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let objEnemyMain;
let objScene;
let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;
let GetLife = 0;
let GetTimer = 0;
let GetX = 0;
let GetY = 0;

let TeleArray = [
	[40,40],
	[GetCenterX,40],
	[GetStgFrameWidth-40,40],
	[GetStgFrameWidth-40,GetCenterY],
	[GetStgFrameWidth-40,GetStgFrameHeight-40],
	[GetCenterX,GetStgFrameHeight-40],
	[40,GetStgFrameHeight-40],
	[40,GetCenterY],
];

@Loading {
LoadTexture(MD ~ "IMG/Enemy/Leblanc1.png");
}

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
	case(EV_REQUEST_TIMER){	SetScriptResult(120); }
	case(EV_REQUEST_IS_DURABLE_SPELL) { SetScriptResult(true); }
}

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);
	
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlancs, the Blade's Blades");
	Obj_SetValue(objEnemyMain,"Circle Color","Red");
	Obj_SetValue(objEnemyMain,"Aura Color",[1,1,10]);
	Obj_SetValue(objEnemyMain,"Spell ID",023);
	Obj_SetValue(objScene,"Spell Base",1);
	Obj_SetValue(objEnemyMain,"Cut Image",[MD ~ "IMG/Event/LeBlanc/LeBlanc1.png"]);
	SetAutoDeleteObject(true);
	AnimateBossLB(objEnemyMain);
	PlayEnemyHitSound(objEnemyMain,1);
	SetInvincibilityDamageRate(objEnemyMain, 360, 70/3, 50/3);
	
	SetCommonData("DurableSpell",true);
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, 120, 10, 5);
	CreateLocalSoundObject("watchclosely", MD ~ "SE/LeBlanc_WatchClosely.ogg", GetOwnScriptID);
	CreateLocalSoundObject("laugh1", MD ~ "SE/LeBlanc_laugh1.ogg", GetOwnScriptID);
	CreateLocalSoundObject("laugh2", MD ~ "SE/LeBlanc_laugh2.ogg", GetOwnScriptID);
	CreateLocalSoundObject("laugh3", MD ~ "SE/LeBlanc_laugh3.ogg", GetOwnScriptID);
	CreateLocalSoundObject("laugh4", MD ~ "SE/LeBlanc_laugh4.ogg", GetOwnScriptID);
	CreateLocalSoundObject("teleport", MD ~ "SE/LeBlanc_W_Return.ogg", GetOwnScriptID);
	CreateLocalSoundObject("noescape", MD ~ "SE/LeBlanc_NoEscape.ogg", GetOwnScriptID);
	CreateLocalSoundObject("silence", MD ~ "SE/LeBlanc_Silence.wav", GetOwnScriptID);
	mainTask;
	End;
}

@MainLoop {
	GetX = ObjMove_GetX(objEnemyMain);
	GetY = ObjMove_GetY(objEnemyMain);
	GetLife = ObjEnemy_GetInfo(objEnemyMain,INFO_LIFE);
	GetTimer = ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER);
	
	ObjEnemy_SetIntersectionCircleToShot(objEnemyMain, GetX, GetY, 32);
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemyMain, GetX, GetY, 24);
	
	yield;
}

task mainTask{
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/Katarina.png");
	let angle = 0;
	let scalex = 0.35;
	let scaley = 0.20;
	let locarray = [];
	let index = 0;
	let one = -1;
	let color = 0;
	ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
	wait(60);
	PlaySoundEffect("effect2", 100);
	loop(60){
		ObjRender_SetAngleZ(objEnemyMain, angle);
		ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
		angle+=360/60;
		scalex-=0.35/60;
		scaley-=0.20/60;
		yield;
	}
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/LeBlanc.png");
	PlaySoundEffect("effect2", 100);
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlanc, the Deceiver");
	Obj_SetValue(objEnemyMain,"Aura Color",[2,10,1]);
	Obj_SetValue(objEnemyMain,"Circle Color","Purple");
	loop(60){
		ObjRender_SetAngleZ(objEnemyMain, angle);
		ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
		angle-=360/60;
		scalex+=0.25/60;
		scaley+=0.25/60;
		yield;
		}
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*45);
	wait(75);
	PlaySoundEffect("silence",100);
	PlaySoundEffect("watchclosely",100);
	ascent(i in 0..length(TeleArray)) {
		TeleportPad(TeleArray[i][0],TeleArray[i][1]);
	}
	wait(60);
	let dir = rand(0,360);
	CreateShotRing02(16,GetX,GetY,4,dir,-1.0/15,3.0,ORB_BLUE,30);
	CreateShotRing02(16,GetX,GetY,4,dir+360/30,-1.8/15,2.2,ORB_BLUE,30);
	CreateShotRing02(16,GetX,GetY,4,dir,-2.6/15,1.4,ORB_BLUE,30);
	CreateShotRing02(16,GetX,GetY,4,dir+360/30,-3.4/15,0.6,ORB_BLUE,30);
	PlaySoundEffect("charge2",90);
	ConcentrationA4(objEnemyMain,"MAGIC",7,120, [0, 0, 255]);
	wait(30);
	TeleportBoss(-200,-200);
	SetCommonData("MagicCircle",false);
	wait(120);
	PlaySoundEffect("laugh1", 100);
	wait(120);
	ObjEnemy_SetLife(objEnemyMain, 0); //for ending early
	wait(100);
	let prev = -1;
	let num = 0;
	loop {
		num = RandomPadSelection(prev,4,7);
		prev = num;
		TeleportBoss(TeleArray[num][0],TeleArray[num][1]);
		wait(30);
		PlaySoundEffect("charge2",90);
		ConcentrationA4(objEnemyMain,"MAGIC",7,120, [0, 0, 255]);
		wait(30);
		Fire1;
		TeleportBoss(-200,-200);
		wait(90);
		num = RandomPadSelection(prev,0,3);
		prev = num;
		TeleportBoss(TeleArray[num][0],TeleArray[num][1]);
		wait(30);
		PlaySoundEffect("charge2",90);
		ConcentrationA4(objEnemyMain,"MAGIC",7,120, [0, 0, 255]);
		wait(30);
		Fire2;
		TeleportBoss(-200,-200);
		wait(90);
	}
}
task Fire1 {
	let dir = rand(0, 360);
		loop(15){
			let obj = CreateShot02(GetX,GetY,4,dir,-0.1,2,LARGE_PURPLE,30);
			dir+=360/15;
			Explode(obj);
		}
		
	task Explode(obj){
	let dir2 = ObjMove_GetAngle(obj);
        while(!Obj_IsDeleted(obj)){
            if(trunc(ObjMove_GetX(obj))>384 || trunc(ObjMove_GetX(obj))<0 || trunc(ObjMove_GetY(obj))>448 || trunc(ObjMove_GetY(obj))<0){
				PlaySoundEffect("shot5", 75);
				CreateShotRing02(9,ObjMove_GetX(obj),ObjMove_GetY(obj),3, dir2, -2/30, 1+1/3, BL_SMALL_MAGENTA, 15);
                Obj_Delete(obj);
                return;    
			}
                yield;
		}
	}
}

task Fire2 {
	let dir = 90;
	ascent(i in 0..15){
		loop(38){
			CreateShot02(GetX,GetY,3.5+0.4*sin(dir*5),dir,-0.1, 1+(i/12.5)+0.4*sin(dir*5),DIAMOND_PELLET_YELLOW,30);
			dir+=360/38;
		}
	}
}

task Fire3 {
	let dir = rand(0,360);
	CreateShotRing02(12,GetX,GetY,4,dir,-1.0/15,3.0,ORB_BLUE,30);
	CreateShotRing02(12,GetX,GetY,4,dir+360/24,-1.8/15,2.2,ORB_BLUE,30);
	CreateShotRing02(12,GetX,GetY,4,dir,-2.6/15,1.4,ORB_BLUE,30);
}
task CurveLaser(x,y,angle,rot) {
	let obj = CreateCurvyLaserB1(x,y,10,angle,12,24,ORB_YELLOW,1,40,0);
	ObjMove_AddPatternA2(obj,0,NO_CHANGE,NO_CHANGE,-6/8,6.0*rot,2);
	ObjMove_AddPatternA2(obj,30,NO_CHANGE,NO_CHANGE,10/90,0,10);
	wait(30);
	loop(30) {
		let angle = ObjMove_GetAngle(obj);
		if(angle<GetAngleToPlayer(obj)) {
			angle+=0.05;
		}
		if(angle>GetAngleToPlayer(obj)) {
			angle-=0.05;
		}
		ObjMove_SetAngle(obj,angle);
		yield;
	}
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
}
function RandomPadSelection(prev,low,high) {
	let select = rand_int(low,high);
	while(select==prev) { select = rand_int(low,high); }
	return select;
}
function TeleportBoss(x,y) {
	PlaySoundEffect("teleport",100);
	TeleportEffect(GetX,GetY);
	ObjMove_SetPosition(objEnemyMain,x,y);
	TeleportEffect(x,y);
}
function rand_int(low,high) {
	let int = floor(rand(0,high+1));
	while(int==high+1) { int = floor(rand(0,high+1)); }
	return int
}
task TeleportPad(x,y) {
	let objEffect = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objEffect,40);
	ObjRender_SetBlendType(objEffect,BLEND_ADD_ARGB);
	ObjPrim_SetTexture(objEffect,MD ~ "System/IMG/MC_Purple.png");
	ObjSprite2D_SetSourceRect(objEffect,0,0,512,512);
	ObjSprite2D_SetDestCenter(objEffect);
	
	let spin = 0;
	let dspin = 1;
	
	ObjRender_SetScaleXYZ(objEffect,0.15,0.15,0.15);
	ObjRender_SetPosition(objEffect,x,y,0);
	
	while(!Obj_IsDeleted(objEnemyMain)) {
		spin+=dspin;
		ObjRender_SetAngleZ(objEffect,spin);
		yield;
	}
}
task TeleportEffect(x,y) {
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj1,40);
	ObjRender_SetBlendType(obj1,BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj1,MD ~ "System/IMG/Effect/Magic.png");
	ObjSprite2D_SetSourceRect(obj1,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj1);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2,40);
	ObjRender_SetBlendType(obj2,BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj2,MD ~ "System/IMG/Effect/ConcentrationCircle.png");
	ObjSprite2D_SetSourceRect(obj2,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj2);
	
	let maxscale1 = 2.25*0.6;
	let maxscale2 = 3.375*0.6;
	let scale1 = 0;
	let scale2 = 0;
	let alpha = 255*2;

	ObjRender_SetPosition(obj1,x,y+10,0);
	ObjRender_SetPosition(obj2,x,y,0);

	ascent(i in 0..60) {
		scale1 = maxscale1*sin(90*i/60);
		scale2 = maxscale2*sin(90*i/60);
		alpha-=(255*2)/60;
		ObjRender_SetScaleXYZ(obj1,scale1,scale1,scale1);
		ObjRender_SetColor(obj1,alpha/2,0,alpha);
		ObjRender_SetScaleXYZ(obj2,scale2,scale2,scale2);
		ObjRender_SetColor(obj2,alpha/2,0,alpha);
		yield;
	}
}

task End {
	while(ObjEnemy_GetInfo(objEnemyMain, INFO_LIFE) > 0) { yield; }
	ExplosionB1(objEnemyMain,"STAR",[128,64,255],[128,64,255]);
	Obj_Delete(objEnemyMain);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
}