//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["MarioSaulNon"]
#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 = 3600;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";


let charge = LoadEffect(path~ "Charge.WAV", 80);
let shot3 = LoadEffect(path~ "ATTACK3.WAV", 90);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 60);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 60);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 70);
let twinkle = LoadEffect(path~"TWINKLE.WAV", 70);
let wave = LoadEffect(path~"WAVE.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);
	enemy2 = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(enemy2);
	SetPlayerClip(5,5, 415, 435);
	SetCommonData("non1", "no");
	
	animNon(objBoss, 2); Mario;
	TFinalizeNon(scene, objBoss);
	
	Name(2, objBoss);
    Indicator(objBoss, 2);
    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);
	ObjEnemy_SetIntersectionCircleToShot(enemy2, X(enemy2), Y(enemy2), 28);
    yield;
}




task Main{
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	SetForbidPlayerSpell(false);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path ~"Charge.WAV", 80));
    wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);

	move(objBoss, width/3, 2*width/3, height/4, 3*height/10, 0, 90, 30, objBoss, 0, scene, 0);
	wait(160);
	wavyCircle(19, 50, 1.6, 2, 50);
	wait(180);
	ObjSound_Play(LoadEffect("ATTACK3.WAV", 90));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let obj= CreateShotA1(X(objBoss), Y(objBoss), 0,0, 362,0); ObjShot_SetSpellResist(obj, true);
		ObjSound_Play(wave);
		wait(60);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		shotSound(shot5, 60, 60, 6, objBoss, false, 0, scene, 0);
		ascent(i in 0..4){
		    helix(ObjMove_GetX(obj), ObjMove_GetY(obj), 8, GetAngleToPlayer(obj) +90*i, 25, 50, 10, 6,200,200, objBoss,0);
		}
		wait(60);
		Obj_Delete(obj);
		wait(120);
	}
	
}

//task wavy(obj, wvel, pause){

// wvel and pause are arrays with min/ max allowed values.

task wavyCircle(numBul, delay, speed, wvel, pause){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(twinkle);
		let ang= rand(0,360);
		ascent(i in 0.. numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj = CreateShotA1(X(objBoss), Y(objBoss), speed, ang+360/numBul * i, 222, 5);
			wavy(obj, wvel, pause);
		}
		wait(delay);
	}
}

	
	
task drawMario(enemy2){
    let img = GetCurrentScriptDirectory() ~ "../../img/nonMario.png";
	let i=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjPrim_SetTexture(enemy2, img);
		if(i%2==0){ObjSprite2D_SetSourceRect(enemy2, 166, 250, 340, 770);}
		else{ObjSprite2D_SetSourceRect(enemy2, 166+512, 250, 340+512, 770);}
		ObjSprite2D_SetDestRect(enemy2, -30, -60, 30, 60);
		i++;
		wait(20);
	}
}

task Mario{
	drawMario(enemy2); let dir =1; if(probaRoll(0.5)){dir=-1;}
	Obj_SetRenderPriorityI(enemy2, Obj_GetRenderPriorityI(objBoss)-1);
	ObjMove_SetDestAtFrame(enemy2, width/2 +dir * rand(width/5, width/3), rand(height/5, 3*height/10), 60); 
	wait(120); let numBranch; let first = true;
	wait(60);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		if(first){numBranch=30;} else{numBranch=22;}
		shotSound(shot4, 60, 50, 2, objBoss, false, 0, scene, 0);
		let t=0; let ang = rand(0,360);
		sound(30+320/6);
		while(absolute(t)< 300){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj= CreateShotA1(X(enemy2), Y(enemy2), 1.8, ang+360/numBranch *t*dir, 413, 10);
			ObjMove_AddPatternA2(obj, 30+(320-t)/6, NO_CHANGE, NO_CHANGE, 1, 0, 9);
			t++; if(t%6==0){yield;}
		}
		wait(60);
		dir*=-1;  
		ObjMove_SetDestAtFrame(enemy2, width/2 +dir * rand(width/5, 2*width/5), rand(height/5, 0.45*height), 60); 
		wait(180); first = false;
	}
	Obj_Delete(enemy2);
}

task sound(delay){
    wait(delay);
	ObjSound_Play(shot2);
}


task Comm(obj){
    while(X(obj)>0 && X(obj)< height){yield;}
	let ang1 = ObjMove_GetAngle(obj)+90;
	if(ang1<=270){ang1 = 90+ (3*180-2*ang1)/4;}
	else{ang1= 360-ang1;}
	ObjMove_SetAngle(obj, ang1 -90);
}





