//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["trails"]
#System["./../../system/NSystem.dnh"]
#Player["./../../player/Reimu by plasmaflame22/Reimu_A.txt", "./../../player/Reimu by plasmaflame22/Reimu_C.txt"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 2700;
let time= 75;
let scene = GetEnemyBossSceneObjectID();

let path = GetCurrentScriptDirectory~"./../../SFX/";
let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 60);
let shot4 = LoadEffect(path~ "LenenShot.WAV", 80);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let whistle = LoadEffect(path~ "Whistle.WAV", 68);
let explosion = LoadEffect(path~ "SmallExplosion.WAV", 95);


@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(time);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	SetPlayerClip(5,5, 415, 435);
    ObjEnemyBossScene_StartSpell(scene);
	
	SetCommonData("non1", "yes");
	SetCommonData("spell", "trails");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/4, 60);
	Main;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 35); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}


task Main{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Ring theory:[r]Unitary trail", objBoss, 1);
    wait(200);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	
	let obj = CreateShotA1(-10,-10, 0,48, 284, 0);
	trail(obj, [30, 6], 60, [0.5, 12], [241,247], 4, 3, 1);
	wait(30);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(60);
	obj = CreateShotA1(width +10,-10, 0,132, 284, 0);
	trail(obj, [30, 6], 60, [0.5, 12], [241,247], 4, 3, 1);
	wait(120); let dir =1;
	ascent(i in 0..3){
		let x = rand(width/5, 2*width/5); if(dir==1){x = rand(3*width/5, 4*width/5);}
		let obj = CreateShotA1(x,-10, 0,45, 284, 0); let ra = rand(7,9); if(i==0){ra=rand(0,3);}
		ObjMove_SetAngle(obj, GetAngleToPlayer(obj) +dir*ra);
		trail(obj, [40, 6], rand(45,70), [1.5, 8], [241,247], 3, 4, 1);
		wait(40); dir *=-1;
	}
	if(GetPlayerX<width/2){ObjMove_SetDestAtFrame(objBoss, width/3 +rand(-30,30), rand(height/4, 0.4*height), 90);}
	else{ObjMove_SetDestAtFrame(objBoss, 2*width/3 +rand(-30,30), rand(height/4, 0.4*height), 90);}
		
	shootStar(320, 60, 160);
	
	ascent(i in 0..3){
	    let x = width/6 + width/3 *i + rand(-10,10);
		ObjSound_Play(shot5);
		let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, 284, 10);
		ObjShot_SetSpellResist(obj, true);
		rocket(obj, x, i);
	}
	
	dir = 1; if(probaRoll(1/2)){dir=-1;}
	wait(200);
	move(objBoss, width/3, 2*width/3, height/4 ,height*0.4, 0, 180, 60, objBoss, 0, scene, 0);
	wait(100);
	let angInc= rand(0.7, 0.85);
	ObjSound_Play(shot5);
	ascent(i in 0..4){
	    let obj = CreateShotA1(X(objBoss)+90*cos(90*i+45), Y(objBoss)+90*sin(90*i+45), 0, 90*i+45, 297, 30);
		ObjShot_SetSpellResist(obj, true);
		control(obj, 100, -angInc*dir, 1);
	}
	wait(60);
	angInc = rand(1,1.2);
	ObjSound_Play(shot5);
	ascent(i in 0..4){
	    let obj = CreateShotA1(X(objBoss)+60*cos(90*i), Y(objBoss)+60*sin(90*i), 0, 90*i, 293, 30);
		ObjShot_SetSpellResist(obj, true);
		control(obj, 100, angInc*dir, 2);
	}	
}

task control(obj, pause, inc, n){
	if(n==1){
		Com(obj, objBoss, inc, 80); 
		wait(pause);
		shotSound(shot4, -1, 70, 4, objBoss, false, 0, scene, 0);
		trail(obj, [0], 50, [0.5,1.4], [263, 264], 1, 0,7);
	}
	else{
	    Com(obj, objBoss, inc, 50); 
		wait(pause);
		trail(obj, [0], 60, [2,2.4], [255, 256], 1, 0, 5);
	}
}
	    


task rocket(obj, x, i){
    ObjMove_SetDestAtFrame(obj, x, height-2, 120);
	wait(140); ObjMove_SetAngle(obj, -90);
	if(i==1){trail(obj, [60, 4], 70, [0.5, 2.2],[241,247], 1, 6, 1);}
	else{trail(obj, [60, 4], 50, [0.7, 2],[241,247], 1, 6, 2);}
}
    


task shootStar(iniDel, delay, pause){
	wait(iniDel);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let dir = -1; if(GetPlayerX < width/2){dir=1;}
		let dir2 = dir;
		loop(3){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let x = rand(width/4, 0.35*width); if(dir==1){x = rand(0.65*width, 3*width/4);}
			let x2 = rand(width/5, 2*width/5); if(dir2== 1){x2 = rand(3*width/5, 4*width/5);}
			let obj = CreateShotA1(x,-10, 0,45, 284, 0);
			ObjShot_SetSpellResist(obj, true);
			let ang = findAngRad(x2, x, height, Y(obj))[0];
			ObjMove_SetAngle(obj, ang);
			trail(obj, [delay, 6], rand(50,70), [1.5, 8], [241,247], 3, 4, 1);
			wait(40); dir*=-1;
		}
		wait(pause);
		loop(3){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let x = rand(width/5, 2*width/5); if(dir==1){x = rand(3*width/5, 4*width/5);}
			let obj = CreateShotA1(x,-10, 0,45, 284, 0);
			ObjShot_SetSpellResist(obj, true);
			ObjMove_SetAngle(obj, GetAngleToPlayer(obj)+rand(-3,3));
			trail(obj, [delay, 6], rand(50,70), [1.5, 8], [241,247], 3, 4, 1);
			wait(40); dir*=-1;
		}
		wait(pause);
	}
}
	 


// delayLaser used for objects not moving yet. First argument is the actual delay, the second is the final speed of the object bullet.

task trail(obj, delayLaser, spread, speed, graphic, numBul, soundDelay, delay){
	explode(obj, 40);
    let s = spread/2; 
	if(delayLaser[0]> 1){
	    DelayLaser(obj, X(obj), Y(obj), ObjMove_GetAngle(obj), 700, 40, 25, delayLaser[0]);
		ObjSound_Play(horn);
		wait(delayLaser[0]); ObjMove_SetSpeed(obj, delayLaser[1]);
	}
	if(soundDelay>0 && ! Obj_IsDeleted(obj)){
	    ObjSound_Play(whistle); 
		sound(obj, soundDelay);
	}
	while(! Obj_IsDeleted(obj)){
	    let ang = ObjMove_GetAngle(obj);
	    loop(numBul){
		    if(Obj_IsDeleted(obj)){return;}
			CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(speed[0], speed[1]), ang+180+rand(-s,s), truncate(rand(graphic[0], graphic[1])), 0);
		}
		wait(delay);
	}
}	

task sound(obj, soundDelay){
	wait(5);
    shotSound(shot2, -1, 70, soundDelay, obj, true, 0, scene, 0);
}

task explode(obj, numBul){
    while(!Obj_IsDeleted(obj) && Y(obj)< height){yield;}
	if(Y(obj)>= height){
		ObjSound_Play(explosion);
	    ScreenShake(15, 8);
	    loop(numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			CreateShotA1(X(obj)+rand(-3,3), Y(obj), rand(0.8, 4), rand(-125, -65), 121, 5);
		}
	}
	Obj_Delete(obj);
}    

	









	
	
	
	
	
	