//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["DaveNon"]
#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 = 3600;
let time= 55;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let shot= LoadEffect(path~"ATTACK4.WAV", 70);
let shot2= LoadEffect(path~"ATTACK5.WAV", 80);
let slash = LoadEffect(path~"swordFlash.WAV", 100);


@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", "no");
	
	animNon(objBoss, 2);
	TFinalizeNon(scene, objBoss);
	
	ObjMove_SetPosition(objBoss, -60, height/3);
	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(2, objBoss);
    Indicator(objBoss, 2);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 20, 0);
	wait(120);
	dam;
    let col= 0; if(rand(0,1)>=1/2){col=1;} 
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	
	    let xini = rand(0.15*width, 0.3*width); let yini = rand(60, height/5);
		ObjMove_SetDestAtFrame(objBoss, xini, yini, 30);
		wait(30);
		let xdest = rand(0.7*width, 0.85*width); let ydest= rand(60, height/5);
		knives(xdest, ydest, 0, 8, col);
		wait(60);
		
		xdest = rand(0.15*width, 0.3*width); ydest= rand(height/2, 0.6*height);
		knives(xdest, ydest, 1, 8, col);
		wait(60);
		
		xdest = rand(0.7*width, 0.85*width);  ydest= rand(height/2, 0.6*height);
		knives(xdest, ydest, 2, 8, col);
		wait(60);
		
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), rand(height/3, 0.45*height), 120);
		wait(300); col++; ObjSound_Play(LoadEffect(path~"Charge.WAV", 90));
		wait(60);
		
		shotSound(shot2, 300, 50, 12, objBoss, false, 0, scene, 0);
		ObjMove_SetDestAtFrame(objBoss, rand(width/3, 2*width/5), rand(height/3, 2*height/5), 180);
		breakNon;
		wait(360);
		
		xini = rand(0.7*width, 0.85*width); yini = rand(60, height/5);
		ObjMove_SetDestAtFrame(objBoss, xini, yini, 30);
		wait(30);
		xdest = rand(0.15*width, 0.3*width); ydest= rand(60, height/5);
		knives(xdest, ydest, 0, 8, col);
		wait(60);
		
		xdest = rand(0.7*width, 0.85*width); ydest= rand(height/2, 0.6*height);
		knives(xdest, ydest, 1, 8, col);
		wait(60);
		
		xdest = rand(0.15*width, 0.3*width);  ydest= rand(height/2, 0.6*height);
		knives(xdest, ydest, 2, 8, col);
		wait(60);
		
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), rand(height/3, 0.45*height), 120);
		wait(300); col++; ObjSound_Play(LoadEffect(path~"Charge.WAV", 90));
		wait(60);
		
		shotSound(shot2, 300, 60, 12, objBoss, false, 0, scene, 0);
		ObjMove_SetDestAtFrame(objBoss, rand(3*width/5, 2*width/3), rand(height/3, 2*height/5), 180);
		breakNon;
		wait(360);
	}
}

task dam{
    wait(180);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
}


task breakNon{
	let t=0; let ang= rand(0,360);
	while(t<300){
		ascent(i in 0..6){
			ascent(j in 0..2){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj =CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(3,4), ang +rand(-3,3)+60*i, rand(-0.05, -0.02), rand(1, 1.2), rand(43,56), 0);}
		}
		ascent(i in 0..6){
			ascent(j in 0..2){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj =CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(3,4), -ang+rand(-3,3) +60*i, rand(-0.05, -0.02), rand(1, 1.2), rand(43,56), 0);}
		}
		wait(12); ang+=12; t+=12;
	}
}




task knives(xdest, ydest, n, num, col){
    let x= ObjMove_GetX(objBoss); let y =ObjMove_GetY(objBoss);
	let col1=325; let col2= 327; if(col%2==0){col1=329; col2= 326;}
	let angle = findAngRad(xdest, x, ydest, y)[0]; let rad= findAngRad(xdest, x, ydest, y)[1];
	DelayLaser(x,y, angle, rad, 25, 22, 40);
	ObjSound_Play(LoadEffect(path~ "Horn.WAV", 90));
	wait(30);
	ObjSound_Play(slash);
	ObjMove_SetDestAtFrame(objBoss, xdest, ydest, 5);
	shotSound(shot2, 30, 70, 1, objBoss, false, 0, scene, 0);
	sound;
	ascent(j in 0..30){
		ascent(k in 0..num){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    if(rand(0,1)<=0.28){
			    let r = floor(4*rand(0,1))*90;
			    let obj=CreateShotA1(x+ rad*(num*j+k)/(num*30) *cos(angle), y+ rad*(num*j+k)/(num*30) *sin(angle), 0, r+rand(-25,25), col1, 10);
			    ObjMove_AddPatternA2(obj, 150-n*j, 0, NO_CHANGE, rand(0.01,0.02), 0, rand(0.8,1.4));
			}
			else{
			    let ang = rand(120, 420);
			    let obj=CreateShotA1(x+ rad*(num*j+k)/(num*30) *cos(angle), y+ rad*(num*j+k)/(num*30) *sin(angle), 0, ang, col2, 10);
				ObjMove_AddPatternA2(obj, 150-n*j, 0, NO_CHANGE, rand(0.01,0.02), 0, rand(1.4,2));
			}
		}
		yield;
	}
	wait(15);
}

task sound{wait(150); ObjSound_Play(LoadEffect(path~ "Wave.WAV", 90));}
	
	