//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["DaveSpell"]
#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 "./../../ZUNShot/shotMathsCons.txt"
#include "./../sharedFuncSamir.dnh"


let objBoss; let bulCancel; let bulCancel2;
let width = 420;
let height = 440;
let life = 3100;
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 lenen = LoadEffect(path~ "LenenShot.WAV", 80);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 60);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let bomb = LoadEffect(path~ "LenenBomb.WAV", 90);
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", "no");
	SetCommonData("spell", "dave");
	
	animSpell(objBoss, 3);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(3, objBoss);
    Indicator(objBoss, 3);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 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{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Chaos sign:[r]Dynamical orbits", objBoss, 3);
    wait(200);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(charge);
	wait(50);
	ObjSound_Play(shot5);
	let obj= CreateShotA2(width/2,height/2, 5, rand(-95,-85), rand(-0.1,-0.07), 0, 399, 0);
	ObjShot_SetSpellResist(obj, true);
	firework(obj, 35, 90, 3.5, [45, 25], 12, [1.2, 1.4], [0.6, 2.5], 10);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(440);
	let dir= 1; if(probaRoll(0.5)){dir=-1;}
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjSound_Play(horn);
		loop(89){
			ascent(i in -1..2){DelayLaser(objBoss, width/2, height/2, GetAngleToPlayer(objBoss) + 50*i,  600, 50, 16, 2);}
			yield;
		}
		let ang = GetAngleToPlayer(objBoss);
		yield;

		ObjSound_Play(horn);
		ascent(i in -1..2){
			DelayLaser(objBoss, width/2, height/2, ang + 50*i,  600, 50, 16, 60);
		}
		wait(60);
		ObjSound_Play(shot5);
		ascent(i in -1..2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj= CreateShotA2(width/2,height/2, 5, ang + 50*i, rand(-0.1,-0.07), 0, 399, 0);
			ObjShot_SetSpellResist(obj, true);
			firework(obj, 22, 80, 3, [20, 18], 8, [0.8,1], [0.3,1.6], 8);
		}
		
		wait(260);
		
		let x = width/2 + dir* rand(width/10, width/4); let y = rand(height*0.3, 2*height/5);
		sflash(x,y, [0.5,2], 30);
		loop(70){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){yield;}}
		
		x = width/2 - dir* rand(width/10, width/4);  y = rand(height*0.3, 2*height/5);
		sflash(x,y, [0.5,2], 30);
		loop(70){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){yield;}}
		
		x = width/2 + dir* rand(width/10, width/4);  y = rand(3*height/5, height*0.7);
		sflash(x,y, [0.5,2], 30);
		loop(70){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){yield;}}
		
		x = width/2 - dir* rand(width/10, width/4);  y = rand(3*height/5, 0.7*height);
		sflash(x,y,[0.5,2], 30);
		wait(70);
		ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 30);
		wait(90);
	}
}


task sflash(x, y, speed, numBul){
	let angle = findAngRad(x, X(objBoss), y, Y(objBoss))[0];
	let rad = findAngRad(x, X(objBoss), y, Y(objBoss))[1];
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	ObjSound_Play(horn);
	DelayLaser(objBoss, X(objBoss), Y(objBoss), angle, rad, 40, 3, 40);
	wait(40);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	ObjSound_Play(flash);
	ObjMove_SetDestAtFrame(objBoss, x, y, 10);
	wait(10);
	ObjSound_Play(shot5);
	loop(numBul){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		CreateShotA1(X(objBoss), Y(objBoss), rand(speed[0], speed[1]), rand(0,360), 271, 10);
	}
}


//numBul[0] is number of bullets per random wave, numBul[1] is number of bullets per circle.

task firework(obj, numLas, lasLength, speedLas, numBul, numWave, speed1, speed2, pause){
    while(ObjMove_GetSpeed(obj)>0){yield;}
	let graph = [399, 400];
	ascent(i in 1 ..4){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	    ObjSound_Play(lenen); ObjShot_SetGraphic(obj, graph[i%2]); wait(30);
	}
	ObjSound_Play(charge);
	wait(60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
	let ang = rand(0,360);
	ObjSound_Play(bomb);
	ScreenShake(numWave*pause, 10);
	falseLaser(obj, ang, numLas, lasLength, speedLas);
	ascent(i in 0..numWave){
	    if(i%2 ==1){
		    ascent(j in 0.. numBul[1]){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				CreateShotA2(X(obj), Y(obj), 5, ang + 360/numBul[1] *j, -0.2, rand(speed1[0], speed1[1]), 226, 20);
			}
			ang+= rand(6,9);
		}
	    ascent(j in 0.. numBul[0]){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			CreateShotA2(X(obj), Y(obj), rand(4,6), rand(0,360), rand(-0.3,-0.05), rand(speed2[0], speed2[1]), 102, 20);
		}
		wait(pause);
	}
	Obj_Delete(obj);
}


task falseLaser(obj, ang, numLas, lasLength, speedLas){
    let x = X(obj); let y = Y(obj);
    loop(lasLength){
	    ascent(i in 0..numLas){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj = CreateShotA1(x, y, speedLas, ang+ 360/numLas *i, 234, 0);
			ObjShot_SetSpellResist(obj, true);
		}
		yield;
	}
}
			
			
			
			
			
			
			
