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



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

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(time);
    }
	case(EV_REQUEST_IS_DURABLE_SPELL){
	    SetScriptResult(true);
	}
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetForbidPlayerShot(true);
	SetCommonData("non1", "no");
	SetCommonData("spell", "Vector field");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, true, 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{
    let shot = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/TWINKLE.WAV", 50);
    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));
	Spell("Chaos sign:[r]Phase portrait", objBoss, 1);
    wait(240);
    NotifyEventAll(EV_USER_STAGE+4, 0);
	
    let t=0;
	while(t<680){spawn(140, 413, 50,2,4, 400, true); wait(20-17*t/800); t+= 20-17*t/800;}
	while(t<800){spawn(140, 413, 50,2,4, 400, false); wait(20-17*t/800); t+= 20-17*t/800;}
	while(t<860){spawn(140, 413, 50,2,4, 400, false); wait(2); t+=2;}
	t=0;
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-30){
	    spawn(140, 412, 50,2,3.5, 400, false); if(t%4	==0){spawn(140, 412, -width/4, 2,4, 200, false);}
		spawn(140, 412, -width/4, 2,4, 200, false);
		if(t%4==0){wait(2);} else{yield;} t++;
	}
	t=0;
	wait(120);
	while(t<100){spawn(-90, 410, 640,2, 3, 200, false); wait(6-t/50); t+= 6-t/50;}
	phase2(1); phase2(-1);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-62){spawn(-90, 410, 640,0.8, 1.5, 200, false); wait(8);}
	wait(180);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>3){spawn(-90, 410, 640,2, 4, 200, false); wait(4);}
	NotifyEventAll(EV_USER_STAGE+5, 0);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>0){spawn(-90, 410, 640,2, 4, 200, false); wait(3);}
	
	
	
}

task phase2(n){
    let t=0; let ang= rand(25, 35);
	while(t<100){spawn(90+n*ang, 414, -width/2+n*100, 2, 3, 200, false); wait(7); t+=7;}
	ascent(i in 0..97){spawn(90+n*ang, 414, -width/2+ 2*n*i, 2, 3, 200, false); wait(8);}
	wait(120);
	t=0; ang = rand(5, 20);
	if(n==1){
	    while(t<120){spawn(-ang, 414, 932, 2, 2.8, 100, false); wait(8); t+=8;}
		ascent(i in 0..62){spawn(-ang, 414, 900+3*i, 1, 2.5, 120, false); wait(9);}
	}
	else{
	    while(t<120){spawn(180+ang, 414, 100, 2, 2.8, 100, false); wait(8); t+=8;}
		ascent(i in 0..62){spawn(180+ang, 414, 80+3*i, 1, 2.5, 120, false); wait(9);}
	}
}
	
   
    

function coordinates(t){
    let s = t%1664;
	if(s <= 448){result= [384, s];}
	else if(s<=832){result= [384-s%448, 448];}
	else if(s<=1280){result= [0, 448- s%832];}
	else{result = [s%1280, 0];}
	return result;
}

task spawn(ang, col, t, speed1, speed2, maxdiff, soun){
    let coord = coordinates(t+rand(-maxdiff, maxdiff));
	let shot = CreateShotA1(coord[0], coord[1], rand(speed1,speed2), ang, col, 5);
	ObjShot_SetSpellResist(shot, true);
}



