/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Aya (Nonspell 2)"]
#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(4000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000);
	}
}

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

    ObjMove_SetDestAtWeight(objBoss, cx, 90, 15, 6);

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

	Pattern1;
	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){
			FireCircle(1);
			wait(60*4);
			FireCircle(2);
			wait(60*3);
			Charge(60);
			wait(60);
			FireCircle21; FireCircle22;
			wait(60);
			illu = true;
			wait(60*5);
		}
		yield;
	}
}

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

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

task FireCircle(type){
	let angleT = 0;
	let angleT2 = 20;
	let inc = 0;
	loop(60*2/8){
		while(Obj_IsDeleted(objBoss)){ yield; }
		if(type == 1 || type == 3){ inc += 38; }
		else if(type == 2){ inc -= 38; }
		loop(3){
			loop(9){
				let shot1 = CreateShotA1(ObjMove_GetX(objBoss)+25*cos(angleT2), ObjMove_GetY(objBoss)+25*sin(angleT2), 7, angleT+inc, BALL_BS_RED, 0);
				let shot2 = CreateShotA1(ObjMove_GetX(objBoss)+30*cos(angleT2), ObjMove_GetY(objBoss)+30*sin(angleT2), 5, angleT-inc/2, BALL_BS_BLUE, 0);
				angleT2 += 360/9;
			}
			angleT += 360/3;
		}
		PlaySE(ShotSE1);
		wait(8);
	}
}
task FireCircle21{
	let angleT = 0;
	let angleT2 = 180;
	let inc = 0;
	loop(60*1/8){
		while(Obj_IsDeleted(objBoss)){ yield; }
		inc += 38;
		loop(6){
			loop(9){
				let fam1 = CreateFamiliarA3(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 7, angleT+inc, 0);
				let shot1 = CreateShotA1(ObjMove_GetX(fam1)+25*cos(angleT2), ObjMove_GetY(fam1)+25*sin(angleT2), 7, angleT+inc, BALL_BS_RED, 0);
				Commands(shot1, fam1, angleT2);
				angleT2 += 360/9;
			}
			angleT += 360/6;
		}
		wait(8);
	}
}
task FireCircle22{
	let angleT = 0;
	let angleT2 = 180;
	let inc = 0;
	loop(60*2/8){
		while(Obj_IsDeleted(objBoss)){ yield; }
		inc += 38;
		loop(4){
			loop(9){
				let shot2 = CreateShotA1(ObjMove_GetX(objBoss)+30*cos(angleT2), ObjMove_GetY(objBoss)+30*sin(angleT2), 5, angleT-inc/2, BALL_BS_BLUE, 0);
				angleT2 += 360/9;
			}
			angleT += 360/4;
		}
		PlaySE(ShotSE1);
		wait(8);
	}
}
task Commands(shot, fam1, angleT2){
	let rad = 25;
	let alpha = 50;
	let inc = 0;
	let inc2 = 0;

	while(!illu){ yield; }

	PlaySE(SlashSE2); SetCommonData("isBirb", 1);
	ObjShot_SetSpellResist(shot, true);
	ObjRender_SetAlpha(shot, alpha); ObjShot_SetIntersectionEnable(shot, false); ObjShot_SetGraphic(shot, BALL_BS_PURPLE);
	ObjMove_SetSpeed(shot, 0.1); ObjMove_SetSpeed(fam1, 0.1);
	//EffectTrail1(shot, BALL_BS_PURPLE, 3, BLEND_ALPHA, alpha, 1);
		loop(60*2){
			ObjMove_SetPosition(shot, ObjMove_GetX(fam1)+rad*cos(angleT2+inc), ObjMove_GetY(fam1)+rad*sin(angleT2+inc));
			rad += 0.2;
			if(rad >= 90*0.2){ alpha += 2; ObjRender_SetAlpha(shot, alpha); }
			inc += 10-inc2;
			inc2 += 10/120;
			yield;
		}
	ObjRender_SetAlpha(shot, 255); ObjShot_SetIntersectionEnable(shot, true); ObjShot_SetGraphic(shot, BALL_BS_RED);
	ObjMove_SetSpeed(shot, 0); ObjMove_SetSpeed(fam1, 0); ObjMove_SetAcceleration(shot, 0.1); ObjMove_SetAcceleration(fam1, 0.1); ObjMove_SetMaxSpeed(shot, 5); ObjMove_SetMaxSpeed(fam1, 5);

	ObjShot_SetSpellResist(shot, false);
	illu = false; SetCommonData("isBirb", 0);
	Obj_Delete(fam1);
}

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;
	}
}