//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Sine of the times"]
#include "./../ZUNShot/ZUNShot_Const.txt"
#include "./sharedfuncJames.dnh"


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

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(90);
    }
    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);
    progress;
    takeDamage(objBoss, scene, life);
	let obj = LoadEffect(GetCurrentScriptDirectory~"./../SFX/ATTACK4.WAV", 40);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/SPELLCARD.WAV", 100));
	Spell("Sine of the times", scene, objBoss);
    wait(240);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK5.WAV", 80));
	let obj1= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjShot_SetSpellResist(obj1, true);
	ObjMove_SetDestAtFrame(obj1, width/8, height/8, 60);
	let obj3 = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 401, 0);
	ObjShot_SetSpellResist(obj3, true);
	ObjMove_SetDestAtFrame(obj3, width/4, 0, 60);
	let obj4 = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 401, 0);
	ObjMove_SetDestAtFrame(obj4, 3*width/4, 0, 60);
	ObjShot_SetSpellResist(obj4, true);
	let obj2= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, 399, 0);
	ObjMove_SetDestAtFrame(obj2, 7*width/8, 7*height/8, 60);
	ObjShot_SetSpellResist(obj2, true);
	wait(90);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Charge.WAV", 80));
	wait(60);
	fuel(1, 49); fuel(2, 55);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 30);
	shotSound(obj, -1, 50, 2, objBoss, 0);
	Bowap(obj1); Bowap(obj2);
	moveEdge(obj1, 90, 0.4, height/8, 7*height/8, 30, objBoss, 0);
	moveEdge(obj2, -90, 0.4, 7*height/8, height/8, 30, objBoss, 0);
	
	
}

/*
task control(obj, wvel, timeToChange, delay){
    ObjRender_SetAlpha(obj, 250);
	ascent(c in 0..4){
		ObjMove_AddPatternA2(obj, timeToChange*c, NO_CHANGE, NO_CHANGE, NO_CHANGE, wvel, 3);
		wvel*= -1;;
	}
	wait(delay);
	Obj_Delete(obj);
}
*/

task fuel(n, col){
    let shot = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK2.WAV", 60);
	shotSound(shot, -1, 60, 4, objBoss, 0);
    if(n==1){
	    let t=0;
	    while(t<110){
			CreateShotA1(3*width/4, 0, 8, rand(atan((7*height/8)/(width/8))-2, atan((7*height/8)/(width/8)) +2), col, 0);
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan(-(-7*height/40)/(3*width/8)) +178, atan(-(-7*height/40)/(3*width/8)) +182), col, 0);
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan((23*height/40)/(3*width/8))-2, atan((23*height/40)/(3*width/8))+2), col, 0);
			CreateShotA1(width/4, 0, 8, rand(atan(-(height/8)/(width/8)) +178, atan(-(height/8)/(width/8)) +182),col, 0);
			t++;
			yield;
		}
		t=0;
		let obj1down = true;
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			if(obj1down){
				while(t<840){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					CreateShotA1(3*width/4, 0, 8, rand(atan((7*height/8-0.4*t)/(width/8))-2, atan((7*height/8-0.4*t)/(width/8)) +2), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan(-(-7*height/40 +0.4*t)/(3*width/8)) +178, atan(-(-7*height/40 +0.4*t)/(3*width/8)) +182), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan((23*height/40 -0.4*t)/(3*width/8))-2, atan((23*height/40 -0.4*t)/(3*width/8))+2), col, 0);
					CreateShotA1(width/4, 0, 8, rand(atan(-(height/8+0.4*t)/(width/8)) +178, atan(-(height/8+0.4*t)/(width/8)) +182), col, 0);
					t++;
					yield;
				}
				t=0;
			    while(t<30){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					CreateShotA1(3*width/4, 0, 8, rand(atan((height/8)/(width/8))-2, atan((height/8)/(width/8)) +2), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan(-(23*height/40)/(3*width/8)) +178, atan(-(23*height/40)/(3*width/8)) +182), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan((-7*height/40)/(3*width/8))-2, atan((-7*height/40)/(3*width/8))+2), col, 0);
					CreateShotA1(width/4, 0, 8, rand(atan(-(7*height/8)/(width/8)) +178, atan(-(7*height/8)/(width/8)) +182), col, 0);
					t++;
					yield;
				}
			}
			else{
				while(t<840){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					CreateShotA1(3*width/4, 0, 8, rand(atan((height/8+0.4*t)/(width/8))-2, atan((height/8+0.4*t)/(width/8)) +2), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan(-(23*height/40 - 0.4*t)/(3*width/8)) +178, atan(-(23*height/40 -0.4*t)/(3*width/8)) +182), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan((-7*height/40 +0.4*t)/(3*width/8))-2, atan((-7*height/40 +0.4*t)/(3*width/8))+2), col, 0);
					CreateShotA1(width/4, 0, 8, rand(atan(-(7*height/8-0.4*t)/(width/8)) +178, atan(-(7*height/8 -0.4*t)/(width/8)) +182), col, 0);
					t++;
					yield;
				}
				t=0;
				while(t<30){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
					CreateShotA1(3*width/4, 0, 8, rand(atan((7*height/8)/(width/8))-2, atan((7*height/8)/(width/8)) +2), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan(-(-7*height/40)/(3*width/8)) +178, atan(-(-7*height/40)/(3*width/8)) +182), col, 0);
					CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, rand(atan((23*height/40)/(3*width/8))-2, atan((23*height/40)/(3*width/8))+2), col, 0);
					CreateShotA1(width/4, 0, 8, rand(atan(-(height/8)/(width/8)) +178, atan(-(height/8)/(width/8)) +182), col, 0);
					t++;
					yield;
				}
			}
		    t=0;
			obj1down= ! obj1down;
		}
	}
	else{
	    wait(60);
		let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/Wave.WAV", 70);
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>2*life/3){yield;}
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		    ObjSound_Play(shot2);
		    let ang= GetAngleToPlayer(objBoss)-60;
		    ascent(i in 0..5){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 4, rand(ang+40*i-5, ang+40*i+5), rand(-0.02, -0.05), 2, 357, 0);}
			ascent(i in 0..7){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, rand(180+ 30*i-5, 180+30*i+5), rand(-0.02, -0.05), 2, 357, 0);}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3){wait(60);}
			else{wait(30);}
		}
	}
}

task progress{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>2*life/3){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 70));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../SFX/ATTACK3.WAV", 70));
}
			

task Bowap(obj){
    let ang= rand(200, 220);
	let count= 1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    loop(4){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, ang, 124, 5);
			ang+= 360/4;
		}
		ang+= sin(count)*20;
		count++;
		wait(3);
	}
}