//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["LB Spell 3 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;

@Loading {
}

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

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);
	
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlanc, the Arrow of Retribution");
	Obj_SetValue(objEnemyMain,"Aura Color",[2,10,1]);
	Obj_SetValue(objEnemyMain,"Circle Color","Purple");
	Obj_SetValue(objEnemyMain,"Spell ID",011);
	Obj_SetValue(objScene,"Spell Base",1);
	Obj_SetValue(objEnemyMain,"Cut Image",[MD ~ "IMG/Enemy/LuxCut.png"]);
	SetAutoDeleteObject(true);
	AnimateBossLB(objEnemyMain);
	PlayEnemyHitSound(objEnemyMain,1);
	SetInvincibilityDamageRate(objEnemyMain, 360, 70/3, 57/3);
	
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, 120, 10, 5);
	CreateLocalSoundObject("luxdeclare", MD ~ "SE/Lux_Declare.ogg", GetOwnScriptID);
	CreateLocalSoundObject("E_Toss", MD ~ "SE/Lux_E_Toss.wav", GetOwnScriptID);
	CreateLocalSoundObject("E_Explode", MD ~ "SE/Lux_E_Explode.wav", GetOwnScriptID);
	CreateLocalSoundObject("effort", MD ~ "SE/Lux_Effort.wav", GetOwnScriptID);
	CreateLocalSoundObject("laugh", MD ~ "SE/Lux_Laugh.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{
	let spark = CSD ~ "SE/spark.wav";
	CreateLocalSoundObject("spark", MD ~ "SE/spark.wav", GetOwnScriptID);
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/Varus.png");
	let angle = 0;
	let one = 1;
	let scalex = 0.25;
	let scaley = 0.20;
	loop(60){
		scalex-=0.25/60;
		scaley-=0.20/60;
		scalex+=0.35/60;
		scaley+=0.25/60;
		}
	ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
	let tarx = 0;
	let tary = 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.25/60;
		yield;
		}
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/Lux.png");
	Obj_SetValue(objEnemyMain,"Aura Color",[1,1,10]);
	PlaySoundEffect("effect2", 100);
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlanc, the Lady of Luminosity");
	loop(60){
		ObjRender_SetAngleZ(objEnemyMain, angle);
		ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
		angle-=360/60;
		scalex+=0.35/60;
		scaley+=0.2125/60;
		yield;
		}
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*52);
	SetShotAutoDeleteClip(30, 50, 30, 30);
	wait(75);
	PlaySoundEffect("luxdeclare", 100);
	wait(60);
	loop{
		loop(2){
		PlaySoundEffect("E_Toss", 100);
		angle = GetAngleToPlayer(objEnemyMain);
		loop(6){
			Singularity(GetX+115*cos(angle), GetY+115*sin(angle), 60);
			angle+=360/6;
		}
		angle+=360/6/3*one;
		loop(6){
			Singularity(GetX+190*cos(angle), GetY+190*sin(angle), 90);
			angle+=360/6;
		}
		angle+=360/6/3*one;
		loop(6){
			Singularity(GetX+275*cos(angle), GetY+275*sin(angle), 120);
			angle+=360/6;
		}
		wait(75);
		ExMoveCenter(objEnemyMain, 120, 5, rand(25, 40));
		wait(75);
		}
		loop(2){
			MasterSpark01(60, GetAngleToPlayer(objEnemyMain), 0.35, 75);
			wait(30);
			//PlaySoundEffect("effort", 100);
			wait(90);
			}
			one*=-1;
			wait(45);
	}
}

task Singularity(destx, desty, time){
	let angle2 = rand(0, 360);
	let color = 0;
	let obj = CreateShot01(GetX, GetY, 0, 90, ORB_GRAY, 0);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtWeight(obj, destx, desty, 10, 5);
	wait(time);
	if(Obj_IsDeleted(obj)){ return; }
	PlaySoundEffect("E_Explode", 100);
	SunExplosion(ObjMove_GetX(obj),ObjMove_GetY(obj),0.3,20,[255,255,255]);
	loop(3){
		loop(5){
			let obj2 = CreateShot02(ObjMove_GetX(obj)+rand(15, 55)*cos(angle2), ObjMove_GetY(obj)+rand(15, 55)*sin(angle2), 0, angle2+rand(-2, 2), 0.015, rand(0.7, 1.35), [SMALL_GRAY, SMALL_RED, SMALL_ORANGE, SMALL_YELLOW, SMALL_GREEN, SMALL_BLUE, SMALL_AQUA, SMALL_MAGENTA][color%7], 10);
			ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
			angle2+=360/5 + rand(-25, 25);
			color++;
		}
		color++;
		wait(9);
		}
	Obj_Delete(obj);
	}

task RunReflectCode(obj, left, top, right, bottom){
        while(!Obj_IsDeleted(obj)){
                if(trunc(ObjMove_GetX(obj))>384 && right == true)
				{
                        ObjMove_SetX(obj,384-ObjMove_GetX(obj)+384);
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
                        return;
                }
   
                if(trunc(ObjMove_GetX(obj))<0 && left == true)
                {      
                        ObjMove_SetX(obj,-ObjMove_GetX(obj));
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
                        return;
                }
               
        if(trunc(ObjMove_GetY(obj))>448 && bottom ==true)
                {
                       
                        ObjMove_SetY(obj,448-ObjMove_GetY(obj)+448);
                        ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
						return;
        }
        if(trunc(ObjMove_GetY(obj))<0 && top == true)
                {
						
                        ObjMove_SetY(obj,-ObjMove_GetY(obj));
                        ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
                        return;
                
        }
                yield;
        }
}
task SunExplosion(x,y,maxscale,frames,color) {
	let objSun1 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objSun2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let scale = 0;
	let alpha = 1;
	
	ObjRender_SetPosition(objSun1,x,y,0);
	
	let path = MD ~ "IMG/Enemy/Nuke1.png";
	ObjPrim_SetTexture(objSun1,path);
	ObjRender_SetBlendType(objSun1,BLEND_ADD_ARGB);
	ObjSprite2D_SetSourceRect(objSun1,0,0,GetTextureWidth(path),GetTextureHeight(path));
	ObjSprite2D_SetDestCenter(objSun1);
	Obj_SetRenderPriorityI(objSun1,51);
	
	path = MD ~ "IMG/Enemy/Nuke2.png";
	ObjPrim_SetTexture(objSun2,path);
	ObjRender_SetBlendType(objSun2,BLEND_ADD_ARGB);
	ObjSprite2D_SetSourceRect(objSun2,0,0,GetTextureWidth(path),GetTextureHeight(path));
	ObjSprite2D_SetDestCenter(objSun2);
	Obj_SetRenderPriorityI(objSun2,51);
	
	ascent(i in 0..frames) {
		scale = maxscale*sin(90*i/frames);
		if(Obj_IsDeleted(objEnemyMain)) { Obj_Delete(objSun1); Obj_Delete(objSun2); }
		ObjRender_SetPosition(objSun1,x,y,0);
		ObjRender_SetPosition(objSun2,x,y,0);
		ObjRender_SetScaleXYZ(objSun1,scale,scale,1);
		ObjRender_SetScaleXYZ(objSun2,scale,scale,1);
		ObjRender_SetColor(objSun1,color[0],color[1],color[2]);
		ObjRender_SetColor(objSun2,255,255,255);
		yield;
	}
	wait(5);
	loop(45) {
		ObjRender_SetScaleXYZ(objSun1,scale,scale,1);
		ObjRender_SetScaleXYZ(objSun2,scale,scale,1);
		ObjRender_SetColor(objSun1,color[0]*alpha,color[1]*alpha,color[2]*alpha);
		ObjRender_SetColor(objSun2,255*alpha,255*alpha,255*alpha);
		alpha-=1/45;
		scale+=0.05/45;
		yield;
	}
	Obj_Delete(objSun1);
	Obj_Delete(objSun2);
}
task MasterSpark01(frames,angle,widthfactor,delay) {
	let imgEffect = MD ~ "IMG/Enemy/spark.png";
	let MSring = 0;
	let Master_Time = 0;
	
	//ScreenShake;
	
	MasterSpark(angle,widthfactor,[200,200,200]);
	MasterSpark(angle+3,widthfactor,[200,200,200]);
	MasterSpark(angle-3,widthfactor,[200,200,200]);
	MasterSparkHitbox(widthfactor);
	
	wait(delay);
	
	PlaySoundEffect("spark",100);
	PlaySoundEffect("laugh", 100); 
	loop(frames+30){
		MSring++;
		Master_Time++;
		if(MSring>10 && frames-10 > Master_Time){ MasterSparkRing(widthfactor); MSring=0; }
		if(Master_Time == frames) { FadeOutSound("spark",30);}
		if(GetLife<=0) { FadeOutSound("spark",30); break; }
		yield;
	}

	task MasterSpark(objAngle, widthfactor, Color){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let SparkTime = 0;
		let xBend = 4;
		let yBend = 4;
		let BendInc = 0;
		let SparkSet = 0;
		let objAngleSet = angle;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj, 40);
		ObjPrim_SetTexture(obj, imgEffect);
		ObjSprite2D_SetSourceRect(obj, 1, 0, 256, 128);
		ObjRender_SetPosition(obj, GetX, GetY, 0);
		ObjRender_SetAngleZ(obj, objAngleSet);
		ObjRender_SetColor(obj, Color[0], Color[1], Color[2]);
		
		ObjRender_SetScaleY(obj,widthfactor);
		
		loop(delay) {
			ObjSprite2D_SetDestRect(obj, 0, -xBend, yBend, xBend);
			if(yBend<512) { yBend+=508/delay*4; }
			if(GetLife<=0) { break; }
			yield;
		}

		while(Obj_IsDeleted(obj) == false){
			SparkTime++;
			ObjRender_SetPosition(obj, GetX, GetY, 0);
			ObjSprite2D_SetDestRect(obj, 0, -xBend, yBend, xBend);
			ObjRender_SetAngleZ(obj, objAngleSet);
			
			if(xBend<100&&SparkSet==0){
				xBend+=BendInc;
				BendInc+=1.0;
				if(objAngleSet<objAngle){ objAngleSet+=BendInc/12; }
				if(objAngleSet>objAngle){ objAngleSet-=BendInc/12; }
			}
			if(SparkTime>frames && SparkSet==0){
				objAngleSet = objAngle;
				SparkSet=1;
				BendInc=0;		
				SparkSet=2;
			}
			if(SparkTime>frames&&SparkSet==2){
				xBend+=BendInc;
				BendInc-=0.8;
				if(objAngleSet<angle){ objAngleSet+=0.2; }
				if(objAngleSet>angle){ objAngleSet-=0.2; }
			}
			if(xBend<-20){ Obj_Delete(obj); }
			if(GetLife<=0) { Obj_Delete(obj); }
			yield;
		}
	}
	
	task MasterSparkRing(widthfactor) {
	
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let SparkTime = 0;
		let yBend = 15;
		let xBend = 60;
		let BendInc = 0.22;
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.68);
		//ObjRender_SetColor(obj, 255, 134, 72);
		ObjRender_SetAlpha(obj, 110);
		ObjPrim_SetTexture(obj, imgEffect);
		ObjSprite2D_SetSourceRect(obj, 15, 129, 242, 212);

		while(Obj_IsDeleted(obj) == false){
			SparkTime++;
			ObjSprite2D_SetDestRect(obj, -xBend, -yBend, xBend, yBend);
			ObjRender_SetPosition(obj, GetX+SparkTime*24*cos(angle), GetY+SparkTime*24*sin(angle), 0);
			
			ObjRender_SetScaleX(obj,widthfactor);
			ObjRender_SetAngleZ(obj, angle+90);
			
			if(yBend<53){
				yBend+=BendInc;
			}
			if(xBend<120){
				xBend+=BendInc/2;
			}
			if(SparkTime>119){ Obj_Delete(obj); }
			BendInc+=6;
			yield;
		}
	}
	
	task MasterSparkHitbox(widthfactor) {
		wait(delay+5);
		let obj = CreateStraightLaserA1(GetX-30*cos(angle),GetY-30*sin(angle),angle,500,335*widthfactor,frames-5,LARGE_RED,0);
		Obj_SetVisible(obj,false);
		while(!Obj_IsDeleted(obj)) {
			if(GetLife<=0) { Obj_Delete(obj); }
			yield;
		}
	}	
	
	task ScreenShake {
		loop(120) {
			Set2DCameraFocusX(Get2DCameraX + rand(-12, 12));
			Set2DCameraFocusY(Get2DCameraY + rand(-12, 12));
			yield;
		}
		Reset2DCamera;
	}
}	
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);
}