//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Non helix"]
#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 = 4000;
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/4, 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);
	let shot = LoadEffect(path~"ATTACK4.WAV", 80); let shot2 = LoadEffect(path~"ATTACK2.WAV", 80); let wave = LoadEffect(path~"Wave.WAV", 80);
    ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(70);
	move(width/4, 3*width/4, 3*height/10, 0.45*height, 180, 180, 240, objBoss, 0, scene, 0);
	otherBul;
	wait(180);
	bulletLas(6,0.6,60,60,180, 0);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0,0, 362,0); ObjShot_SetSpellResist(obj, true);
		ObjSound_Play(wave);
		wait(60);
		ascent(i in 0..4){
		    helix(ObjMove_GetX(obj), ObjMove_GetY(obj), 8, GetAngleToPlayer(obj) +90*i, 40, 50, 10, 6,200,200, objBoss,0);
		}
		shotSound(shot, 90, 60, 3, objBoss, false, 0, scene, 0);
	    shotSound(shot2, 60, 80, 5, objBoss, false, 0, scene, 0);
		helix(ObjMove_GetX(obj), ObjMove_GetY(obj), 11, GetAngleToPlayer(obj), 60, 70, 30, 3,269,269, objBoss,0);
		wait(100);
		Obj_Delete(obj);
		wait(30);
	}
}

task otherBul{
    let shot = LoadEffect(path~"TWINKLE.WAV", 60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot);
	    ascent(i in 0..7){
		    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4, 180+24*i, 188,0);
			wavy(obj, 4, 10); ObjShot_SetSpellResist(obj, true);
		}
		wait(6);
	}
}


task bulletLas(numBranches, speed, pause, initialDelay, rotationQuantity, hp){
    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)<=0){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, 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, 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, hp); 
			Obj_Delete(las);
		}
		yield;
	}
}

task shoot(las,i, hp){
	let ang = ObjStLaser_GetAngle(las); let col= 43 +2*i;
	ObjSound_Play(LoadEffect(path~ "ATTACK5.WAV", 80));
	loop(10){
	    loop(6){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= hp){return;}
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(0.1, 5), ang+rand(-15,15), col,0);
		}
		yield;
	}
}



