//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["roseCircleNon"]
#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 = 3700;
let time= 55;
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", 60);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);

@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{
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60);
	NotifyEventAll(EV_USER_STAGE, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(charge);
    wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	
	let theta=rand(0,90);
	rose(theta, width/2, 0.35*height, 120, [1.2, 2], [452, 250], 7, -13);
	theta+=720;
	rose(theta, width/2, 0.35*height, 120, [1.2, 2], [452, 250], -7, 11);
	
	move(objBoss, width/2-50, width/2+50, height/5, height/3 , 120, 180, 60, objBoss, 0, scene, 0);
	
	wait(80); let numBul = 8;
	ObjSound_Play(horn);
	wait(60); let dir = 1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let ang = GetAngleToPlayer(objBoss); let t=0;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		shotSound(shot3, 10, 40, 2, objBoss, false, 0, scene, 0);
		while(t<10){
			ascent(i in 0..numBul){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				CreateShotA1(X(objBoss), Y(objBoss), 2.5-0.05*t, ang- dir*10 + 360/numBul *i +1.5*t*dir, 43, 5);
			}
			t++; yield;
		}
		wait(10); t=0;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		shotSound(shot3, 10, 40, 2, objBoss, false, 0, scene, 0);
		while(t<10){
			ascent(i in 0..numBul){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj = CreateShotA1(X(objBoss), Y(objBoss), 2.5-0.05*t, ang+10*dir + 360/numBul *i -1.5*t*dir, 56, 5); 
			}
			t++; yield;
		}
		wait(100); dir*=-1;
	}
	
}

//task shotSound(obj, time, volume, delay, enemy, delete, limit, scene, timeLimit){


task rose(theta, xini, yini, rad, speed, graphic, incAng, inc){
	let x = xini + rad *sin(3/4 *theta)*cos(theta); 
	let y = yini + rad *sin(3/4 *theta)*sin(theta); 
	let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic[0], 0);
	ObjSound_Play(shot5);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtFrame(obj, x, y, 30);
	wait(30);
	ObjSound_SetVolumeRate(shot3, 60); ObjSound_Play(shot3);
	wait(60);
	let t= rand(0,90);
	sound;
	shoot(obj, 190, t, graphic[1], speed, 1, inc);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(obj); return;}
	    x= xini + rad *sin(3/4 *theta)*cos(theta);
		y = yini + rad *sin(3/4 *theta)*sin(theta);
		ObjMove_SetPosition(obj, x,y);
		theta+= incAng; yield;
	}
	Obj_Delete(obj);
}

task sound{
    wait(10);
	shotSound(shot4, -1, 45, 4, objBoss, false, 0, scene, 0);
}

task shoot(obj, frame, t, graphic, speed, spread, inc){
    let i=0; wait(10);
    while(! Obj_IsDeleted(obj)){
	    if(Obj_IsDeleted(obj)){return;}
	    let obja = CreateShotA1(X(obj), Y(obj), 0 ,t+rand(-2,2), graphic, 10);
		ObjMove_AddPatternA1(obja, frame, rand(speed[0], speed[1]), NO_CHANGE);
		t+= inc; wait(2);
		i++; //if(i%2 ==0){yield;}
	}
}
	



