//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["LB Spell 2 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 objbullets = [];
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 Deceiver");
	Obj_SetValue(objEnemyMain,"Aura Color",[2,10,1]);
	Obj_SetValue(objEnemyMain,"Circle Color","Purple");
	Obj_SetValue(objEnemyMain,"Spell ID",007);
	Obj_SetValue(objScene,"Spell Base",1);
	Obj_SetValue(objEnemyMain,"Cut Image",[MD ~ "IMG/Enemy/VarusCut.png"]);
	SetAutoDeleteObject(true);
	AnimateBossLB(objEnemyMain);
	PlayEnemyHitSound(objEnemyMain,1);
	SetInvincibilityDamageRate(objEnemyMain, 360, 70/3, 57/3);
	
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, 120, 10, 5);
	CreateLocalSoundObject("varusdeclare", MD ~ "SE/Varus_Declare.wav", GetOwnScriptID);
	CreateLocalSoundObject("QCharge", MD ~ "SE/Varus_Q_Charge.wav", GetOwnScriptID);
	CreateLocalSoundObject("QFire", MD ~ "SE/Varus_Q_Fire.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 angle = 0;
	let scalex = 0.25;
	let scaley = 0.20;
	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.25/60;
		scaley-=0.20/60;
		yield;
		}
	//wait(15);
	ObjPrim_SetTexture(objEnemyMain, MD ~ "IMG/Enemy/Varus.png");
	PlaySoundEffect("effect2", 100);
	Obj_SetValue(objEnemyMain,"Boss Name","LeBlanc, the Arrow of Retribution");
	loop(60){
		ObjRender_SetAngleZ(objEnemyMain, angle);
		ObjRender_SetScaleXYZ(objEnemyMain, scalex, scaley, 0);
		angle-=360/60;
		scalex+=0.35/60;
		scaley+=0.25/60;
		yield;
		}
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*50.5);
	SetShotAutoDeleteClip(20, 50, 20, 20);
	wait(75);
	PlaySoundEffect("varusdeclare", 100);
	wait(60);
	loop{
		PlaySoundEffect("shot", 100);
		loop(9){
			TrailShot(GetX, GetY, 3.15, angle, ARROW_RED, 10);
			angle+=360/9;
			}
		wait(45);
		angle+=360/9/2;
		PlaySoundEffect("shot", 100);
		loop(9){
			TrailShot(GetX, GetY, 3.15, angle, ARROW_RED, 10);
			angle+=360/9;
			}
			angle = rand(0, 360);
		wait(180);
		loop(2){
		ConcentrationA4(objEnemyMain, "EARTH", 5, 150, [255, 0, 255]);
		PlaySoundEffect("QCharge", 100);
		Indicator(120);
		loop(60){
			tarx = GetPlayerX;
			tary = GetPlayerY;
			yield;
		}
		wait(30);
		PlaySoundEffect("QFire", 100);
		ascent(i in -1..2){
			ArrowShot(GetX, GetY, 5, AngleBetweenPoints(GetX, GetY, tarx, tary)+(i*55), ARROW_RED, 10);
			}
		wait(30);
		}
		wait(30);
		ExMoveCenter(objEnemyMain, 120, 5, rand(25, 40));
		wait(90);
	}
}

task ArrowShot(x, y, v, angle, graphic, delay){
	let obj = CreateLooseLaserA1(x, y, v, angle, 60, 40, graphic, delay);
	let shotang = 0;
	objbullets = objbullets ~ [obj];
	while(!Obj_IsDeleted(obj)){
		yield;
		}
	ascent(i in 0..length(objbullets)) { if(objbullets[i] == obj) { objbullets = erase(objbullets, i); break; } }
}

task TrailShot(x, y, v, angle, graphic, delay){
	let obj = CreateShot01(x, y, v, angle, graphic, delay);
	ObjShot_SetSpellResist(obj, true);
	RunReflectCode(obj, true, false, true, false);
	let time = 0;
	while(!Obj_IsDeleted(obj)){
		if(time%10==0 && time>10){ PlaySoundEffect("wave", 75); BlightShot(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(-0.06, 0.06), rand(0, 360), S_ORB_PURPLE, 10, time*2); }
		time++;
		yield;
	}
}

task BlightShot(x, y, v, angle, graphic, delay, shotang) {
		let obj = CreateShot01(x, y, v, angle, graphic, delay);
		let tiempo = 0;
		ObjShot_SetSpellResist(obj, true);
		while(!Obj_IsDeleted(obj)) {
			ascent(i in 0..length(objbullets)){
				if(GetDistanceSq(ObjMove_GetX(obj), ObjMove_GetY(obj), ObjMove_GetX(objbullets[i]), ObjMove_GetY(objbullets[i]))<(20^2) && tiempo<480 && ObjMove_GetX(objbullets[i])!=0 && ObjMove_GetY(objbullets[i])!=0){
					PlaySoundEffect("shot2", 85);
					loop(3){
						CreateShot02(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(2.35, 3.35), shotang, -0.1, rand(0.5, 1.1), BL_SMALL_MAGENTA, rand(5, 10));
						shotang+=360/3;
						}
					Obj_Delete(obj);
				}
			}
			tiempo++;
			if(tiempo==480){ ObjShot_FadeDelete(obj); break; } 
			yield;
		}
	}

task Indicator(x){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetModuleDirectory~"IMG/Enemy/Target.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj, 50);
	let angle = 270;
	let scale = 1.25;
	let alpha = 255;
	ascent(i in 0..x){
		if(i<59){
			ObjRender_SetPosition(obj,GetPlayerX, GetPlayerY,0);
			}
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		angle+=4;
		if(scale>0.375){ scale-=0.025; }
		yield;
	}
	loop(30){
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle+=4;
		alpha-=255/30;
		ObjRender_SetAlpha(obj, alpha);
		yield;
		}
	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 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);
}