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


let objBoss;
let width = 444;
let height = 448;
let life = 4000;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let shot4 = LoadEffect(path~"ATTACK4.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);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Dave");
	
	animSpell(objBoss, 2);
	TFinalizeSpell(scene, objBoss, true);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/3, 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{
    Name(2, objBoss);
    Indicator(objBoss, 2);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Chaos sign: Divine vortex", objBoss, 2);
    wait(240);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"POWER UP.WAV", 80));
    wait(120);
	let dir=1; if(rand(0,1)>1/2){dir=-1;}
	let shot5 = LoadEffect(path~"ATTACK5.WAV", 80);
	ObjSound_Play(shot5);
	figure(10,7, 22, dir);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> life/2  && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/2){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	wait(60);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> 0){
	    ObjSound_Play(shot5);
	    ascent(i in 0..3){
		    let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1, GetAngleToPlayer(objBoss)+120*i, 357, 0);
			knife(obj);
		}
		wait(180);
	}
}

task knife(obj){
    while(!Obj_IsDeleted(obj)){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot4);
	    CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(0.6,1), rand(0,360), 329, 10);
		wait(20);
	}
}

	    

task figure(numBranch, bulPerBranch, angIni, dir){
    let obja = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 356, 0); ObjShot_SetSpellResist(obja, true);
	ObjMove_SetDestAtFrame(obja, width/2, height/2, 120);
    ascent(j in 0..numBranch){
		ascent(i in 0..bulPerBranch){
			let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 360/numBranch *j+ angIni, 317, 0);
			ObjShot_SetSpellResist(obj, true);
			rotat(obj, numBranch, bulPerBranch, 360/numBranch *j+angIni, i, dir);
		}
	}
}


task rotat(obj, numBranch, bulPerBranch, angIni, i, dir){
    let shot = LoadEffect(path~"ATTACK2.WAV", 70);
    ObjMove_SetDestAtFrame(obj, width/2+ (35+(440/(2*bulPerBranch+1))*i +rand(-10,10))*cos(angIni), height/2+ (35+(440/(2*bulPerBranch+1))*i +rand(-10,10))*sin(angIni), 120);
	wait(120);
	let r = findAngRad(width/2, ObjMove_GetX(obj), height/2, ObjMove_GetY(obj))[1]; wait(120); let t=0;
	shotSound(shot, -1, 75, 3, objBoss, false, 0, scene, 0);
	if(i ==bulPerBranch-1){shoot(obj);}
	wait(60);
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 80));
	wait(60);
	loop(120){
		if(i!= bulPerBranch-1){
			ObjMove_SetPosition(obj, width/2+r*cos(angIni +2*t/(1+i/2)), height/2+ r*sin(angIni+2*t/(1+i/2)));
			ObjMove_SetAngle(obj,angIni+2*t/(1+i/2)); t+=dir/2;
		}
		yield;
	}
	loop(120){
		if(i!= bulPerBranch-1){
			ObjMove_SetPosition(obj, width/2+r*cos(angIni +2*t/(1+i/2)), height/2+ r*sin(angIni+2*t/(1+i/2)));
			ObjMove_SetAngle(obj,angIni+2*t/(1+i/2)); t+=dir;
		}
		yield;
	}
	while(!Obj_IsDeleted(obj)){
	    if(i == bulPerBranch-1){
		    ObjMove_SetPosition(obj, width/2+r*cos(angIni +t), height/2+ r*sin(angIni+t));
			ObjMove_SetAngle(obj,angIni+t); t+= dir; yield;
		}
		else{
			ObjMove_SetPosition(obj, width/2+r*cos(angIni +2*t/(1+i/2)), height/2+ r*sin(angIni+2*t/(1+i/2)));
			ObjMove_SetAngle(obj,angIni+2*t/(1+i/2)); t+=dir; yield;
		}
	}
}


task shoot(obj){
	while(!Obj_IsDeleted(obj)){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    let ang= ObjMove_GetAngle(obj);
		let obja = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(0.5,1), ang, 324, 0);
		yield; yield;
	}
}

