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

let random = 14;
let random2 = 0;
let illu = false;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4400/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, 140, 15, 6);

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

	Pattern1;
}

@MainLoop{
	SetShotAutoDeleteClip(64, 512, 64, 128);

	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){
			Ring;
			wait(60*6.5);
			random = rand(-30,30);
			random2 = rand(-30,30);
			Ring2;
			wait(60*2);
			Attack = true;
			Charge(60*2);
			wait(60*2);
			EShockwave(GetBX,GetBY, 0.09); 	PlaySE(ExpSE2);	Attack = false;
		}
		yield;
	}
}

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

task Start{
	if(frame == 60){Spell("Weapon Practice \"Crossroad Killing\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		Charge(90);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		EShockwave(GetBX,GetBY, 0.09);
		PlaySE(ExpSE2);
		start=true;
		Attack=false;
	}
}

task Ring{
	let angleT = 90+random;
	loop(36){
		ascent(i in 3..10){
			let shot = CreateShotA2(GetBX, GetBY, i, angleT, -0.1, 0, BALL_S_GRAY, 0);
			Commands(shot);
			ReflectWalls2(shot, 1);
			//ReflectCeiling2(shot, 1);
		}
		angleT += 360/38;
	}
}
task Ring2{
	let angleT = 90+random;
	loop(60*4/5){
		while(Obj_IsDeleted(objBoss)){ yield; }
		Ring22(angleT); // i have no clue why it wouldn't work without this shitty workaround
		wait(5);
	}
}
task Ring22(angleT){
	loop(36){
			let shot = CreateShotA1(GetBX, GetBY, 8, angleT, BALL_S_GRAY, 0);
			ObjRender_SetAlpha(shot, 100); ObjShot_SetIntersectionEnable(shot, false);
			ObjShot_SetSpellResist(shot, true);
			//Commands(shot);
			//ReflectWalls(shot, 1);
			angleT += 360/38;
		}
}
task Commands(shot){
		wait(60*1);
		let speed = ObjMove_GetSpeed(shot); let x = ObjMove_GetX(shot); let y = ObjMove_GetY(shot); let angle = ObjMove_GetAngle(shot); 
		wait(60*0.5);
		ObjMove_SetAcceleration(shot, rand(0.005,0.01)); ObjMove_SetAngle(shot, rand(80,100)); ObjMove_SetMaxSpeed(shot, 2);
		ObjShot_SetGraphic(shot, BALL_S_BLUE); //if(Obj_GetValue(shot, "IsCorner") == 1){ ObjShot_SetGraphic(shot, CIRCLE_GREEN); }
		PlaySE(ShotSE2);
		wait(60*5);
		ObjRender_SetAlpha(shot, 150); ObjShot_SetIntersectionEnable(shot, false); illu = true; SetCommonData("isBirb", 1);
		//ObjShot_SetGraphic(shot, BALL_BS_PURPLE); //if(Obj_GetValue(shot, "IsCorner") == 1){ ObjShot_SetGraphic(shot, CIRCLE_GREEN); }
		//ObjMove_SetDestAtWeight(shot, GetPlayerX, GetPlayerY, 24, 12);
		ObjMove_SetSpeed(shot, 0); ObjMove_SetAcceleration(shot, 0.01); ObjMove_SetMaxSpeed(shot, 3); ObjShot_SetGraphic(shot, KUNAI_S_PURPLE);
		PlaySE(SlashSE2);
		loop((60*4-30)/5){
			ObjMove_SetAngle(shot, GetAngleToPlayer(shot)); 
			wait(5);
		}
		wait(30);
		//wait(60*2);
		//ObjMove_SetAngle(shot, angle);
		//wait(60*1);
		ObjRender_SetAlpha(shot, 255); ObjShot_SetIntersectionEnable(shot, true); illu = false; SetCommonData("isBirb", 0);
		ObjMove_SetSpeed(shot, 5); ObjMove_SetMaxSpeed(shot, 5); ObjShot_SetGraphic(shot, KUNAI_S_PURPLE); //ObjMove_SetAngle(shot, angle); //ObjShot_SetGraphic(shot, BALL_S_GRAY);
}

task ReflectWalls2(obj, bounces){
	loop{
		if(illu){ break; }
			while(bounces <= 0){yield;}
			if(ObjMove_GetX(obj) >= GetStgFrameWidth){
				ObjMove_SetX(obj, GetStgFrameWidth);
				ObjMove_SetAngle(obj, 180-ObjMove_GetAngle(obj));
				bounces -= 1;
			}
			else if(ObjMove_GetX(obj) <= 0){
				ObjMove_SetX(obj, 0);
				ObjMove_SetAngle(obj, 180-ObjMove_GetAngle(obj));
				bounces -= 1;
			}
		yield;
	}
}

task ReflectCeiling2(obj, bounces){
	loop{
		if(illu){ break; }
		while(bounces <= 0){yield;}
		if(ObjMove_GetY(obj) <= GetStgFrameTop){
			ObjMove_SetAngle(obj, -1*ObjMove_GetAngle(obj));
		}
		yield;
	}
}