//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Hourglass (last spell)"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 9000;
let time= 120;
let scene = GetEnemyBossSceneObjectID();


@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);
	
	
	animSpell(objBoss,1);
	TFinalizeSpell(scene, objBoss, false, true);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Hourglass");
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 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);
    Name(1, objBoss);
    Indicator(objBoss, 1);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/SPELLCARD.WAV", 100));
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTIONSHORT.WAV", 100));
	let shot3 =  LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK3.WAV", 100);
	Spell("Chaos sign:[r]The end of time", objBoss, 1);
    wait(240);

	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	let mul =1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0.85*life && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 0.85*time){
	    addendum(mul, 2, true);
		wait(200);
	}
	ObjSound_Play(shot3);
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	wait(25);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0.7*life && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 0.7*time){
	    addendum(mul,2, false);
		laser;
		wait(300);
	}
	ObjSound_Play(shot3);
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	wait(25);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0.4*life && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 0.4*time){
	    addendum(mul,2, false);
		rose(mul);
		laser;
		wait(300);
		mul*=-1;
	}
	ObjSound_Play(shot3);
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	wait(25);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    rose(mul);
		laser;
		addendum(mul,1, false); addendum(mul,2, false);
		mul*=-1;
		wait(300);
	}
}



task rose(mul){  
	let theta=0;
	let r; let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 70);
	let i=0;
	shotSound(shot2, 90, 60, 1, objBoss, false, 0);
	loop(60){	
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		r = 150* sin(2*theta);
		let obj =CreateShotA1(r*mul*cos(theta) + width/2, r*mul*sin(theta) + height/2, 0, theta, 76, 3);
		ObjShot_SetSpellResist(obj, true);
		ObjMove_AddPatternA1(obj,100, 1.5, NO_CHANGE);
		theta+=360/60;
		yield;
	}		
}


task addendum(mul, num, delay){
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/TWINKLE.WAV", 80);
	let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK5.WAV", 70);
	let theta=0;
	let r;
	let i =0;
	if(num==1){
	    shotSound(shot, 60, 80, 2, objBoss, false, 0); let n;
	    loop(40){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			r = 100* cos(5*theta);
			let obj =CreateShotA1(r*mul*cos(theta) + width/2, r*mul*sin(theta) + height/2, 0, theta, 286, 0);
			ObjShot_SetSpellResist(obj, true);
			ObjMove_AddPatternA1(obj,100-i, 2.8, NO_CHANGE);
			theta+=360/40;
			i++;
			yield;
		}
	}
	else{
	    let del = 60;
		if(delay){del=90;}
		ObjSound_Play(shot2);
		loop(50){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj =CreateShotA1(180 * sin(3*theta) + width/2, 180 * cos(theta) + height/2, 0, rand(80, 100), 329, del);
			ObjMove_AddPatternA2(obj,100, 0, NO_CHANGE, rand(0.01, 0.02), 0, rand(2, 3));
			theta+=360/50;
			i++;
		}
	}
}


	
	
task laser{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 70);
    let las = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/GUN.WAV", 70);
    let ang=0; sound(las);
	let obj= CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ang+30, 400, 30, 120, 22, 60); mo(obj,1);
	ang=150;
	let obj3=CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ang, 400, 30, 120, 22, 60); mo(obj3,2);
	CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), ang+120, 400, 30, 60, 25, 60);
	CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 90, 450, 40, 60, 25, 60);
	CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 180, 450, 40, 60, 25, 60);
	CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 450, 40, 60, 25, 60);
	let obj1= CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), -30, 450, 40, 120, 22, 60);
	let obj2= CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 210, 450, 40, 120, 22, 60);
	lasMove(obj1, 1); lasMove(obj2, 2);
}

task lasMove(obj, n){
    wait(60);
    if(n==1){ascent(i in 0..120){ObjStLaser_SetAngle(obj, -30-i); yield;}}
	else{ascent(i in 0..120){ObjStLaser_SetAngle(obj, 210+i); yield;}}
}

task mo(obj, n){
    wait(60);
	if(n==1){ascent(i in 0..120){ObjStLaser_SetAngle(obj, 30-2*i); yield;}}
	else{ascent(i in 0..120){ObjStLaser_SetAngle(obj, 150+2*i); yield;}}
}

task sound(las){ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80)); wait(60); ObjSound_Play(las);}






