//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["matrixSpell"]
#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 "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 3100;
let limit = life*0.4;
let time= 70;
let scene = GetEnemyBossSceneObjectID();

let path = GetCurrentScriptDirectory~"./../../SFX/";
let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 60);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 60);
let lenen = LoadEffect(path~ "LenenShot.WAV", 90);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let flash = LoadEffect(path~ "swordFlash.WAV", 90);


@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);
	SetPlayerClip(5,5, 415, 435);
    ObjEnemyBossScene_StartSpell(scene);
	
	SetCommonData("non1", "yes");
	SetCommonData("spell", "matrix");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/4, 60);
	Main;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 35); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}

//task shotSound(obj, time, volume, delay, enemy, delete, limit, scene, timeLimit){


task Main{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Ring theory:[r]Matrix ring", objBoss, 1);
    wait(160);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	shotSound(lenen, -1, 80, 60, objBoss, false, 0, scene, 0);
	lines(4, 1.6, 83, 60, true);
	lines(4, 1.6, 76, 60, false);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	bulSpawn(4, 70, 294, 360);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){yield;}
	yield;
	DeleteShotAll(TYPE_SHOT, TYPE_FADE);
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 100));
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	let dir =1; let ang = -90; let numBul =5;
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		loop(5){
		    ObjSound_Play(shot4);
		    ascent(i in 0.. numBul){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj = CreateShotA1(X(objBoss), Y(objBoss), 2.5, ang+ 360/numBul *i, 252, 5);
				Obj_SetRenderPriorityI(obj, 51);
			}
			ang+= dir* rand(13, 20);
			wait(20);
		}
		dir*= -1; yield;
	}
}


task bulSpawn(numBullets, rad, graphic, pause){
    let first = true;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let x = GetPlayerX; let y = GetPlayerY; let dir=1; if(rand(0,1)>0.5){dir = -1;} let ang= rand(0,360); let numBul= numBullets;
		if(first || ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=time/2){numBul=3; ang = rand(-30,30);}
		sound;
	    ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			ObjSound_Play(shot5);
			let obj = CreateShotA1(x+rad*cos(ang + 360/numBul *dir*i),y + rad*sin(ang+ 360/numBul *dir*i), 0, ang+ 360/numBul *dir*i + 180, graphic, 60);
			ObjShot_SetSpellResist(obj, true);
			Obj_SetRenderPriorityI(obj, 50);
			ObjMove_AddPatternA2(obj, 100-numBul*i, 0, NO_CHANGE, 0.05, 0, 2.5); Comm(obj, x, y, 293);
			wait(5);
		}
		wait(pause);
		first = false;
	}
}


task sound{
    wait(50);
	ObjSound_Play(horn);
}


//task helix(x, y, speed, angle, radius, gapLength, numBul, delay, topGraphic, bottomGraphic, enemy, limit){
//task DelayLaser(x, y, ang, len, wid, graph, time){ 


task Comm(obj, x, y, graphic){
	while(absolute(X(obj)-x) >= 2 || absolute(Y(obj)-y) >= 2){yield;}
	graphChange(obj, [ObjShot_GetImageID(obj), graphic], 10);
	while(X(obj) < width && X(obj) > 0 && Y(obj) > 0 && Y(obj)< height){yield;}
	ObjMove_SetSpeed(obj, 0); ObjMove_SetAcceleration(obj, 0); 
	let ang=0;
	if(X(obj)>= width){ang=180;} 
	if(Y(obj)>= height){ang=270;} 
	if(Y(obj)<= 0){ang=90;} 
	ObjSound_Play(flash);
	DelayLaser(obj, X(obj), Y(obj), ang, 500, 30, 16, 60);
	wait(30);
	shotSound(shot4, 60, 50, 6, objBoss, false, 0, scene, 0);
	helix(X(obj), Y(obj), 7, ang, 22, 50, 10, 6, 57,57, objBoss, 0);
	wait(60);
	Obj_Delete(obj);
}


task graphChange(obj, graphic, delay){
    let len = length(graphic); let i=1;
    while(!Obj_IsDeleted(obj) && ObjMove_GetSpeed(obj)>0){
	    ObjShot_SetGraphic(obj, graphic[i]);
		i= (i+1)%len ; wait(delay);
	}
	if(!Obj_IsDeleted(obj)){ObjShot_SetGraphic(obj, graphic[1]);}
}
	


task lines(numEntries, speed, graphic, pause, vertical){
    if(vertical){
		while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj = CreateShotA1(8, 0, speed, 90, graphic, 5);
			let obj2 = CreateShotA1(width-8, 0, speed, 90, graphic, 5);
			Obj_SetRenderPriorityI(obj, 51); Obj_SetRenderPriorityI(obj2, 51);
			ObjRender_SetAlpha(obj, 210); ObjRender_SetAlpha(obj2, 210);
			ascent(i in 1..numEntries){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj3= CreateShotA1(8 + 404/numEntries *i, 0, speed, 90, graphic, 5);
				ObjRender_SetAlpha(obj3, 210); Obj_SetRenderPriorityI(obj3, 51);
			}
			wait(pause);
		}
	}
	else{
	    let ang = 0; if(rand(0,1)>0.5){ang=180;}
		if(ang==0){
			while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj = CreateShotA1(0, 8, speed, 0, graphic, 5); ObjRender_SetAlpha(obj, 210);
				Obj_SetRenderPriorityI(obj, 51);
				ascent(i in 1..numEntries+1){
					if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    if(i%2==0){let obj2 = CreateShotA1(0, 8 + 424/numEntries *i, speed, 0, graphic, 5); ObjRender_SetAlpha(obj2, 210); Obj_SetRenderPriorityI(obj2, 51);} 
					else{let obj2 = CreateShotA1(width, 8 + 424/numEntries *i, speed, 180, graphic, 5); ObjRender_SetAlpha(obj2, 210); Obj_SetRenderPriorityI(obj2, 51);}
				}
				wait(pause);
			}
		}
		else{
			while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj = CreateShotA1(width, 8, speed, 180, graphic, 5); ObjRender_SetAlpha(obj, 210); Obj_SetRenderPriorityI(obj, 51);
				ascent(i in 1..numEntries+1){
				    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				    if(i%2==0){let obj2 = CreateShotA1(width, 8 + 424/numEntries *i, speed, 180, graphic, 5); ObjRender_SetAlpha(obj2, 210); Obj_SetRenderPriorityI(obj2, 51);} 
					else{let obj2 = CreateShotA1(0, 8 + 424/numEntries *i, speed, 0, graphic, 5); ObjRender_SetAlpha(obj2, 210); Obj_SetRenderPriorityI(obj, 51);}
				}
				wait(pause);
			}
		}
	}
}






