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



let objBoss;
let width = 384;
let height = 448;
let life = 5200;
let time= 75;
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);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Timestop");
	
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	
    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{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/SPELLCARD.WAV", 100));
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTIONSHORT.WAV", 100));
	Spell("Chaos sign:[r]Periodic waves", objBoss, 1);
    wait(240);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK3.WAV", 70));
    let graph = [241, 242, 245, 246]; let dir=1; if (rand(0,1)>=1/2){dir=-1;} let phas = 0;
    ascent(i in 0..3){
	    let las = CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), GetAngleToPlayer(objBoss)+(90+40*i) *dir, 400, 50, 300, 25, 220);	
	    lasCon(las, dir,i);
	}
	wait(460); dir*=-1;
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(120);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
		wait(60);
		ascent(i in 0..4){
		    if(i==1){
			    ascent(k in 0..3){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					let las = CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), GetAngleToPlayer(objBoss)+(90+40*k) *dir, 400, 50, 300, 25, 220);	
					lasCon(las, dir,k);
				}
			}
			loop(270-55*i){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 70));
				let obj= CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4,10), rand(0,180), rand(-0.25, -0.1), 0, graph[i], 0);
				ObjShot_SetSpellResist(obj, true); check(obj, phas, i);
			}
			loop(500-80*i){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 70));
				let obj= CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4,10), rand(130,420), rand(-0.25, -0.1), 0, graph[i], 0);
				ObjShot_SetSpellResist(obj, true);
				if(phas%2 ==0){ObjMove_AddPatternA2(obj, 600+50*i, 0, ObjMove_GetAngle(obj)+180, rand(0.004, 0.01), 0, rand(1.2,1.4));}
				else{ObjMove_AddPatternA2(obj, 600+50*i, 0, -ObjMove_GetAngle(obj), rand(0.005, 0.01), 0, rand(1.4,1.8));}
			}
			wait(20);
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/SlowMo.WAV", 90));
			StartSlow(TARGET_ALL, 30);
			if(i<=2){wait(70-20*i);}
			else{wait(30);}
			StopSlow(TARGET_ALL);
		}
		wait(30); // 290 frames since first wave
		StopSlow(TARGET_ALL); dir*=-1; phas++;
		wait(310); // need 1050 , 600 frames since first wave, 510 frames since second
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80));
		wait(140); // 650 since second, 580 since third
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80));
		wait(120); // 700 since third, 650 since foruht
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80));
		wait(100);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 80));
		wait(390);
	}
}

task check(obj, phas, i){
    let t=0; while(t<200){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){Obj_Delete(obj); return;} yield; t++;}
	if(ObjMove_GetY(obj)<= height){
	    if(phas%2 ==0){ObjMove_AddPatternA2(obj, 600+50*i, 0, ObjMove_GetAngle(obj)+180, rand(0.004, 0.01), 0, rand(1.2,1.4));}
		else{ObjMove_AddPatternA2(obj, 600+50*i, 0, -ObjMove_GetAngle(obj), rand(0.005, 0.01), 0, rand(1.4,1.8));}
	}
	else{Obj_Delete(obj);}
}

task lasCon(las, dir,i){
    let t=0;
	while(t<120){ObjStLaser_SetAngle(las, GetAngleToPlayer(objBoss)-dir*(90+40*i)); yield; t++;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 80));
	while(t<180){ObjStLaser_SetAngle(las, GetAngleToPlayer(objBoss)-dir*(90+40*i)); yield; t++;}
	wait(40);
	MasterSparkSound(300, objBoss);
	wait(100);
	let ang = ObjStLaser_GetAngle(las);
	ascent(i in 0..180){ObjStLaser_SetAngle(las, ang + dir* i*7/12); yield;}
}

	
	