/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Aya (Spell 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;
let fan = false;

let id = "spell2";

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3500/4);
	}
	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);

	TFinalizeSpell(id);
	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, 25, 12.5);
	}
	
	Start;

	frame++;

	yield;
}

task Pattern1{
	loop{
		if(start){
			FireRings;
			EShockwave(GetBX,GetBY, 0.09);
			wait(60*3);
			if(fan){ FireFan; }
			wait(60*3);
			FireBullets;
			wait(60*1);
			if(fan){ FireFan; }
			wait(60*2);
			fan = true;
		}
		yield;
	}
}

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

task Start{
	if(frame == 60){
		Spell("Godspeed \"Claws of Lord Enma\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		Charge(90);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		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 FireBullets{
	let angleT = -80;
	PlaySE(ShotSE4);
	loop(24){
		while(Obj_IsDeleted(objBoss)){ yield; }
		ascent(i in 4..20){
			let shot = CreateShotA1(GetBX+30*cos(angleT), GetBY+30*sin(angleT), i/3, angleT, BULLET_BLUE, 5);
			ReflectWalls(shot, 1);
		}
		angleT += 360/24;
		wait(2);
	}
}

task FireFan{
	let angleT = GetAngleToPlayer(objBoss);

	loop(2){
		while(Obj_IsDeleted(objBoss)){ yield; }
		angleT = GetAngleToPlayer(objBoss);
		PlaySE(ShotSE4);
		ascent(i in 3..7){
			ascent(j in 0..6){
				let shot1 = CreateShotA1(GetBX, GetBY, i, angleT+j*15, BALL_BS_RED, 10);
				let shot2 = CreateShotA1(GetBX, GetBY, i, angleT-j*15, BALL_BS_RED, 10);
			}
		}
		wait(60);
	}
}

task FireRings{
	let angleT = GetAngleToPlayer(objBoss)+(360/24/2);
	PlaySE(ShotSE3);

	let random1 = rand(2, 8);
	let random2 = rand(2, 8);

	loop(24){
		ascent(i in 1..16){
			let shot1 = CreateShotA1(GetBX+10*cos(angleT), GetBY+10*sin(angleT), i*2, angleT+rand(-1,1), BULLET_RED, 5);
			let shot2 = CreateShotA1(GetBX+10*cos(angleT), GetBY+10*sin(angleT), i*2, angleT+rand(-1,1), BULLET_RED, 5);
			Commands(shot1, 1, random1);
			Commands(shot2, 2, random2);
		}
		angleT += 360/24;
	}
}
task Commands(shot, type, random){
	wait(12);

	illu = true; SetCommonData("isBirb", 1); PlaySE(SlashSE2);
	let spd = ObjMove_GetSpeed(shot); let ang = ObjMove_GetAngle(shot);
	ObjRender_SetAlpha(shot, 75); ObjShot_SetGraphic(shot, BULLET_PURPLE); ObjMove_SetSpeed(shot, 0); 
	ObjShot_SetSpellResist(shot, true); ObjShot_SetIntersectionEnable(shot, false);

	let inc = 5;

	loop(60*2){
		if(type == 1){  ObjMove_SetX(shot, ObjMove_GetX(shot)-inc); }
		else if(type == 2){ ObjMove_SetX(shot, ObjMove_GetX(shot)+inc); }
		ObjMove_SetY(shot, ObjMove_GetY(shot)+random);

		random -= random/60*2;
		inc -= inc/60*2;
		yield;
	}
	ObjMove_SetSpeed(shot, spd); ObjShot_SetIntersectionEnable(shot, true);
	ObjRender_SetAlpha(shot, 255); ObjShot_SetGraphic(shot, BULLET_RED);
	illu = false; SetCommonData("isBirb", 0);
	PlaySE(ShotSE3);
}