#TouhouDanmakufu
#Title["Danmaku Bounded Field"]
#Text[Procellae Yucarisque arcanum.]
#Image[]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.dnh]
#PlayLevel[Last Word]
#BGM[bgm\Nantettatte Densetsu.mp3]
#ScriptVersion[2]

script_enemy_main {
	#include_function "lib\SHOT_REPLACE\shot_replace.dnh"
	let ImgBoss = "script\img\stg8enm.png";
	let SeShot = "script\se\tan02.wav";
	let ImgFamiliar = "script\img\circle6_s.png";
	let frame = -120;
	let trig = 0;
	let movecounter = 0;
	let r = 330;
	let rm = 0.77;
	let end = false;
	let launch = false;
	let freq = 33;
	let ln = 4;
	let vm = 0.5;
	let bm = 0;
	@Initialize {
		SetLife(6666);
		SetTimer(78);
		SetScore(1111111);
		CutIn(KOUMA, ""\""Danmaku Bounded Field"\", "script\img\procella.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		LoadSE(SeShot);
		LoadGraphic(ImgFamiliar);
		LastSpell;
		SetDurableSpellCard;
		Expert;

		psrand(707491);
		shotinit;
		SetMovePosition02(GetCenterX, GetCenterY, 120);
	}
	@MainLoop {
		if(movecounter < 11) {
			if(frame%420 == 1) {
				end = false;
				launch = false;
				r = 330;
				ascent(a in 0..ln) {
					if(movecounter == 10) {
						SetEffectForZeroLife(66, 182, 3);
						DanmakuKekkai1(a * 360 / ln, RED21 + a);
						DanmakuKekkai2(a * 360 / ln, RED21 + a + 4);
					}
					else if(movecounter%2 == 0) {
						DanmakuKekkai1(a * 360 / ln, RED21 + bm + a%2);
					}
					else {
						DanmakuKekkai2(a * 360 / ln, RED21 + bm + a%2);
					}
				}
				if(freq <= 4) {
					vm += 0.125;
				}
				if(bm < 5) {
					bm += 2;
				}
				else {
					bm = 0;
				}
				movecounter++;
			}
		}
		if(frame%420 == 241) {
			end = true;
		}
		if(frame%420 == 301) {
			launch = true;
			if(freq > 5) {
				freq = freq - 3;
			}
		}
		if(!end && frame > 0) {
			r-=rm;
		}
		yield;
		frame++;
	}
	task DanmakuKekkai1(sa, graphic) {
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 0;
		let var = 0;
		ObjEffect_SetTexture(obj, ImgFamiliar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 64, 0);
		ObjEffect_SetVertexUV(obj, 2, 64, 64);
		ObjEffect_SetVertexUV(obj, 3, 0, 64);
		while(Obj_BeDeleted(obj) == false) {
			if(freq <= 4) {
				KekkaiDanmaku1(Obj_GetX(obj), Obj_GetY(obj), 4, graphic, var);
				var += vm;
			}
			else if(frame%freq * 2.5 < freq) {
				KekkaiDanmaku1(Obj_GetX(obj), Obj_GetY(obj), 4, graphic, 0);
			}
			ObjEffect_SetVertexXY(obj, 0, -32, -32);
			ObjEffect_SetVertexXY(obj, 1, 32, -32);
			ObjEffect_SetVertexXY(obj, 2, 32, 32);
			ObjEffect_SetVertexXY(obj, 3, -32, 32);
			ObjEffect_SetAngle(obj, 0, 0, ga);
			Obj_SetPosition(obj, GetX + cos(sa) * r, GetY + sin(sa) * r);
			sa += 1.82;
			ga += 2;
			if(end) {
				Obj_Delete(obj);
			}
			yield;
		}
		yield;
	}
	task DanmakuKekkai2(sa, graphic) {
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 0;
		let var = 0;
		ObjEffect_SetTexture(obj, ImgFamiliar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 64, 0);
		ObjEffect_SetVertexUV(obj, 2, 64, 64);
		ObjEffect_SetVertexUV(obj, 3, 0, 64);
		while(Obj_BeDeleted(obj) == false) {
			if(freq <= 4) {
				KekkaiDanmaku1(Obj_GetX(obj), Obj_GetY(obj), 4, graphic, var);
				var -= vm;
			}
			else if(frame%freq * 2.5 < freq) {
				KekkaiDanmaku1(Obj_GetX(obj), Obj_GetY(obj), 4, graphic, 0);
			}
			ObjEffect_SetVertexXY(obj, 0, -32, -32);
			ObjEffect_SetVertexXY(obj, 1, 32, -32);
			ObjEffect_SetVertexXY(obj, 2, 32, 32);
			ObjEffect_SetVertexXY(obj, 3, -32, 32);
			ObjEffect_SetAngle(obj, 0, 0, ga);
			Obj_SetPosition(obj, GetX + cos(sa) * r, GetY + sin(sa) * r);
			sa -= 1.82;
			ga += 2;
			if(end) {
				Obj_Delete(obj);
			}
			yield;
		}
		yield;
	}
	function Obj_IsOffScreen(obj) {
		return Obj_GetX(obj) < GetClipMinX || Obj_GetY(obj) < GetClipMinY || Obj_GetX(obj) > GetClipMaxX || Obj_GetY(obj) > GetClipMaxY;
	}
	task KekkaiDanmaku1(x, y, v, graphic, var) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, atan2(GetY - y, GetX - x));
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetBombResist(obj, true);
		ObjShot_SetGraphic(obj, graphic);
		while(!launch) {
			if(Obj_GetSpeed(obj) > 0) {
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) - 0.1);
			}
			else {
				Obj_SetAngle(obj, atan2(GetY - y, GetX - x) + var);
				Obj_SetSpeed(obj, 0);
			}
			yield;
		}
		while(Obj_GetSpeed(obj) < v) {
			Obj_SetSpeed(obj,  Obj_GetSpeed(obj) + 0.05);
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			if(Obj_IsOffScreen(obj)) {
				Obj_SetAutoDelete(obj, true);
			}
			yield;
		}
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		SetAlpha(182);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteSE(SeShot);
		DeleteGraphic(ImgBoss);
		DeleteGraphic(ImgFamiliar);
	}
}