//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["hourglassNon"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 6500;
let time= 60;
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);
	SetCommonData("non1", "no");
	
	
	animNon(objBoss);
	TFinalizeNon(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, rand(3*height/10, 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{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	SetForbidPlayerShot(false);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	let shot = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 60);
	
    let inc= -1; if(rand(0,1)>=1/2){inc=-1;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjMove_SetDestAtFrame(objBoss, 40, 5*height/12, 120);
		wait(60);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Charge.WAV", 80));
		wait(60);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		shotSound(shot, 200, 60, 3, objBoss, false, 0);
		hourglass(height/3, -inc);
		wait(60);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTION.WAV", 90));
		ascent(i in -1..2){DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss) +90*i, 0, 400, 40, 22, 180);}
		wait(120);
		ObjEnemy_SetDamageRate(objBoss, 100, 50);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		shotSound(shot, 200, 60, 3, objBoss, false, 0);
		hourglass(height/2, inc); // 3*height/4
		wait(60);
		MasterSparkSound(120, objBoss);
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 400, 360, 120, 22, 0);
		wait(120);
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), height/2, 120);
		wait(420);
		ObjMove_SetDestAtFrame(objBoss,40, 0.78*height, 120);
		wait(120);
		shotSound(shot, 200, 60, 3, objBoss, false, 0);
		hourglass(3*height/4, -inc);
		wait(60);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTION.WAV", 90));
		ascent(i in -1..2){DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss) +90*i, 0, 400, 40, 22, 180);}
		wait(180);
		MasterSparkSound(120, objBoss);
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 400, 360, 120, 22, 0);
		wait(120);
		ObjMove_SetDestAtFrame(objBoss, rand(width/4, 3*width/4), 3*height/4, 120);
		wait(260); inc*=-1;
	}
}


task hourglass(y, inc){
	let rad1= 182; let t=0;
	loop(200){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
		let obj1 =CreateShotA1(rad1*cos(t)+ width/2, rad1*0.4 *sin(5*t) + y, 0, rand(80, 100), 43, 3);
		let obj2= CreateShotA1(rad1*cos(t+180)+ width/2, rad1*0.4 *sin(5*(360-t)) + y, 0, rand(255, 295), 52, 3);
		ObjMove_AddPatternA2(obj1,270, 0, NO_CHANGE, rand(0.005, 0.04), 0, 1.8);
		ObjMove_AddPatternA2(obj2, 270,0, NO_CHANGE, rand(0.01, 0.03), 0, rand(0.5, 1.5));
		t+= inc*180/200;
		yield;
	}
	let i=0;
	loop(50){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
		let obj1 =CreateShotA1(rad1*cos(t)+ width/2, rad1*0.4 *sin(5*t) + y, 0, rand(75, 105), 43, 3);
		let obj2= CreateShotA1(rad1*cos(t+180)+ width/2, rad1*0.4 *sin(5*(360-t)) + y, 0, rand(255, 295), 52, 3);
		ObjMove_AddPatternA2(obj1,100-i, 0, NO_CHANGE, rand(0.01, 0.02), 0, rand(2, 3));
		ObjMove_AddPatternA2(obj2, 100-i,0, NO_CHANGE, rand(0.01, 0.02), 0, rand(1, 2));
		t+= inc*180/50;
		yield;
		i++;
	}
	wait(20);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/TWINKLE.WAV",80));

}