/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Aya (Nonspell 1)"]
#Text["bird is watching"]
#System["./data/system/Awalys_System.dnh"]
#Player["./data/player/Reisen/player.dnh"]
#Background["./data/background/back_miko.dnh"]*/

#include"./data/shot/CrispBright4/Shotconstants.dnh"
#include"./data/lib/lib_main.dnh"
#include"./data/lib/Boss_Anim.dnh"
#include"./data/lib/shot_lib.dnh"

let frame = 0;
let time = 0;
let timer = 0;
let move = 0;
let angleInc = 0;
let inc = 0;

let illu = false;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000);
	}
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

    ObjMove_SetPosition(objBoss, cx, 120);

	TFinalizeNonspell;
	StartSlow(TARGET_ALL, 60);
	BossMarker;
	MagicCircle(objBoss);
	BossRender;
	Name("Shameimaru Aya");

	Pattern1; //Pattern2;
	Movement(90);
}

@MainLoop{
	SetShotAutoDeleteClip(256, 256, 256, 256);

	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, GetBX, GetBY, 16);
	ObjEnemy_SetIntersectionCircleToShot(objBoss, GetBX, GetBY, 24);

	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss, 100, 50);
	}
	
	Start;

	frame++;

	yield;
}

task Pattern1{
	loop{
		if(start){
			FireFans; Attack = true;
			wait(60*1);
			Attack = false;
			wait(60*1);
			FireFans; 
			wait(60*0.5);
			Charge(90);
			wait(60*1.5);
			FireCircles;
			wait(60*5);
		}
		yield;
	}
}
task Pattern2{
	loop{
		if(start){
			wait(60*2);
			//FireRing;
		}
		yield;
	}
}

task Charge(frame){
	nTestCharge(GetBX, GetBY, frame, 255, 205, 50);
	PlaySE(ChargeSE);
}

task Start{
	if(frame == 60){
		Charge(90);
		Attack = true;
		wait(100);
		EShockwave(GetBX,GetBY, 0.09);
		PlaySE(ExpSE2);
		/*FireFans; PlaySE(ShotSE4);
		wait(60*1);
		Charge(60);
		wait(60);
		EShockwave(GetBX,GetBY, 0.09);
		PlaySE(ExpSE2);*/
		start=true;
		Attack=false;
	}
}

task Movement(time){
	loop{
		if(start){
			wait(time);

			if(ObjMove_GetX(objBoss) < 200){
				ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(objBoss)+100, ObjMove_GetY(objBoss)+rand(-10,5), 15,6);
			}
			else if(ObjMove_GetX(objBoss) > GetStgFrameWidth-200){
				ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(objBoss)-100, ObjMove_GetY(objBoss)+rand(-10,5), 15,6);
			}
			else{
				ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(objBoss)+rand(-100,100), ObjMove_GetY(objBoss)+rand(-10,5), 15,6);
			}

			wait(time);
		}
		yield;
	}
}

task FireFans{
	while(Obj_IsDeleted(objBoss)){ yield; }
	let fam1 = CreateFamiliarA2(GetBX-60, GetBY-20, 0);
	let fam2 = CreateFamiliarA2(GetBX+60, GetBY-20, 0);

	let angleT1 = GetAngleToPlayer(objBoss);
	let angleT2 = GetAngleToPlayer(objBoss);
	PlaySE(ShotSE4);
	ascent(i in 2..8){
		ascent(j in 0..5){
			CreateShotA1(GetBX, GetBY, i, angleT1+j*30, SCALE_RED, 10);
			CreateShotA1(GetBX, GetBY, i, angleT1-j*30, SCALE_RED, 10);
		}
	}
	wait(15);
	while(Obj_IsDeleted(objBoss)){ yield; }

	angleT1 = GetAngleToPlayer(fam1); angleT2 = GetAngleToPlayer(fam2);
	ascent(i in 2..8){
		ascent(j in 0..5){
			CreateShotA1(ObjMove_GetX(fam1), ObjMove_GetY(fam1), i, angleT1+j*30, SCALE_RED, 10);
			CreateShotA1(ObjMove_GetX(fam1), ObjMove_GetY(fam1), i, angleT1-j*30, SCALE_RED, 10);

			CreateShotA1(ObjMove_GetX(fam2), ObjMove_GetY(fam2), i, angleT2+j*30, SCALE_RED, 10);
			CreateShotA1(ObjMove_GetX(fam2), ObjMove_GetY(fam2), i, angleT2-j*30, SCALE_RED, 10);

		}
	}
	wait(15);
	while(Obj_IsDeleted(objBoss)){ yield; }

	ObjMove_SetPosition(fam1, GetBX-120, GetBY-40); ObjMove_SetPosition(fam2, GetBX+120, GetBY-40);
	angleT1 = GetAngleToPlayer(fam1); angleT2 = GetAngleToPlayer(fam2);

	ascent(i in 2..8){
		ascent(j in 0..5){
			CreateShotA1(ObjMove_GetX(fam1), ObjMove_GetY(fam1), i, angleT1+j*30, SCALE_RED, 10);
			CreateShotA1(ObjMove_GetX(fam1), ObjMove_GetY(fam1), i, angleT1-j*30, SCALE_RED, 10);

			CreateShotA1(ObjMove_GetX(fam2), ObjMove_GetY(fam2), i, angleT2+j*30, SCALE_RED, 10);
			CreateShotA1(ObjMove_GetX(fam2), ObjMove_GetY(fam2), i, angleT2-j*30, SCALE_RED, 10);
		}
	}
	Obj_Delete(fam1); Obj_Delete(fam2);
}

/*task FireBounce{
	let angleT = rand(0,360);

	ascent(i in 0..8){
		let shot1 = CreateShotA2(GetBX, GetBY, 0, angleT, 0.2, 6, BIRD_RED, i*10);
		ReflectWalls(shot1, 3);
		ReflectCeiling(shot1, 3);
	}
}*/

task FireRing{
	let angleT = rand(0,360);

	PlaySE(ShotSE4);
	loop(32){
		ascent(i in 2..4){
			let shot = CreateShotA1(GetBX+20*cos(angleT), GetBY+20*sin(angleT), 7, angleT, BALL_BS_RED, 5*i);
			//ReflectWalls(shot, 1);
		}
		angleT += 360/32;
	}
}

task FireCircles{
	let angleT = 120;
	let angleT2 = rand(0,360);
	let inc = 0;
	//loop(2){
		ascent(i in 1..42){
			while(Obj_IsDeleted(objBoss)){ yield; }
			//angleT2 = rand(0,360);
			loop(12){
				let shot1 = CreateShotA1(GetBX+25*(i*0.15)*cos(angleT2), GetBY+25*(i*0.15)*sin(angleT2), 7-(i*0.13), angleT+i*32, BALL_BS_RED, 5);
				let shot2 = CreateShotA1(GetBX+25*(i*0.15)*cos(angleT2), GetBY+25*(i*0.15)*sin(angleT2), 7-(i*0.13), angleT+i*32+180, BALL_BS_BLUE, 5);
				//ReflectWalls(shot1, 1);
				angleT2 += 360/12;
			}
			PlaySE(ShotSE1);
			wait(5);
		}
		//angleT += 360/2;
	//}
}