//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["spinRing2"]
#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 "./../../ZUNShot/shotMathsCons.txt"
#include "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 3400;
let time= 50;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";

let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot3 = LoadEffect(path~ "ATTACK3.WAV", 90);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 70);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let twinkle = LoadEffect(path~ "TWINKLE.WAV", 65);


@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);
	SetPlayerClip(5,5, 415, 435);
	SetCommonData("non1", "yes");
	
	animNon(objBoss, 1);
	TFinalizeNon(scene, objBoss);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	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{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(charge);
    wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	
	move(objBoss, width/3, 2*width/3, height/5, height/4, 0, 240, 60, objBoss, 0, scene, 0);
	ring(100);
	wait(20);
	ObjSound_Play(horn); wait(40);
	shoot(0,[8,10,9], 70);
	let delay = 175;
	wait(delay-40);
	ObjSound_Play(horn);
	wait(40);
	shoot(1,[8,10,9], 70);
}



task ring(pause){
	let dir = 1;
	let col = [[60,62, 72], [61,66, 71], [68, 64,71]]; let len = length(col);
	let j = truncate(rand(0, len));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let colEntry = col[j];
		ObjSound_Play(shot5);
	    ascent(i in -1..2){
		    spinRing(X(objBoss), Y(objBoss), 14, 75, GetAngleToPlayer(objBoss) + 60*i, 4, 6, dir, colEntry[0], 5);
			spinRing(X(objBoss), Y(objBoss), 10, 40, GetAngleToPlayer(objBoss) + 60*i, 4, 8, -dir, colEntry[1], 5);
			spinRing(X(objBoss), Y(objBoss), 6, 20, GetAngleToPlayer(objBoss) + 60*i, 4, 10, dir, colEntry[2], 5);
		}
		wait(pause); dir*=-1; j= (j+ truncate(rand(0, len)))% len;
	}
}




task shoot(n, numBul, delay){
	ObjSound_Play(shot4);
	let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, 453, 0);
	ObjShot_SetSpellResist(obj, true); let x= width/4; if(n==1){x= 3*width/4;} let y;
	ObjMove_SetDestAtFrame(obj, x, 0.35*height, 30);
	wait(30);
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 60));
	wait(60);
	if(n==0){move(obj, width/5, 0.4*width, 0.25*height, 0.42*height, 0, 120, 60, objBoss, 0, scene, 0);}
	else{move(obj, 0.6*width, 4*width/5, 0.25*height, 0.42*height, 0, 120, 60, objBoss, 0, scene, 0);}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjSound_Play(twinkle);
		ascent(j in 0..3){
		    let ang = rand(0,360);
			ascent(i in 0..numBul[j]){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(obj); return;}
				x = X(obj) + 10*cos(ang+ 360/numBul[j] *i); y = Y(obj) + 10* sin(ang +360/numBul[j] *i);
				CreateShotA1(x,y, 3-0.8*j, ang+ 360/numBul[j] *i, 57, 5);
			}
		}
		wait(delay);
	}
	Obj_Delete(obj);
	
}
    
