//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["fallingNon"]
#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 = 4200;
let time= 60;
let pause = 180;
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/3, 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);
    wait(60); let las = LoadEffect(path~"LASER2.WAV", 60);
	helices(pause);
	wait(380+3*pause-60); ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 50); 
	let colLas = [27,28,29,31,33, 36,37,38,39]; let colPellets = [104,103,102, 100, 98,96,94,93,92]; let col;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    col = floor(rand(0,9)); ObjSound_Play(las);
	    ascent(i in 0..2){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 10, 215+22*i +rand(0,20), 30,40, colLas[col],0);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			spawn(obj,4, colPellets[col], 0.05, 0.2, 3.2, 3.8);
		}
		wait(10);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} ObjSound_Play(las);
		let obja= CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 10, 215+22*2 +rand(0,20), 30,40, colLas[col],0);
		ObjRender_SetBlendType(obja, BLEND_ADD_RGB);
		spawn(obja,3, colPellets[col], 0.05, 0.2, 3,3.5);
		wait(10);
		ObjSound_Play(las);
		ascent(i in 3..5){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateCurveLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 10, 215+22*i +rand(0,20), 30,40, colLas[col],0);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			spawn(obj,3, colPellets[col], 0.05, 0.2, 3.2,3.8);
		}
		wait(10);
	}
}



task helices(pause){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    hel(0); hel(2);
		wait(120);
		wait(pause);
		hel(0); hel(1);
		wait(120);
		wait(pause);
		hel(1); hel(2);
		wait(120);
		wait(pause);
	}
}

		
task hel(n){
    warn(n);
    wait(120); let shot = LoadEffect(path~"ATTACK4.WAV", 80); let shot2 = LoadEffect(path~"ATTACK2.WAV", 80);
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 90));
	wait(60);
	shotSound(shot, 140, 60, 3, objBoss, false, 0, scene, 0);
	shotSound(shot2, 140, 80, 4, objBoss, false, 0, scene, 0);
	helix(width/6 +n* width/3, 0, 10, 90, width/6, width/4, 40,3, 271,271, objBoss, 0);
    helix(width/6 +n* width/3, 0, 8, 90, width/8, width/3, 36,4, 200,200, objBoss, 0);
	helix(width/6 +n* width/3, 0, 6, 90, width/10, width/5, 22,5, 275,275, objBoss, 0);
}


task spawn(obj,numBul, graphic, accelmin, accelmax, maxspeed1, maxspeed2){
    while(ObjMove_GetY(obj)>0){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK5.WAV", 60));
	loop(numBul){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    CreateShotA2(ObjMove_GetX(obj), 0, 0, 90+rand(-3,3), rand(accelmin, accelmax), rand(maxspeed1, maxspeed2), graphic, 3);
	}
}



task warn(n){
    let objText = ObjText_Create();
	ObjText_SetText(objText,"WARNING!");
	ObjText_SetFontSize(objText, 30);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontColorTop(objText, 255, 165, 30);
	ObjText_SetFontColorBottom(objText, 255, 165, 30);
	ObjText_SetFontBold(objText, true);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 130);
	Obj_SetRenderPriorityI(objText, 70);
	
	ObjRender_SetPosition(objText, width/6 +width/3 *n -65, 400, 1);
	let alpha = 95;
	loop(40){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(objText); return;}
		ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;
	}
	loop(40){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(objText); return;}
		ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;
	}
	loop(40){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(objText); return;}
		ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;
	}
	loop(60){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(objText); return;}
		ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;
	}
	
	Obj_Delete(objText);
}
	
	
	
	
	