//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["2-rose non"]
#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 = 5200;
let limit1= 0.7*life;
let limit2 = 0.4*life;
let time= 70;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";

let charge = LoadEffect(path~ "Charge.WAV", 80);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let shot4 = LoadEffect(path~ "ATTACK4.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", "no");
	
	animNon(objBoss, 1);
	TFinalizeNon(scene, objBoss);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	ObjMove_SetPosition(objBoss, width/3, -80);
	
	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(180);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(charge);
    wait(60);
	ObjSound_Play(shot5);
	rose(45, width/2, 0.34*height, 120, [1.2,1.6], [452, 47], 2, [-100,-94], 1);
	rose(228, width/2, 0.34*height, 120, [1.2,1.6], [452, 47], -2, [92,100], 1);
	wait(60);
	shotSound(shot4, -1, 45, 4, objBoss, false, limit1, scene, 2*time/3);
	wait(80);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit1 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 2*time/3){yield;}
	
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	wait(5);
	ObjSound_Play(shot5);
	shotSound(shot4, 60, 45, 4, objBoss, false, 0, scene, 0);
	ellipse(110, 1, width/2, 0.34*height, [110, 60], [1.7,1.9], [450, 43], -2, [60,65], 2);
	wait(60);
	shotSound(shot4, -1, 45, 3, objBoss, false, limit2, scene, 0.4*time);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit2 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time*0.4){yield;}

	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	wait(5);
	ObjSound_Play(shot5);
	shotSound(shot4, 60, 45, 3, objBoss, false, 0, scene, 0);
	ellipse(-70, 0, width/2, 0.34*height, [110, 60], [2.2, 2.5], [451, 51], 2, [-98,-90], 2);
	wait(60);
	shotSound(shot4, -1, 45, 2, objBoss, false, 0, scene, 0);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
}



task ellipse(angIni, n, xini, yini, rad, speed, graphic, incAng, inc, delay){
	let theta=angIni; let bigRad= rad[0]; let littleRad = rad[1];
	let x = xini + bigRad *cos(theta);
	let y = yini + littleRad *sin(theta); 
	if(n==1){x = xini + littleRad *cos(theta); y = yini + bigRad *sin(theta); }
	let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic[0], 0);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtFrame(obj, x, y, 30);
	wait(60);
	let t= rand(0,90);
	shoot(obj, 160, t, graphic[1], 2, speed, inc, delay);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(n==0){x = xini + bigRad *cos(theta); y = yini + littleRad *sin(theta);}
		else{x = xini + littleRad *cos(theta); y = yini + bigRad *sin(theta); }		
		ObjMove_SetPosition(obj, x,y);
		theta+= incAng; yield;
	}
}



task rose(theta, xini, yini, rad, speed, graphic, incAng, inc, delay){
	let x = xini + rad *sin(2*theta)*cos(theta); 
	let y = yini + rad *sin(2*theta)*sin(theta); 
	let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic[0], 0);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtFrame(obj, x, y, 30);
	wait(60);
	let t= rand(0,90);
	shoot(obj, 160, t, graphic[1], 1, speed, inc, delay);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    x= xini + rad *sin(2*theta)*cos(theta);
		y = yini + rad *sin(2*theta)*sin(theta);
		ObjMove_SetPosition(obj, x,y);
		theta+= incAng; yield;
	}
}

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





