//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Random aim"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 4200;
let time= 42;
let scene = GetEnemyBossSceneObjectID();

@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("non2", "no");
	
	
	animNon(objBoss);
	TFinalizeNon(scene, objBoss);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, rand(2*height/5, 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{
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 20, 0);
	wait(180);
	let shot2 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK5.WAV", 50);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/Charge.WAV", 80));
    wait(60); 
	ObjEnemy_SetDamageRate(objBoss, 100, 50);
    let t; let col= [[285, 80], [286, 83], [289, 75], [291, 81]]; let dir;
	let angleGener;
	dir =1; if(rand(0,1)<1/2){dir=-1;}
	angleGener = -90-dir*90;
	let colour = col[truncate(rand(0,4))];
	shotSound(shot2, 60, 70, 2, objBoss, false, 0);
	ascent (i in 0..30){
		ascent(j in 0.. i%2+1){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(1.5,3), angleGener+ 3*dir*i+rand(-40, 40), colour[0], 0);
			ObjShot_SetSpellResist(obj, true);
			shoot(obj, colour[1]);
		}
		wait(2);
	}
	wait(180);
	ObjMove_SetDestAtFrame(objBoss, width/2 + dir*rand(width/5, width/4), rand(0.32*height, 0.4*height), 60);
	wait(90);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    angleGener = -90-dir*90;
	    let colour = col[truncate(rand(0,4))];
		shotSound(shot2, 60, 70, 2, objBoss, false, 0);
	    ascent (i in 0..30){
			ascent(j in 0.. i%2+1){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(1.5,3), angleGener+ 3*dir*i+rand(-40, 40), colour[0], 0);
				ObjShot_SetSpellResist(obj, true);
			    shoot(obj, colour[1]);
			}
			wait(2);
		}
		wait(180);
		dir*=-1; ObjMove_SetDestAtFrame(objBoss, width/2 + dir*rand(width/5, width/4), rand(0.32*height, 0.4*height), 60);
		wait(90);
	}
}


task shoot(obj, colour){
    let shot1 = LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/ATTACK4.WAV", 70);
    wait(rand(30, 100));
	while(ObjMove_GetX(obj)>0 && ObjMove_GetX(obj)<width){
	    ObjSound_Play(shot1);
	    let ang = GetAngleToPlayer(obj)+rand(-40, 40);
	    ascent(i in 0..3){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(1.5,4), ang+120*i, colour, 20);}
		wait(rand(20, 50));
	}
}
	
	
	
	
	