#TouhouDanmakufu
#Title[Felicia Opsors Attack 3 -Lunatic-]
#Text[Feliciae arcanum.]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh]
#PlayLevel[Lunatic]
#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;
	@Initialize {
		SetLife(3500);
		SetInvincibility(181);
		SetTimer(44);
//		SetScore(444444);
//		CutIn(KOUMA, ""\""Bottomless Wallet"\", "script\img\feliciaandcasus.png", 0, 0, 0, 0);

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

		SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame%360 == 1) {
			ascent(a in 0..90) {
				Ricochet(GetX, GetY, 1.5, a * 4, 0, WHITE56, 0);
				Ricochet(GetX, GetY, 1.25, a * 4 + 2, 0, WHITE56, 0);
				Ricochet(GetX, GetY, 1, a * 4, 0, WHITE56, 0);
				Ricochet(GetX, GetY, 0.75, a * 4 + 2, 0, WHITE56, 0);
			}
		}
		if(frame%360 == 121) {
			let minxb = GetX - prand(40, 100);
			let maxxb = GetX + prand(40, 100);
			if(GetClipMinX > minxb) {
				minxb = GetClipMinX;
			}
			else if(GetClipMaxX < maxxb) {
				maxxb = GetClipMaxX;
			}
			SetMovePosition03(prand(minxb, maxxb), prand(80, 180), 10, 3.5);
		}
		if(frame%360 == 201) {
			loop(77) {
				let prm = prand(1, 2.5);
				CreateShot02(GetX, GetY, prm + 2, prand(0, 360), -0.1, prm, SP08, 30);
			}
		}
		if(frame%360 == 241) {
			loop(77) {
				let prm = prand(1, 2.5);
				CreateShot02(GetX, GetY, prm + 2, prand(0, 360), -0.1, prm, SP10, 30);
			}
		}
		if(frame%360 == 271) {
			loop(77) {
				let prm = prand(1, 2.5);
				CreateShot02(GetX, GetY, prm + 2, prand(0, 360), -0.1, prm, SP09, 30);
			}
		}
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
	task Ricochet(x, y, v, angle, count, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let lifeCounter = 0;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj, true);
		while(Obj_BeDeleted(obj) == false) {
			yield;
			if(Obj_GetX(obj) <= GetClipMinX || Obj_GetX(obj) >= GetClipMaxX) {
				if(count > 0) {
					count--;
					if(Obj_GetAngle(obj) == 0 || Obj_GetAngle(obj) == 180) {
						Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)), count, graphic, 0);
					}
					else {
						Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), count, graphic, 0);
					}
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), graphic, 0);
					Obj_Delete(obj);
				}
				yield;
			}
			else if(Obj_GetY(obj) <= GetClipMinY) {
				if(count > 0) {
					count--;
					Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, 0 - Obj_GetAngle(obj), count, graphic, 0);
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 0 - Obj_GetAngle(obj), graphic, 0);
					Obj_Delete(obj);
				}
				yield;
			}
		}
	}
	task CreateShot03(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		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);
		yield;
	}
}