//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["JamesDaveNon"]
#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 enemy2;
let width = 420;
let height = 440;
let life = 3800;
let limit =0.6*life;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";

let charge = LoadEffect(path~ "Charge.WAV", 80);
let twinkle = LoadEffect(path~ "TWINKLE.WAV", 70);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 60);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 45);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);


@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);
	enemy2 = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(enemy2);
	SetPlayerClip(5,5, 415, 435);
	SetCommonData("non1", "no");
	
	animNon(objBoss, 3); James;
	TFinalizeNon(scene, objBoss);
	
	Name(3, objBoss);
    Indicator(objBoss, 3);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height*0.3, 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);
	wait(100);
	SetForbidPlayerSpell(false);
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
	wait(20);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path ~"Charge.WAV", 80));
    wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	wait(120);
	
	move(objBoss, width/3, 2*width/3, height/4, 2*height/5, 0, 180, 30, objBoss, 0, scene, 0); let t=0;
	let ang = rand(0,360); circle;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjSound_Play(shot4);
		ascent(i in 0..6){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
				CreateShotA1(X(objBoss), Y(objBoss), 2, ang+ 60*i +t, 47, 5); 
				CreateShotA1(X(objBoss), Y(objBoss), 2, ang+ 60*i -t, 47, 5);
			}
		}
		t+= rand(15, 17);
		wait(14);
	}
}
	

task circle{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>2*time/3){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 90));
	let numBul = 18;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let ang = rand(0,360);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot2);
		ascent(i in 0..numBul){CreateShotA2(X(objBoss), Y(objBoss), 1, ang+ 360/numBul *i, 0.2, 4, 321, 5);}
		wait(40);
	}
}
    	
	
	
task drawJames(enemy2){
    let img = GetCurrentScriptDirectory() ~ "../../img/NonSpell.png";
	let i=0; ObjPrim_SetTexture(enemy2, img);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		if(i%2 ==0){
			ObjSprite2D_SetSourceRect(enemy2, 316, 70, 436, 410); 
			ObjSprite2D_SetDestRect(enemy2, -25, -70, 25, 70);
		}
		else{
			ObjSprite2D_SetSourceRect(enemy2, 25, 125, 180, 420); 
			ObjSprite2D_SetDestRect(enemy2, -43, -70, 43, 70);
		}
		
		i++;
		wait(30);
	}
}

// task move(obj, xmin, xmax, ymin, ymax, iniDel, time, delay, objBoss, limit, scene, timeLimit){



task James{
	drawJames(enemy2); 
	Obj_SetRenderPriorityI(enemy2, Obj_GetRenderPriorityI(objBoss)-1);
	ObjMove_SetPosition(enemy2, width/3, height/3);
	ObjMove_SetDestAtFrame(enemy2, width/3, height/2, 60); wait(180);
	let numBul =30; let dir =1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		
		let dist1 = rand(90,100); let dist2 = rand(-30,30);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot5);
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateShotA1(X(enemy2) - dist1*dir +60*cos(360/numBul *i), Y(enemy2) +dist2 + 60*sin(360/numBul *i), 0, 360/numBul *i, 137, 10);
			aim(obj, -dir, 100);
		}
		wait(20);
		dist1 = rand(80,95); dist2 = rand(-30,30);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot5);
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateShotA1(X(enemy2) + dist2 +60*cos(360/numBul *i), Y(enemy2) -dist1 + 60*sin(360/numBul *i), 0, 360/numBul *i, 137, 10);
			aim(obj, 0, 80);
		}
		wait(20);
		dist1 = rand(90,100); dist2 = rand(-30,30);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot5);
		ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateShotA1(X(enemy2) + dist1*dir +60*cos(360/numBul *i), Y(enemy2) +dist2 + 60*sin(360/numBul *i), 0, 360/numBul *i, 137, 10);
			aim(obj, dir, 60);
		}
		wait(30);
		ObjMove_SetDestAtFrame(enemy2, rand(width/4, 3*width/4), rand(height/4, 2*height/5), 60);
		wait(100); dir*=-1;
	}
	Obj_Delete(enemy2);
}


task aim(obj, n, delay){
	if(n==0){
	    let ra = rand(-2,2);
	    loop(10+delay-30){
		    if(!Obj_IsDeleted(obj)){ObjMove_SetAngle(obj, GetAngleToPlayer(obj)+ ra); yield;}
			else{yield;}
		}
	}
	else{
		let ra = rand(-20,20);
	    loop(10+delay-30){
		    if(!Obj_IsDeleted(obj)){ObjMove_SetAngle(obj, findAngRad(GetPlayerX + n* 140+rand(-20,20), X(obj), height, Y(obj))[0]); yield;}
			else{yield}
		}
	}
	wait(30);   
	if(!Obj_IsDeleted(obj)){
	    ObjSound_Play(twinkle); 
		ObjMove_SetSpeed(obj, rand(1.5,4));
	}
}












