//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["LB Spell 4 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 Lady of Luminosity");
	Obj_SetValue(objEnemyMain,"Aura Color",[2,10,1]);
	Obj_SetValue(objEnemyMain,"Circle Color","Purple");
	Obj_SetValue(objEnemyMain,"Spell ID",015);
	Obj_SetValue(objScene,"Spell Base",1);
	Obj_SetValue(objEnemyMain,"Cut Image",[MD ~ "IMG/Enemy/AhriCut.png"]);
	SetAutoDeleteObject(true);
	AnimateBossLB(objEnemyMain);
	PlayEnemyHitSound(objEnemyMain,1);
	SetInvincibilityDamageRate(objEnemyMain, 360, 70/3, 57/3);
	
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, 120, 10, 5);
	CreateLocalSoundObject("ahrideclare", MD ~ "SE/Ahri_Declare.ogg", GetOwnScriptID);
	CreateLocalSoundObject("spiritrush", MD ~ "SE/Ahri_R.wav", GetOwnScriptID);
	CreateLocalSoundObject("orbout", MD ~ "SE/Ahri_Q_out.wav", GetOwnScriptID);
	CreateLocalSoundObject("orbin", MD ~ "SE/Ahri_Q_in.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/Lux.png");
	let angle = 0;
	let scalex = 0.35;
	let scaley = 0.25;
	let locarray = [];
	let index = 0;
	let one = -1;
	let color = 0;
	loop(60){
		scalex-=0.35/60;
		scaley-=0.25/60;
		scalex+=0.35/60;
		scaley+=0.2125/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.215/60;
		yield;
	}
	//wait(15);
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/Ahri.png");
	Obj_SetValue(objEnemyMain,"Aura Color",[1,1,10]);
	PlaySoundEffect("effect2", 100);
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlanc, the Nine-Tailed Fox");
	loop(60){
		ObjRender_SetAngleZ(objEnemyMain, angle);
		ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
		angle-=360/60;
		scalex+=0.4/60;
		scaley+=0.2/60;
		yield;
		}
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*52);
	SetShotAutoDeleteClip(20, 50, 20, 20);
	wait(75);
	PlaySoundEffect("ahrideclare", 100);
	wait(60);
	loop{
		PlaySoundEffect("charge3", 100);
		ConcentrationA4(objEnemyMain, "HEART", 5, 150, [255, 0, 255]);
		wait(45);
		loop(2){
			PlaySoundEffect("wave2", 100);
			CharmEffect(GetX, GetY);
			Charm(60);
			wait(60);
		}
		PlaySoundEffect("charge3", 100);
		ConcentrationA4(objEnemyMain, "FIRE", 5, 150, [0, 76, 153]);
		wait(45);
		locarray = [GetCenterX+105, GetCenterX, GetCenterX-105];
		loop(3){
			index = floor(rand(0, length(locarray)));
			while(RoughlyEqualTo(locarray[index], GetX, 20)){
				index = floor(rand(0, length(locarray)));
			}
			if(locarray[index]>GetX){ one = 1; } else{ one = -1; }
			ObjMove_SetDestAtFrame(objEnemyMain, locarray[index], 120, 40);
			locarray = erase(locarray, index);
			PlaySoundEffect("spiritrush", 100);
			ascent(i in 0..18){
				loop(2){
					ascent(j in 1..4){
						CreateShot01(GetX+65*cos(angle), GetY+65*sin(angle), (j*0.95)+0.2-(i/35), angle, FIRE_BLUE, 10);
						}
					angle+=360/2*one;
					angle+=360/(18/(3.95))*one;
					angle+=rand(-2, 2);
					}
					loop(2){
					yield;
					}
			}
			wait(10);
			angle = rand(0, 360);
			wait(15);
		}
		PlaySoundEffect("charge3", 100);
		ConcentrationA4(objEnemyMain, "HEART", 5, 150, [255, 0, 255]);
		wait(45);
		loop(2){
			PlaySoundEffect("wave2", 100);
			CharmEffect(GetX, GetY);
			Charm(60);
			wait(60);
		}
		ObjMove_SetDestAtFrame(objEnemyMain, GetCenterX, GetY, 30);
		wait(30);
		PlaySoundEffect("charge3", 100);
		ConcentrationA4(objEnemyMain, "FIRE", 5, 150, [0, 76, 153]);
		wait(45);
		PlaySoundEffect("orbout", 100);
		ascent(i in -5..6){
			let obj = CreateShot01(GetX, GetY, (3.15-(absolute(i)/3))*1.16666666667, GetAngleToPlayer(objEnemyMain)+(i*25), ORB_AQUA, 10);
			Orb(obj, 9);
			}
		wait(240);
	}
}

task Orb(obj, rate){
	let time = 0;
	let maxspd = ObjMove_GetSpeed(obj);
	let spd = ObjMove_GetSpeed(obj);
	ObjShot_SetSpellResist(obj, true);
	ObjShot_SetAutoDelete(obj, false);
	wait(90);
	loop(15){
		spd-=maxspd/15;
		ObjMove_SetSpeed(obj, spd);
		yield;
		}
	PlaySoundEffect("orbin", 100);
	ObjMove_SetAngle(obj, AngleBetweenPoints(GetX, GetY, ObjMove_GetX(obj), ObjMove_GetY(obj)));
	while(!Obj_IsDeleted(obj)){

		if(spd>-maxspd*2){
			spd-=maxspd/30;
			ObjMove_SetSpeed(obj, spd);
		}
		if(time%rate==0){
			PlaySoundEffect("shot5", 90);
			ascent(i in -2..3){
				CreateShot02(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, ObjMove_GetAngle(obj)+(i*22.5), 0.015, maxspd/1.16666666667, PELLET_AQUA, 10);
				}
			}
		if(time>90){
			ObjShot_SetAutoDelete(obj, true);
		}
		time++;
		yield;
		}
	}

task Charm(frames){
	let magnitude = 1.15;
		ascent(i in 0..frames){
			ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX-(magnitude*(sin(i*(180/frames)))*cos(GetAngleToPlayer(objEnemyMain))), GetPlayerY-(magnitude*(sin(i*(180/frames)))*sin(GetAngleToPlayer(objEnemyMain)))); //non-linear movement
			//ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX-magnitude*cos(GetAngleToPlayer(objEnemyMain)), GetPlayerY-magnitude*sin(GetAngleToPlayer(objEnemyMain))); //linear movement
			yield;
		}
	}
	
task CharmEffect(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/Heart.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;
	let maxscale2 = 3.375;
	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,0,alpha);
		ObjRender_SetScaleXYZ(obj2,scale2,scale2,scale2);
		ObjRender_SetColor(obj2,alpha,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);
}