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

let objBoss;
let width = 384;
let height = 448;
let life = 1;
let arrow = false;
let time= 122;
let scene= GetEnemyBossSceneObjectID();



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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	Indicator(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetForbidPlayerShot(true);
	
    animSpell(objBoss);
	
	TFinalizeSpell(scene, objBoss);
	
	
    ObjMove_SetDestAtWeight(objBoss, width/2, height/5+20, 4, 4);
	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{
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/SPELLCARD.WAV", 100));
	Spell("Vectorganza", scene, objBoss);
	wait(240);
	stars;
	warn;
	
	phase1(18, 1, 0.6); // durée 35 secondes.
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-30){yield;}
	phase2(4,15, 12);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-42){yield;}
	phase1(22, -1, 0.9);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-42-26){yield;}
	phase2(5, 12, 10);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-42-26-12){yield;}
	phase1(26, 1, 1.2);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-42-26-12-26){yield;}
	phase2(7, 10, 8);
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 10){yield;}
	
}
	
task rapidArrow{
    while(arrow){
	    ascent(i in 0..5){let obj= CreateShotA1(0, 5*i, rand(6,8), 0, 410, 0); ObjRender_SetAlpha(obj, 100);}
		ascent(i in 0..5){let obj=CreateShotA1(width, 5*i, rand(6,8), 180, 410, 0); ObjRender_SetAlpha(obj, 100);}
		ascent(i in 0..5){let obj=CreateShotA1(0, height -5*i, rand(6,8), 0, 410, 0); ObjRender_SetAlpha(obj, 100);}
		ascent(i in 0..5){let obj=CreateShotA1(width, height-5*i, rand(6,8), 180, 410, 0);ObjRender_SetAlpha(obj, 100);}
		ascent(i in 0..5){let obj=CreateShotA1(10*i, 0, rand(6,8), 90, 410, 0);ObjRender_SetAlpha(obj, 100);}
		ascent(i in 0..5){let obj=CreateShotA1(width-5*i, 0, rand(6,8), 90, 410, 0);ObjRender_SetAlpha(obj, 100);}
	    wait(5);
	}
}
	
	
task phase1(numLas, dir, inc){
    loop(2){	
		let ang= rand(30,60);
		let col= truncate(rand(411, 417));
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/Horn.WAV", 80));
		ascent(i in 0.. numLas/2){DelayLaser(0, i*2* height/numLas, ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(i*2* width/numLas, 0,  ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(width, i* 2*height/numLas, 180-ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(i* 2*height/numLas, 0, 180-ang, 600, 24, 3, 60);}
		ascent(i in 0.. 12){DelayLaser(i* 2*width/12, height, -90, 600, 24, 3, 60);}
		wait(60);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/ATTACK4.WAV", 60));
		ascent(i in 0.. numLas/2){CreateShotA1(0, i*2* height/numLas, rand(6, 7), ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(i*2* width/numLas, 0, rand(6,7), ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(width, i*2* height/numLas, rand(6,7), 180-ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(i*2* height/numLas, 0, rand(6,7), 180-ang, col, 0);}
		ascent(i in 0.. 12){CreateShotA1(i*2* width/12, height, rand(6,7), -90, 410, 0);}
		wait(20);
		
		col = truncate(rand(410, 417));
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/Horn.WAV", 80));
		ascent(i in 0.. 15){DelayLaser(i* height/15, 0, 90, 500, 24, 3, 60);}
		ascent(i in 0.. 22){DelayLaser(0, i* height/22, 0, 500, 24, 3, 60);}
		wait(60);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/ATTACK4.WAV", 60));
		ascent(i in 0.. 15){CreateShotA1(i*height/15, 0, 8, 90, col, 0);}
		ascent(i in 0.. 15){CreateShotA1(i*height/15, height, 8,  90+180, col, 0);}
		ascent(i in 0.. 22){CreateShotA1(0, i* height/22, 8, 0, col, 0);}
		ascent(i in 0.. 22){CreateShotA1(width, i* height/22, 8, 180, 411, 0);}
		wait(20);
		
		col = truncate(rand(411, 417));
		ang=rand(30,60);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/Horn.WAV", 80));
		ascent(i in 0.. numLas/2){DelayLaser(0, i*2* height/numLas, -ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(i*2* width/numLas, height,  -ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(width, i* 2*height/numLas, -180+ang, 600, 24, 3, 60);}
		ascent(i in 0.. numLas/2){DelayLaser(i* 2*height/numLas, height, -180+ang, 600, 24, 3, 60);}
		ascent(i in 0.. 12){DelayLaser(i* 2*width/12, 0, 90, 600, 24, 3, 60);}
		wait(60);
		ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/ATTACK4.WAV", 60));
		ascent(i in 0.. numLas/2){CreateShotA1(0, i*2* height/numLas, rand(6, 7), -ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(i*2* width/numLas, height, rand(6,7), -ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(width, i*2* height/numLas, rand(6,7), -180+ang, col, 0);}
		ascent(i in 0.. numLas/2){CreateShotA1(i*2* height/numLas, height, rand(6,7), -180+ang, col, 0);}
		ascent(i in 0.. 12){CreateShotA1(i*2* width/12, 0, rand(6,7), 90, 410, 0);}
		wait(60);
		
		let j=dir;
		loop(4){
		    let a = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK4.WAV", 60);
		    shotSound(a, 90, 50, 10, objBoss);
			ang= GetAngleToPlayer(objBoss);
			let col1= truncate(rand(409,417));
			let col2= truncate(rand(154, 169));
			loop(90){
				loop(5){
				    loop(2){
					    let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, 5, rand(ang-20, ang+20), col2, 0);
						ObjShot_SetSpellResist(obj, true);
					}
					loop(2){
					    let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, 7, rand(ang-15, ang+15), col1, 0);
						ObjShot_SetSpellResist(obj, true);
					}
					ang+=360/5;
				}
				ang= ang + j* inc;
				yield;
			}
			j=-j;
			if(inc<0.9){wait(10);}
		}
	}	    
}
	
	
	
task phase2(phas, num1, num2){
    arrow = true;
	rapidArrow;
    let a = LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK4.WAV", 60);
	wait(60);
    ascent(i in 1..5){
		let col = truncate(rand(411, 417));
		let c= (i+3) % 5;
		if(c==0){c=1;}
		ObjSound_Play(a);
		direction(i, 414, num1, phas);
		direction(c,414, num2, phas);
		CurveLas(i);
		wait(90);
	}
	if(phas != 5){arrow= false;}
	else{while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 42){yield;} arrow = false;}
}

task direction(dir, col, num, phas){
    alternative(dir)
	case(1){ascent(i in 0..num){ let obj =CreateShotA1(0, i* height/(num-1) + rand(-1, 1)* height/(num-1), phas, 0, col, 60); ObjRender_SetAlpha(obj, 250);}}
	case(2){ascent(i in 0..num){ let obj=CreateShotA1(i*width/(num-1) + rand(-1, 1)* width/(num-1), 0, phas, 90, col, 60); ObjRender_SetAlpha(obj, 250);}}
	case(3){ascent(i in 0..num){ let obj = CreateShotA1(width, i*height/(num-1) +rand(-1, 1)* height/(num-1), phas, 180, col, 60); ObjRender_SetAlpha(obj, 250);}}
	case(4){ascent(i in 0..num){ let obj = CreateShotA1(i*width/(num-1) + rand(-1, 1)* width/(num-1), height, phas, -90, col, 60); ObjRender_SetAlpha(obj, 250);}}
}

task CurveLas(i){
    if(i<=2){
	    ascent(j in 0..4){
		    let obj= CreateCurveLaserA1(0,100*(j+1), 3, -45, 70, 20, 28, 0);
			control(obj, 1, 60, 300);
		}
	}
	else{
	    ascent(j in 0..4){
		    let obj = CreateCurveLaserA1(width, 30 + 100*j, 3, 135, 70, 20, 28, 00);
			control(obj, 1, 60, 300);
		}
	}
}

task stars{
    let twinkle = LoadEffect(GetCurrentScriptDirectory~ "../SFX/TWINKLE.WAV", 60);
	ObjSound_SetRestartEnable(twinkle, true);
    while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>92){yield;}
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 79){
	    ObjSound_Play(twinkle);
		CreateShotA1(rand(width/10, 9*width/10), height, rand(0.6,1.5), -90, 219, 0);
		wait(40);
	}
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 47){yield;}
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 43){
	    ObjSound_Play(twinkle);
		loop(2){
			if(rand(0,1)>=1/2){CreateShotA1(width, rand(20, height -30), rand(1,2), 180, 226,0);}
			else{CreateShotA1(0, rand(20, height -30), rand(1,2), 0, 226,0);}
		}
		wait(5);
	}
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 7){yield;}
	ObjSound_Play(twinkle);
	ascent(i in 0..20){CreateShotA1(10, i*height/20+10, 0, 0, 284, 30);}
	ascent(i in 0..20){CreateShotA1(width-10, i*height/20 +10, 0, 0, 284, 30);}
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 2){
	    ObjSound_Play(twinkle);
	    loop(2){CreateShotA1(rand(width/10, 9*width/10), height, rand(1,2), -90, 219, 0);}
		loop(4){
			if(rand(0,1)>=1/2){CreateShotA1(width, rand(20, height -30), rand(1,1.2), 180, 226,0);}
			else{CreateShotA1(0, rand(20, height -30), rand(1,1.2), 0, 226,0);}
		}
		wait(5);
	}
}
    
task warn{
    while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 9){yield;}
    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory()~"../SFX/Horn.WAV", 80));
    let objText = ObjText_Create();
	ObjText_SetText(objText,"WARNING!");
	ObjText_SetFontSize(objText, 40);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontColorTop(objText, 255, 165, 30);
	ObjText_SetFontColorBottom(objText, 255, 165, 30);
	ObjText_SetFontBold(objText, true);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 300);
	Obj_SetRenderPriorityI(objText, 80);
	
	ObjRender_SetPosition(objText, 384/2 -150, 400, 1);
	let alpha = 95;
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;}
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;}
	loop(40){ObjRender_SetAlpha(objText, alpha); alpha+=4; yield;}
	loop(60){ObjRender_SetAlpha(objText, alpha); alpha-=4; yield;}
	
	Obj_Delete(objText);
}
	
	
	
	
	
	
