//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Convergence"]
#BGM["./Byakuren.ogg"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.dnh"


let objBoss;
let width = 384;
let height = 448;
let life = 3000;
let scene = GetEnemyBossSceneObjectID();


@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(80);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	
	animSpell(objBoss);
	TFinalizeSpell(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 3*height/10, 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{
    Indicator(objBoss);
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 40);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/SPELLCARD.WAV", 100));
	Spell("STG space[r]convergence", scene, objBoss);
    wait(180);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Charge.WAV", 70));
	wait(60);
	let t=0;
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	shotSound(shot, 120, 55, 8, objBoss,0);
	while(t<120){
	    ascent(i in 0..10){let obj = CreateShotA1(ObjMove_GetX(objBoss)+10*cos(160+ i*220/9), ObjMove_GetY(objBoss)+10*sin(160+ i*220/9), rand(3, 4), 160+ i*220/9, 45, 0); Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);}
		ascent(i in 0..10){let obj = CreateShotA1(ObjMove_GetX(objBoss)+10*cos(160+ i*220/9), ObjMove_GetY(objBoss)+10*sin(160+ i*220/9), rand(3, 4), 171+ i*220/9, 47, 0); Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);}
		wait(8);
		t+=8;
	}
	t=0;
	shotSound(shot, 300, 55, 3, objBoss,0);
	while(t<300){
	    ascent(i in 0..15){
		    let obj = CreateShotA1(ObjMove_GetX(objBoss) +10*cos(160+ i*220/14), ObjMove_GetY(objBoss) + 10*sin(160+ i*220/14), rand(3, 4), 160+ i*220/14, 45, 0);
			Obj_SetRenderPriorityI(obj, 60);
			ObjRender_SetAlpha(obj, 200);
			if(rand(0,1)>=1/2){converge(obj);}
		}
		ascent(i in 0..15){
		    let obj = CreateShotA1(ObjMove_GetX(objBoss) + 10*cos(160+ i*220/14), ObjMove_GetY(objBoss) + 10*sin(160+ i*220/14), rand(3, 4), 165+ i*220/14, 47, 0);
			Obj_SetRenderPriorityI(obj, 60);
			ObjRender_SetAlpha(obj, 200);
			if(rand(0,1)>=1/2){converge(obj);}
		}
		wait(4);
		t+=4;
	}
	ObjEnemy_SetDamageRate(objBoss, 60, 30);
	wait(60);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Horn.WAV", 85));
	wait(60);
	fall;
	stream;
	circles;

}

task stream{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		let t=0;
		shotSound(shot, 60, 55, 8, objBoss,0);
		while(t<60){
			ascent(i in 0..10){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj = CreateShotA1(ObjMove_GetX(objBoss)+10*cos(160+ i*220/9), ObjMove_GetY(objBoss)+10*sin(160+ i*220/9), rand(3, 4), 160+ i*220/9, 45, 0); 
				Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);
			}
			ascent(i in 0..10){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj = CreateShotA1(ObjMove_GetX(objBoss)+10*cos(160+ i*220/9), ObjMove_GetY(objBoss)+10*sin(160+ i*220/9), rand(3, 4), 171+ i*220/9, 47, 0); 
				Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);
			}
			wait(8);
			t+=8;
		}
		t=0;
		shotSound(shot, 300, 55, 3, objBoss,0);
		while(t<300){
			ascent(i in 0..15){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj = CreateShotA1(ObjMove_GetX(objBoss) +10*cos(160+ i*220/14), ObjMove_GetY(objBoss) + 10*sin(160+ i*220/14), rand(3, 4), 160+ i*220/14, 45, 0);
				Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);
				if(rand(0,1)>=1/2){converge(obj);}
			}
			ascent(i in 0..15){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj = CreateShotA1(ObjMove_GetX(objBoss) +10*cos(160+ i*220/14), ObjMove_GetY(objBoss) + 10*sin(160+ i*220/14), rand(3, 4), 165+ i*220/14, 47, 0);
				Obj_SetRenderPriorityI(obj, 60);
				ObjRender_SetAlpha(obj, 200);
				if(rand(0,1)>=1/2){converge(obj);}
			}
			wait(4);
			t+=4;
		}
		wait(120);
	}
}


task converge(obj){
    wait(truncate(rand(5, 30)));
	if(rand(0,1)>=1/2){
	    if(rand(0,1)>=1/2){ObjMove_AddPatternA1(obj, 0, rand(3,4), GetAngleToPlayer(obj)-40);}
		else{ObjMove_AddPatternA1(obj, 0, rand(3,4), GetAngleToPlayer(obj)+40);}
	}
	else{ObjMove_AddPatternA1(obj, 0, rand(2,2.5), GetAngleToPlayer(obj));}
}

task circles{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK4.WAV", 60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>2*life/3){yield;}
    let transl = [0, 45, 90, 135, 180];
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let x = GetPlayerX; let y = GetPlayerY; let ang = rand(0,360); let dec = transl[truncate(rand(0, 3.9))];
		shotSound(shot, 20, 70, 1, objBoss,0);
		ascent(i in 0..20){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} let obj = CreateShotA1(x+ 160*cos(ang+ i*360/19+ dec), y+ 160*sin(ang+ i*360/19 +dec), 0, -ang-i*360/19 -dec + dec, 338, 0); yield; 
		    ObjMove_AddPatternA2(obj, 70-i, 0, NO_CHANGE, 0.02, 0, 2);
			Obj_SetRenderPriorityI(obj, 70);}
        if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/8){wait(120);}
		else{wait(60);}
	}
}

task fall{
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 60);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		let i = 1; let y;
		if(rand(0,1)>=1/2){i = -1;}
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
		    if(i==-1){y= height;} else{y = 0;}
			let period = rand(300, 500); let t=0;
			warn(period);
			while(t<period){
                ObjSound_Play(shot);			
				ascent(j in 0..6){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} let obj = CreateShotA1(10+ j*(width-10)/5, y, 4, i*90, 325, 30);Obj_SetRenderPriorityI(obj, 70);}
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2){wait(80); t+=80;}
				else{wait(50); t+=50;}
				yield;
			}
			i*=-1;
		}	
	}
}

task warn(period){
    let horn = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Horn.WAV", 85);
    loop(period-60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;} yield;}
	ObjSound_Play(horn);
}

		    
	
	
	
	