/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Aya (Nonspell 3)"]
#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(3600);
	}
	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;
	//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){
			FireCircle(1);
			wait(60*1);
			loop(3){ FireFan; wait(45); }
			FireCircle(2);
			wait(60*1);
			loop(3){ FireFan; wait(45); }
			Charge(60); Attack = true;
			wait(60);
			FireCircle21; FireCircle22; Attack = false;
			wait(60);
			SetCommonData("angleT3", 0);
			illu = true;
			wait(60*8);
		}
		yield;
	}
}
task Pattern2{
	loop{
		if(start){
			wait(60*2);
			if(!illu){ FireFan; }
		}
		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 Movement(time){
	loop{
		if(start){
			wait(time);

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

			wait(time);
		}
		yield;
	}
}

task FireCircle(type){
	let angleT = 0;
	let angleT2 = 60;
	let inc = 0;
		loop(6){
			ascent(i in 2..7){
				loop(9){
					if(type == 1){ 
						let shot1 = CreateShotA1(ObjMove_GetX(objBoss)+(70-i*10)*cos(angleT2), ObjMove_GetY(objBoss)+(70-i*10)*sin(angleT2), i*1, angleT+i*15, BALL_BS_RED, 0);
						let shot2 = CreateShotA1(ObjMove_GetX(objBoss)+(70-i*10)*cos(angleT2), ObjMove_GetY(objBoss)+(70-i*10)*sin(angleT2), i*0.5, angleT-i*5, BALL_BS_BLUE, 0);
					}
					if(type == 2){ 
						let shot1 = CreateShotA1(ObjMove_GetX(objBoss)+(70-i*10)*cos(angleT2), ObjMove_GetY(objBoss)+(70-i*10)*sin(angleT2), i*1, angleT-i*15, BALL_BS_RED, 0);
						let shot2 = CreateShotA1(ObjMove_GetX(objBoss)+(70-i*10)*cos(angleT2), ObjMove_GetY(objBoss)+(70-i*10)*sin(angleT2), i*0.5, angleT+i*5, BALL_BS_BLUE, 0);
					}
					angleT2 += 360/9;
				}
			}
			angleT += 360/6;
		}
		PlaySE(ShotSE1);
}
task FireCircle21{
	let angleT = 0;
	let angleT2 = 180;
	let angleT3 = 180;
		loop(9){
			ascent(i in 2..7){
				loop(9){
					let fam1 = CreateFamiliarA3(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), i, angleT+i*20, 0);
					let shot1 = CreateShotA1(ObjMove_GetX(fam1)+(90-i*10)*cos(angleT2), ObjMove_GetY(fam1)+(90-i*10)*sin(angleT2), i, angleT+i*20, BALL_BS_RED, 0);
					Commands(shot1, fam1, angleT2, 90-i*10);
					angleT2 += 360/9;
				}
			}
			angleT += 360/9;
		}
}
task FireCircle22{
	let angleT = 0;
	let angleT2 = 180;
	let inc = 0;
		inc += 38;
		loop(9){
			ascent(i in 2..7){
				loop(9){
					let shot2 = CreateShotA1(ObjMove_GetX(objBoss)+(70-i*10)*cos(angleT2), ObjMove_GetY(objBoss)+(70-i*10)*sin(angleT2), i*0.5, angleT-i*15, BALL_BS_BLUE, 0);
					angleT2 += 360/9;
				}
			}
			angleT += 360/9;
		}
		PlaySE(ShotSE1);
}
task FireFan{
	PlaySE(ShotSE4);
	ascent(i in 1..5){
		ascent(j in 0..7){
			let shot1 = CreateShotA1(GetBX, GetBY, 6, GetAngleToPlayer(objBoss)+j*20, SCALE_RED, i*3);
			let shot2 = CreateShotA1(GetBX, GetBY, 6, GetAngleToPlayer(objBoss)-j*20, SCALE_RED, i*3);
			ReflectWalls(shot1, 1); ReflectWalls(shot2, 1);
		}
	}
}
/*task FireFan{
	loop(60*3/2){
		let shot1 = CreateShotA2(GetBX-30, GetBY, rand(6,7), rand(0,360), -0.5, 0, SCALE_RED, 5);
		let shot2 = CreateShotA2(GetBX+30, GetBY, rand(6,7), rand(0,360), -0.5, 0, SCALE_RED, 5);
		Commands2(shot1); Commands2(shot2);
		wait(2);
	}
}*/
task Commands(shot, fam1, angleT2, rad){
	let alpha = 50;
	let inc = 0;
	let inc2 = 0;

	let rad2 = rad;

	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){
		if(rad > 0){ ObjMove_SetPosition(shot, ObjMove_GetX(fam1)+rad2*cos(angleT2+inc), ObjMove_GetY(fam1)+rad2*sin(angleT2+inc)); }
		rad2 -= rad/120;
		if(rad >= 90*0.2){ alpha += 2; ObjRender_SetAlpha(shot, alpha); }
		inc += 10-inc2;
		inc2 += 10/120;
		yield;
	}
	illu = false; SetCommonData("isBirb", 0);
	while(Obj_IsDeleted(shot)){ yield; }
	let angleT3 = angleT2;
	let shot2 = CreateShotA2(ObjMove_GetX(shot), ObjMove_GetY(shot), 0.5, angleT3, 0, 1, BALL_BS_RED, 0);
	ObjMove_AddPatternA2(shot2, 60, 0.5, NO_CHANGE, 0.025, 0, 3);
	Obj_Delete(shot);

	//ObjShot_SetSpellResist(shot, false);
	Obj_Delete(fam1);
}