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

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

let phase = 0;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(3500/4);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(75);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000);
	}
}

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

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

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

	Pattern1;
	//SetPhase;
}

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

	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, GetBX, GetBY, 16);
		ObjEnemy_SetIntersectionCircleToShot(objBoss, GetBX, GetBY, 24);
	}

	ObjEnemy_SetDamageRate(objBoss, 1, 1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss, 25, 10);
	}
	
	Start;

	frame++;
	timer += 1/GetCurrentFps;

	yield;
}

task Pattern1{
	loop{
		if(start){
			Jump;
			wait(60*3*6);
			Charge(60); PlaySE(ChargeSE);
			wait(30);
			if(start){ ObjMove_SetDestAtWeight(objBoss, cx, 60, 15, 12); }
			wait(30);
			if(start){ FireReflect; }
			wait(30);
			if(start){ FireButterflies; }
			wait(60*2);
			if(start){ ObjMove_SetDestAtWeight(objBoss, GetPlayerX+rand(-100,100), GetPlayerY-rand(60,120), 15, 12); }
			FireJump1(2); FireJump2(2);
			wait(30);
			Camera;
			wait(60*4);
			if(start){ if(GetBX < cx){ ObjMove_SetDestAtWeight(objBoss, 10, 80, 15, 6); }
			else{ ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth-10, 80, 15, 6); } }
			wait(60);
			if(start){ Charge(60); } // god just please dont fuckign 0,0 
			wait(60);
		}
		yield;
	}
}

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

task Start{
	if(frame == 60){
		Spell("Tengu Camera \"Ghost Photography\"", id);
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth-10, 80, 15, 6);
		wait(60);
		Charge(90);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		EShockwave(GetBX,GetBY, 0.09);
		PlaySE(ExpSE2);
		start=true;
		Attack=false;
	}
}

task SetPhase{
	loop{
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) < 25){
			start = false;
			Charge(90);
			Attack = true;
			wait(90);
			phase = 1;
			start = true;
			break;
		}
		yield;
	}
}

task Jump{
	let dirx = 2; // 1 is left and 2 is right
	let diry = 1; // // 1 is down and 2 is up

	let delay = 3;

	if(GetBX < cx){ dirx = 1; }
	else{ dirx = 2; }

	loop(6){
		while(!start){ yield; }
		if(dirx == 1 && diry == 1){
			ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth-10, GetBY+rand(80,120), 15, 12);
			FireJump1(1); FireJump2(1);
			Camera;
			dirx = 2;
		}
		else if(dirx == 2 && diry == 1){
			ObjMove_SetDestAtWeight(objBoss, 10, GetBY+rand(80,120), 15, 12);
			FireJump1(1); FireJump2(1);
			Camera;
			dirx = 1;
		}
			else if(dirx == 1 && diry == 2){
			ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth-10, GetBY-rand(80,120), 15, 12);
			FireJump1(1); FireJump2(1);
			Camera;
			dirx = 2;
		}
		else if(dirx == 2 && diry == 2){
			ObjMove_SetDestAtWeight(objBoss, 10, GetBY-rand(80,120), 15, 12);
			FireJump1(1); FireJump2(1);
			Camera;
			dirx = 1;
		}

		if(GetBY > GetStgFrameHeight-200){ diry = 2; }
		else if(GetBY < 200){ diry = 1; }
		if(phase == 1){ delay = 2; }

		wait(60*3);
	}
}

task FireJump1(type){
	let angleT = rand(0,360);
	PlaySE(ShotSE1);

	loop(128){
		let shot = CreateShotA1(GetBX+30*cos(angleT), GetBY+30*sin(angleT), rand(1,5)/type, angleT, SCALE_RED, 10);
		ObjMove_SetAngularVelocity(shot, rand(-0.2,0.2));
		angleT += 360/128;
	}
}
task FireJump2(type){
	let angleT = rand(0,360);
	PlaySE(ShotSE2);

	loop(128){
		ascent(i in 1..5){
				let shot = CreateShotA1(GetBX+20*cos(angleT), GetBY+20*sin(angleT), i/type, angleT, BULLET_BLUE, 10);
				Commands(shot);
			}
		angleT += 360/128;
	}
}
task Commands(shot){
	while(!Obj_IsDeleted(shot)){
		if(illu){ ObjShot_SetIntersectionEnable(shot, false); ObjRender_SetAlpha(shot, 50);   }
		else{ ObjShot_SetIntersectionEnable(shot, true); ObjRender_SetAlpha(shot, 255);   }
		yield;
	}
}

task Camera{
	let scl = 5;
	let delay = 2;
	if(phase == 1){ delay = 1; }

	wait(60);

	PlaySE(FocusSE);

	Attack = true;

	let angle = GetAngleToPlayer(objBoss);

	illu = true; PlaySE(SlashSE2);
	if(start){ SetCommonData("isBirb", 1); }

	let pht = ObjEnemy_Create(OBJ_ENEMY); ObjEnemy_Regist(pht);
	ObjPrim_SetTexture(pht, GetCurrentScriptDirectory ~ "./data/img/photo.png");
	ObjSprite2D_SetSourceRect(pht, 0,0,64,64);
	ObjSprite2D_SetDestCenter(pht);
	Obj_SetRenderPriorityI(pht, 30); 
	ObjRender_SetBlendType(pht, BLEND_ADD_ARGB);
	ObjRender_SetScaleXYZ(pht, scl, scl, 0);
	ObjRender_SetAngleXYZ(pht, 0, 0, 90);
	ObjMove_SetPosition(pht, GetBX, GetBY);

	ObjMove_SetSpeed(pht, 0.25);
	ObjMove_SetAcceleration(pht, 0.03);
	ObjMove_SetMaxSpeed(pht, 3);

	let plrcircle;

	loop(60*delay){
		while(!start){ ObjMove_SetMaxSpeed(pht, 0); Obj_SetVisible(pht, false); yield; }
		if(GetObjectDistance(GetPlayerObjectID, pht) < ObjMove_GetSpeed(pht)){ ObjMove_SetPosition(pht, GetPlayerX, GetPlayerY); }
		else{ ObjMove_SetAngle(pht, atan2(GetPlayerY - ObjMove_GetY(pht), GetPlayerX - ObjMove_GetX(pht))); }
		ObjRender_SetScaleXYZ(pht, scl, scl, 0);
		if(scl > 1.5){ scl -= 0.03; }
		yield;
	}
	let l1 = CreateStraightLaserA1(ObjMove_GetX(pht), ObjMove_GetY(pht)-20*scl, 90, 40*scl, 128*scl*3, 1, BEAM_RED, 0);
	let l2 = CreateStraightLaserA1(ObjMove_GetX(pht)-28*scl, ObjMove_GetY(pht), 0, 56*scl, 128*scl*3, 1, BEAM_RED, 0);
	ObjLaser_SetIntersectionWidth(l1, 128*scl*3); 
	ObjLaser_SetIntersectionWidth(l2, 128*scl*3); 

	PlaySE(ShutterSE);

	if(GetPlayerX > ObjMove_GetX(pht)-28*scl && GetPlayerX < ObjMove_GetX(pht)+28*scl){
		if(GetPlayerY > ObjMove_GetY(pht)-20*scl && GetPlayerY < ObjMove_GetY(pht)+20*scl){
			let whydnhnosetplayerstatefunction = CreateShotA1(GetPlayerX, GetPlayerY, 0, 0, MIKO_ORB_RED, 0);
			ObjShot_SetDeleteFrame(whydnhnosetplayerstatefunction, 1);
			Obj_SetVisible(whydnhnosetplayerstatefunction, false);
		}
	}
	
	illu = false; SetCommonData("isBirb", 0);
	Obj_Delete(pht);

	wait(3);
	Obj_SetVisible(l1, false);
	Obj_SetVisible(l2, false);

	Attack = false;
}

task FireButterflies{
	let angleT = rand(0,360);
	let a = 0;
	loop(60*2/4){
	while(Obj_IsDeleted(objBoss)){ yield; }
	loop(32){
		let shot1 = CreateShotA1(GetBX+30*cos(angleT+a), GetBY+30*sin(angleT+a), 6, angleT+a, BUTTERFLY_RED, 45);
		angleT += 360/32;
	}
		a += 106.88;
		wait(4);
		PlaySE(ShotSE1);
	}
}

task FireReflect{
	let angle1 = rand(140,150);
	let angle2 = rand(30,40);

	ascent(i in 5..30){
		let shot1 = CreateShotA1(GetBX, GetBY, i/4, angle1, BALL_BS_RED, 10);
		let shot2 = CreateShotA1(GetBX, GetBY, i/4, angle2, BALL_BS_RED, 10);
		ReflectWalls(shot1, 1);
		ReflectWalls(shot2, 1);
	}

	PlaySE(ShotSE3);
}