//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["laneSpiralNon"]
#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= 50;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let shot = LoadEffect(path~"ATTACK5.WAV", 70);
let explosion = LoadEffect(path~ "SmallExplosion.WAV", 100);

@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);
	SetCommonData("non1", "no");
	
	animNon(objBoss, 1);
	TFinalizeNon(scene, objBoss);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/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{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path ~"Charge.WAV", 80)); let shot = LoadEffect(path ~"ATTACK4.WAV", 70);
	wait(60);
	move(width/3, 2*width/3, height/3, height/2,120, 180, 240, objBoss, 0, scene, 0);
	shotSound(shot, -1, 60, 3, objBoss, false, 0, scene, 0);
	tentacles(1); tentacles(2);
	wait(60);
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 90));
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	wait(160);
	bullets;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV",100));
	loop(10){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
}

task bullets{
    let dir = 1; let x; let y; let pause = true;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    x = width/2 + dir*width/3 + rand(-60,60);
		y = rand(height/5, 2*height/5);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/2 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<= time/2){
		    if(pause){wait(60); pause = false;}
		}
		ObjSound_Play(shot);
	    let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0,0, 399, 0);
		Obj_SetRenderPriority(obj, 0.78);
		ObjMove_SetDestAtFrame(obj, x,y, 60);
		dir*=-1;
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/2){
		    explode(obj, 1); wait(60);
		}
		else{explode(obj,2); wait(rand(30,38));}
	}
}

task explode(obj, cas){
    if(cas==1){loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/2 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<= time/2){Obj_Delete(obj); return;} yield;}}
	else{wait(60);}
	ObjShot_SetGraphic(obj, 400);
	if(cas==1){loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/2 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<= time/2){Obj_Delete(obj); return;} yield;}}
	else{wait(60);}
	if(!Obj_IsDeleted(obj)){ObjSound_Play(explosion);}
    if(cas==1){
		ascent(j in 0..2){
		    ascent(i in -3..4){
			    if(Obj_IsDeleted(obj)){return;}
				let obja=CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, GetAngleToPlayer(obj)+20*i, rand(-0.2,-0.1), 3-1.2*j, 376, 0);
				Obj_SetRenderPriority(obja, 0.78);
			}
		}
	}
	else{
	    let accel = rand(-0.1, -0.05);
	    ascent(i in -4..5){
		    if(Obj_IsDeleted(obj)){return;}
		    let maxi = rand(2,2.6);
			let obja=CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, GetAngleToPlayer(obj)+15*i, accel, maxi, 376, 0);
			Obj_SetRenderPriority(obja, 0.78);
		}
	}
	Obj_Delete(obj);
}

task tentacles(n){
    let dir = 1; let ang =0;
    if(n==1){
	    let t=0;
		while(t<120){
			ascent(i in -1..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj=CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, 45+8*i +ang , 61,0);
				ObjShot_SetSpellResist(obj, true);
			}
			ascent(i in -1..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, 135+8*i +ang, 61,0);
				ObjShot_SetSpellResist(obj, true);
			}
			wait(3); t+=3;
		}
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			ascent(i in -1..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj =CreateShotA1(ObjMove_GetX(objBoss)+5, ObjMove_GetY(objBoss), 5, 45+8*i +ang , 61,0);
				ObjShot_SetSpellResist(obj, true);
			}
			ascent(i in -1..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj=CreateShotA1(ObjMove_GetX(objBoss)-5, ObjMove_GetY(objBoss), 5, 135+8*i +ang, 61,0);
				ObjShot_SetSpellResist(obj, true);
			}
			ang+= 2.5*dir; if(absolute(ang)>=50){dir*=-1;}
			wait(3);
		}
	}
	else{
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			ascent(i in -2..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj = CreateShotA1(ObjMove_GetX(objBoss)+10, ObjMove_GetY(objBoss)-10, 4, -30+20*i -ang , 66,0); 
				ObjShot_SetSpellResist(obj, true);
				Obj_SetRenderPriority(obj, 0.75);
			}
			ascent(i in -1..3){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let obj = CreateShotA1(ObjMove_GetX(objBoss)-10, ObjMove_GetY(objBoss)-10, 4, 220+20*i +ang, 66,0); 
				ObjShot_SetSpellResist(obj, true);
				Obj_SetRenderPriority(obj, 0.75);
			}
			ang+= 5*dir; if(absolute(ang)>=55){dir*=-1;}
			wait(3);
		}
	}
}






