//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["sizeSpell 2"]
#System["./../../system/NSystem.dnh"]
#Player["./../../player/Reimu by plasmaflame22/Reimu_A.txt", "./../../player/Reimu by plasmaflame22/Reimu_C.txt"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncSaul.dnh"


let objBoss;
let width = 444;
let height = 448;
let life = 4600;
let time= 80;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let twinkle = LoadEffect(path~"TWINKLE.WAV", 80);

@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", "Size 2");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, 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);
	let shot3= LoadEffect(path~"ATTACK3.WAV", 100);
	Spell("Mind twist: Open[r]Inde-size-iveness", objBoss, 1);
    wait(240);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	move(width/3, 2*width/3, 2*height/5, height/2, 0, 120, 180, objBoss, 2*life/3, scene, 2*time/3);
	let speed = [1.4,2.6,2.2,3,3,4.5];
	spiral(8, speed, 8,14, 200, 5, 60, 2*life/3, scene, 2*time/3);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>2*life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> 2*time/3){yield;}
	ObjSound_Play(shot3);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 60);
	wait(60);
	
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	move(width/3, 2*width/3, height/5, 2*height/5, 0, 120, 180, objBoss, life/3, scene, time/3);
	speed = [1.8,2.2,2.4,2.8,2.8,3.4]; let ang= rand(0,360);
	circle(27, ang, speed, 20, 10, 14, life/3, scene, time/3);
	wait(60);
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 80));
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/3){
		ascent(i in 0..4){
		    let s= rand(0,360);
			let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(1.5,2), s+90*i, 376,0);
			toPlayer(obj);
		}
	    wait(40);
	}
	ObjSound_Play(shot3);
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(60);
	ObjSound_Play(LoadEffect(path~"POWER UP.WAV", 80));
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	move(width/3, 2*width/3, height/5, 2*height/5, 0, 180, 240, objBoss, 0, scene, 0);
	speed = [1.8,2.2,2.4,3.2,3.5,3.8];
	circle(32, ang, speed, 15, 10, 16, 0, scene, 0);
}




task spiral(numBranches, speed, angincmin, angincmax, period, pause, pauseSpir, limit, scene, timeLimit){
    let t; let ang; let col =[295,297,298]; let dir=1; if(rand(0,1)>1/2){dir=-1;} let first = true;
	let shot4 = LoadEffect(path~"ATTACK4.WAV", 60);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> timeLimit){
	    if(!first){col= [79,84,87];}
		ascent(i in 0..3){
		    t=0; ang= rand(0,360);
			while(t<period){
			    ObjSound_Play(shot4);
				ascent(j in 0..numBranches){
				    if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=limit || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<= timeLimit){return;}
					let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(speed[0+2*i], speed[1+2*i]), ang + 360/numBranches *j, col[i],0);
					con(obj);
				}
				wait(pause); t+=pause; ang+= dir*rand(angincmin, angincmax);
			}
			wait(pauseSpir); dir*=-1;
		}
		first=false; yield;
	}
}	
    


task circle(numBul, ang, speed, pause, angincmin, angincmax, limit, scene, timeLimit){
    let shot5 = LoadEffect(path~"ATTACK5.WAV", 70);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> timeLimit){
	    ObjSound_Play(shot5);
		ascent(i in 0..numBul/3){let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(speed[0], speed[1]), ang + 360/numBul *3*i, 295,0); con(obj);}
		ascent(i in 0..numBul/3){let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(speed[2], speed[3]), ang + 360/numBul  *(3*i+1), 297,0); con(obj);}
		ascent(i in 0..numBul/3){let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(speed[4], speed[5]), ang + 360/numBul  *(3*i+2), 298,0); con(obj);}
		ang+= rand(angincmin, angincmax); wait(pause);
	}
}


task toPlayer(obj){
	wait(30);
	if(!Obj_IsDeleted(obj)){
	    ObjSound_Play(twinkle);
	    loop(30){
		    if(Obj_IsDeleted(obj)){return;}
		    ObjMove_SetAngle(obj, GetAngleToPlayer(obj)); 
			yield;
		}
	}
}

task con(obj){
    while(!Obj_IsDeleted(obj)){
	    changeSize(obj);
		if(ObjShot_GetImageID(obj) == 295 || ObjShot_GetImageID(obj) == 79){wait(50);}
		else if(ObjShot_GetImageID(obj) == 297 || ObjShot_GetImageID(obj) == 84){wait(35);}
		else{wait(20);}
	}
}


task changeSize(obj){
    let graph= ObjShot_GetImageID(obj);
    alternative(graph)
	case(295){ObjShot_SetGraphic(obj, 79);}
	case(297){ObjShot_SetGraphic(obj, 84);}
	case(298){ObjShot_SetGraphic(obj, 87);}
	case(79){ObjShot_SetGraphic(obj, 295);}
	case(84){ObjShot_SetGraphic(obj, 297);}
	case(87){ObjShot_SetGraphic(obj, 298);}
}



