//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Knives"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 6400;
let time= 62;
let scene = GetEnemyBossSceneObjectID();

@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);
	TFinalizeNon(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, rand(3*height/10, 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{
    StopSlow(TARGET_ALL);
    Name(1, objBoss);
    SetForbidPlayerShot(false);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
    wait(60); 
	let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK5.WAV", 70);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Charge.WAV", 80));
	wait(30);
	ObjEnemy_SetDamageRate(objBoss, 100, 50);
    
    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(40, height/5);
		ObjMove_SetDestAtFrame(objBoss, xini, yini, 30);
		wait(30);
		let xdest = rand(0.7*width, 85*width/100); let ydest= rand(3*height/5, 0.75*height);
		knives(xdest, ydest, 0, 10, col, 1);
		wait(60);
		
		xdest = rand(0.7*width, 85*width/100); ydest= rand(40, 0.2*height);
		knives(xdest, ydest, 1, 8, col, 1);
		wait(60);
		
		xdest = rand(0.15*width, 0.3*width);  ydest= rand(3*height/5, 0.75*height);
		knives(xdest, ydest, 2, 9, col, 1);
		wait(60);
		
		knives(xini, yini, 3, 10, col, 1);
		wait(60);
		
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), rand(height/5, 2*height/5), 120);
		wait(120); col++;
		
		xini = rand(0.7*width, 0.85*width); yini = rand(40, height/5);
		ObjMove_SetDestAtFrame(objBoss, xini, yini, 120);
		wait(120);
		xdest = rand(0.15*width, 0.3*width);  ydest= rand(3*height/5, 0.75*height);
		knives(xdest, ydest, 0, 11, col, 2);
		wait(60);
		
		xdest = rand(0.15*width, 0.3*width); ydest= rand(40, height/5);
		knives(xdest, ydest, 1, 9, col, 2);
		wait(60);
		
		xdest = rand(0.7*width, 0.85*width); ydest= rand(3*height/5, 0.75*height);
		knives(xdest, ydest, 2, 9, col, 2);
		wait(60);
		
		knives(xini, yini, 3, 11, col, 2);
		wait(60);
		
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), rand(height/5, 2*height/5), 120);
		wait(250); col++;
		
		wait(60);
		let dir=1;
		if(rand(0,1)>1/2){dir=-1;}
		ObjMove_SetDestAtFrame(objBoss, width/2 - dir*width/4 + dir* rand(0, 40), rand(height/5, 2*height/5), 60);
		wait(80);
		shotSound(shot2, 120, 70, 4, objBoss, false, 0);
		ascent(i in 0..4){spiral(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, 90-110*dir +30*dir*i, 3, 3*dir, 0, 120, 1, 0, 1.5, 0.004, 0.04, 2.8, 0, 0, 325 +2*i, objBoss, 0);}
		wait(80);
		ObjMove_SetDestAtFrame(objBoss, width/2 - dir*width/4 + dir* rand(0, 40), rand(height/5, 2*height/5), 60);
		wait(80);
		shotSound(shot2, 120, 70, 4, objBoss, false, 0);
		ascent(i in 0..4){spiral(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, 90+110 *dir -dir*30*i, 3, -3*dir, 0, 120, 1, 0, 1.5, 0.004, 0.04, 2.8, 0, 0, 325 +2*i, objBoss, 0);}
		wait(240);
	}
}



task knives(xdest, ydest, n, num, col, phas){
    let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 60);
	let slash = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/swordFlash.WAV", 80);
	let refpoint = width*0.3; if(phas==1){refpoint = 0.7*width;}
    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(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80));
	wait(30);
	ObjSound_Play(slash);
	ObjMove_SetDestAtFrame(objBoss, xdest, ydest, 5);
	shotSound(shot2, 30, 70, 1, objBoss, false, 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.25){
			    let ang = rand(-60, 240);
			    let obj=CreateShotA1(x+ rad*(num*j+k)/(num*30) *cos(angle), y+ rad*(num*j+k)/(num*30) *sin(angle), 0, rand(-40,220), col1, 10);
				ObjMove_AddPatternA2(obj, 200-n*j, 0, NO_CHANGE, rand(0.01,0.02), 0, rand(1.5,2));
			}
			else{
			    let ref = CreateShotA1(x+ rad*(num*j+k)/(num*30) *cos(angle), y+ rad*(num*j+k)/(num*30) *sin(angle), 0, 0, col2, 10); Obj_SetVisible(ref, false); 
				let ang = findAngRad(refpoint, ObjMove_GetX(ref), 440, ObjMove_GetY(ref))[0]; Obj_Delete(ref);
			    let obj=CreateShotA1(x+ rad*(num*j+k)/(num*30) *cos(angle), y+ rad*(num*j+k)/(num*30) *sin(angle), 0, ang +rand(15, 345), col2, 10);
				ObjMove_AddPatternA2(obj, 200-n*j, 0, NO_CHANGE, rand(0.005,0.01), 0, rand(1,1.5));
			}
		}
		yield;
	}
	wait(15);
}

task sound{wait(200); ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80));}
    