//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["spinRing"]
#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 "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 4000;
let limit = 0.35*life;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";

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


@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/4 ,0.4*height, 0, 140, 30, objBoss, limit, scene, 0);

	circles(14, 25, limit); 
	let dir=1; let numBul = 5; 
	let col = [75, 78, 82, 84,88]; 
	let len = length(col);
	let ang = rand(0,360);
	let j = truncate(rand(0, len)); let i=0; let dira=1;
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){
		if(i%4 == 0){ang= GetAngleToPlayer(objBoss); dira*=-1;}
		ascent(i in 0..numBul){ 
			spinRing(X(objBoss), Y(objBoss), 10, 30, ang + 360/numBul *i, 3.2, rand(6,10), 1*dir, col[j], 10);
		}
		
		wait(25); dir*=-1; ang+= dira*rand(30,45); j= (j+truncate(rand(1,len)))%len; i++;
	}
	yield;
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjMove_SetDestAtFrame(objBoss, width/2, 0.2*height, 40);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path~"CAUTIONSHORT.WAV", 100));
		
	wait(120);
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	ObjSound_Play(LoadEffect(path~"LenenBomb.WAV", 100));
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	move(objBoss, width/3, 2*width/3, 0.25*height,0.32* height, 60, 180, 60, objBoss, 0, scene, 0); 
	col = [[76,78], [78,84], [82,88], [75,81]];
	len = length(col);
	j = truncate(rand(0, len));
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(shot5);
	    let colEntry = col[j];
		let first = 0; if(probaRoll(0.5)){first=1;}
	    ascent(i in -2..3){
		    spinRing(X(objBoss), Y(objBoss), 10, 44, GetAngleToPlayer(objBoss) + 55*i, 4.8, 8, 1, colEntry[first], 5);
			spinRing(X(objBoss), Y(objBoss), 6, 23, GetAngleToPlayer(objBoss) + 55*i, 4.8, 9, -1, colEntry[(first+1)%2], 5);
		}
		wait(25);
		j= (j+truncate(rand(1,len)))% len;
		ObjSound_Play(shot5);
		colEntry = col[truncate(rand(0, length(col)))];
		first = 0; if(probaRoll(0.5)){first=1;}
		let ra = GetAngleToPlayer(objBoss)+ 30 +rand(-15,15);
		ascent(i in 0..6){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let spe = rand(4, 4.3); 
		    spinRing(X(objBoss), Y(objBoss), 10, 54, ra + 60*i, spe, 8, 1, colEntry[first], 5);
			spinRing(X(objBoss), Y(objBoss), 6, 26, ra + 60*i, spe, 9, -1, colEntry[(first+1)%2], 5);
		}
		wait(30);
		j= (j+truncate(rand(1,len)))%len;
	}
}

task circles(numBul, delay, limit){
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){
	    ObjSound_Play(shot5);
	    let ang = rand(0,360);
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit){return;}
		    CreateShotA1(X(objBoss), Y(objBoss), rand(1.4,2.8), ang+ 360/numBul *i, 200, 10);
		}
		wait(delay);
	}
}



