//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Non laser"]
#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 "./../sharedFuncSaul.dnh"


let objBoss;
let width = 444;
let height = 448;
let life = 5000;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";


@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);
	SetCommonData("non1", "yes");
	
	animNon(objBoss, 1);
	TFinalizeNon(scene, objBoss);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 60);
	Main;
}

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



task Main{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	bulletLas(6, 0.8, 60, 180, 180, 1, life-2400, time/2);
	wait(60);
	spir(life-2400);
	ObjEnemy_SetDamageRate(objBoss, 100, 50);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life-2400 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 0);
	ObjEnemy_SetDamageRate(objBoss, 100, 50);
	move(width/4, 3*width/4, height/5, 2*height/5, 60, 180, 240, objBoss, 0, scene, 0);
	otherBul;
	bulletLas(7,0.8,60,120,180, 2, 0, 0);
}


task otherBul{
    let dir = 1; let ang =0; let shot = LoadEffect(path~"ATTACK4.WAV", 80); shotSound(shot, -1, 60, 6, objBoss, false, 0, scene, 0);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ascent(i in 0..2){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4.8, -80 +20*i+ang, 125,0);
			ObjShot_SetSpellResist(obj, true);
		}
		ascent(i in 0..2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4.8, 260 -20*i-ang, 125,0);
			ObjShot_SetSpellResist(obj, true);
		}
	    ascent(i in 0..2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4.8, -30+30*i +ang, 125,0);
			ObjShot_SetSpellResist(obj, true);
		}
		ascent(i in 0..2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4.8, 210-30*i -ang, 125,0);
			ObjShot_SetSpellResist(obj, true);
		}
		ang+= 8*dir; if(absolute(ang)>=50){dir*=-1;}
		wait(6);
	}
}



task spir(hp){
    let ang; let dir=-1; let t=0; let shot = LoadEffect(path~"ATTACK4.WAV", 60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>hp){
	    ang= GetAngleToPlayer(objBoss) - dir*rand(30, 40);
		shotSound(shot, 405, 60, 4,objBoss, false, hp, scene, time/2);
		while(t<405){
			ascent(i in 0..4){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= hp || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=time/2){return;}
			    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 9, ang+ 90*i, 332,0);
				ObjShot_SetSpellResist(obj, true);
			}
			wait(3); ang+= 9/4 *dir; t+=3;
		}
		dir*=-1; t=0; wait(60);
	}
}


task bulletLas(numBranches, speed, pause, initialDelay, rotationQuantity, cas, hp, timeLimit){
    let dir=1; let ang; let lasID=[];
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>hp){
	    ObjSound_Play(LoadEffect(path~"Horn.WAV", 90));
		ang = rand(0,360);
		ascent(i in 0..numBranches){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=hp || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLimit){return;}
		    let las = CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ang+360/numBranches *i, 500, 60, 0, 16, rotationQuantity/speed + initialDelay); 
			lasID = lasID~[las];
			conLas(las, ang, dir, speed, initialDelay, rotationQuantity, hp);
			intersect(las, i, initialDelay, cas, hp);
		}
		sound(initialDelay,rotationQuantity, speed, lasID, hp);
	    wait(initialDelay+ rotationQuantity/speed +pause); dir*=-1;
	}
}


task sound(initialDelay, rotationQuantity, speed, las, hp){
    wait(initialDelay-60); let del = [];
	ascent(i in 0.. length(las)){del = del~ [Obj_IsDeleted(las[i])];}
	if(!arrayAllTrue(del)){ObjSound_Play(LoadEffect(path~ "Horn.WAV", 90));}
	wait(60);
	LaserSound(rotationQuantity/speed , las, objBoss, hp);
}


task conLas(las, ang, dir, speed, initialDelay, rotationQuantity, hp){
    let t=0; let x; let y;
	while((t< rotationQuantity/speed + initialDelay -1) && !Obj_IsDeleted(las)){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= hp){Obj_Delete(las); return;}
	    x = ObjMove_GetX(objBoss); y =ObjMove_GetY(objBoss);
	    ObjMove_SetPosition(las, x, y);
	    if(t>=initialDelay){ObjStLaser_SetAngle(las, ObjStLaser_GetAngle(las)+ dir* speed);}
		yield; t++;
	}
	Obj_Delete(las);
}


task intersect(las, i, initialDelay, cas, hp){
	wait(initialDelay);
	ObjShot_SetGraphic(las,2+2*i);
    while(!Obj_IsDeleted(las)){
		if(absolute(GetAngleToPlayer(objBoss)-ObjStLaser_GetAngle(las)) %360<3){
			shoot(las,i,cas, hp); 
			Obj_Delete(las);
		}
		yield;
	}
}

task shoot(las,i,cas, hp){
	let ang = ObjStLaser_GetAngle(las); let col= 43 +2*i; ObjSound_Play(LoadEffect(path~ "ATTACK5.WAV", 80));
	alternative(cas)
	case(1){
	    loop(8){
			loop(11){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= hp || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=time/2){return;}
			    CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(0.1, 3), ang+rand(-15,15), col,0);
			}
			yield;
		}
	}
	case(2){
		loop(8){
			loop(7){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= hp){return;}
			    CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(0.2, 4), ang+rand(-16,16), col,0);
			}
			yield;
		}
	}
}

