#TouhouDanmakufu
#Title["Tri Edge"]
#Text[Arcadiae arcanum.]
#Image[snapshot\triedge.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh]
#PlayLevel[Normal]
#BGM[bgm\Night Falls ~ Evening Star.mp3]
#ScriptVersion[2]

script_enemy_main {
	#include_function "lib\SHOT_REPLACE\shot_replace.dnh"
	let ImgBoss = "script\img\stg8enm.png";
	let frame = -120;
	let ang = 0;
	let turnswitch = 1;
	let velo = 0;
	let freq = 120;
	let fire = false;
	let fire2 = false;
	let count = 0;
	let sc = 0;
	let m = -1;
	let sprayx = -150;
	let turnmarker = 1;
	let trig = 0;
	let delay = 360;
	@Initialize {
		SetLife(700);
		SetInvincibility(360);
		SetDamageRate(20, 10);
		SetTimer(51);
		SetScore(333333);
		CutIn(KOUMA, ""\""Tri Edge"\", "script\img\jeaneandarcadia.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		psrand(747030);
		shotinit;

		SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
	}
	@MainLoop {
		SetShotAutoDeleteClip(0, 0, 0, 0);
		if(frame%540 < 211 || frame%540 > 361) {
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 16);
		}
		if(frame%540 == 0) {
			delay = 360;
		}
		else if(frame%540 == 1) {
			TriEdger(GetClipMinX, GetClipMinY, 10, atan2(GetClipMaxY - GetClipMinY, GetClipMaxX - GetClipMinX), WHITE51, RED04);
		}
		else if(frame%540 == 61) {
			TriEdger(GetClipMaxX, GetClipMinY, 10, atan2(GetClipMaxY - GetClipMinY, GetClipMinX - GetClipMaxX), WHITE51, BLUE04);		
		}
		else if(frame%540 == 121) {
			Concentration01(90);
		}
		else if(frame%540 == 241) {
			CreateLaserB(0, 600, 66, WHITE60, 60);
			SetLaserDataB(0, 0, 0, 240, 0, 270, 0, 90, 0);
			ascent(i in 0..60) {
				CreateShotA(1, 0, 0, 0);
				SetShotDataA(1, 0, 0, rand(0, 360), 0, 0.01, rand(1, 3), WHITE04);
				AddShot(120, 0, 1, i * 10);
			}
			SetShotKillTime(0, 121);
			FireShot(0);
		}
		yield;
		delay--;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		if(frame%540 < 211 || frame%540 > 361) {
			DrawGraphic(GetX, GetY);
		}
	}
	@Finalize {
		loop(9) {
			CreateItem(ITEM_SCORE, GetX + rand(-50, 50), GetY - rand(20, 100));
		}
		DeleteGraphic(ImgBoss);
	}
	task TriEdger(x, y, v, angle, graphic1, graphic2) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);	
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic1);
		ObjShot_SetDelay(obj, 0);
		while(Obj_BeDeleted(obj) == false) {
			PerfectFreezeGlobal(Obj_GetX(obj), Obj_GetY(obj), 0, 0, 0.01, rand(1, 3), WHITE04, graphic2);
			yield;
		}
	}
	task PerfectFreezeGlobal(x, y, sv, decel, accel, ev, graphic1, graphic2) {
		let obj = Obj_Create(OBJ_SHOT);
		let thaw = false;
		let dc = 30;
		Obj_SetPosition(obj, x, y);	
		Obj_SetSpeed(obj, sv);
		Obj_SetAngle(obj, rand(0, 360));
		ObjShot_SetGraphic(obj, graphic1);
		ObjShot_SetDelay(obj, dc);
		while(Obj_BeDeleted(obj) == false) {
			while(dc > 0) {
				dc--;
				yield;
			}
			while(Obj_GetSpeed(obj) > 0) {
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) + decel);
				yield;
			}
			while(thaw == false) {
				if(delay <= 0) {
					thaw = true;
				}
				yield;
			}
			CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 0, rand(0, 360), accel, ev, graphic2, 0);
			Obj_Delete(obj);
			yield;
		}
	}
	task CreateShot03(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let lifecounter = 10;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetBombResist(obj, true);
		ObjShot_SetDelay(obj, delay);
		while(Obj_BeDeleted(obj) == false) {
			lifecounter--;
			if(lifecounter <= 0) {
				Obj_SetAutoDelete(obj, true);
			}
			yield;
		}
	}
}