#TouhouDanmakufu
#Title["Royal Flush" -Lunatic-]
#Text[Casu arcanum.]
#Image[snapshot\royalflushlunatic.bmp]
#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(800);
		SetDamageRate(20, 5);
		SetInvincibility(481);
		SetTimer(61);
		SetScore(444444);
		CutIn(KOUMA, ""\""Royal Flush"\", "script\img\feliciaandcasus.png", 0, 0, 0, 0);

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

		SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame%360 == 1) {
			ascent(a in 0..72) {
				if(a%2 == 0) {
					AimAmulet(GetX, GetY, 3 + a%8 / 2, 1, a * 5, rand(-3, 3), RED02, RED56, 240);
				}
				else {
					AimAmulet(GetX, GetY, 3 + a%8 / 2, 1, a * 5, rand(-3, 3), BLUE02, BLUE56, 240);
				}
			}
		}
		if(frame%360 == 121) {
			ascent(a in 0..72) {
				if(a%2 == 0) {
					AimAmulet(GetX, GetY, 6 - a%8 / 2, 1, a * 5, rand(-3, 3), RED02, RED56, 240);
				}
				else {
					AimAmulet(GetX, GetY, 6 - a%8 / 2, 1, a * 5, rand(-3, 3), BLUE02, BLUE56, 240);
				}
			}
			SetMovePosition03(prand_int(GetClipMinX + 80, GetClipMaxX - 80), prand_int(GetClipMinY + 80, GetClipMinY + 180), 10, 4);
		}
		if(frame%360 >= 241 && frame%360 <= 296) {
			AimAmulet(GetX, GetY, rand(1, 6), 1, GetAngleToPlayer + rand(-15, 15), rand(-3, 3), WHITE02, WHITE56, 240);
		}
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		loop(12) {
			CreateItem(ITEM_SCORE, GetX + rand(-50, 50), GetY - rand(20, 100));
		}
		DeleteGraphic(ImgBoss);
	}
    task AimAmulet(x, y, sv, ev, angle, var, graphic1, graphic2, delay) {
	let obj = Obj_Create(OBJ_SHOT);
	let aim = false;
	Obj_SetPosition(obj, x, y);
	Obj_SetAngle(obj, angle);
	Obj_SetSpeed(obj, sv);
	ObjShot_SetGraphic(obj, graphic1);
	while(Obj_GetSpeed(obj) > 0) {
		Obj_SetSpeed(obj, Obj_GetSpeed(obj) - 0.05);
		yield;
	}
	while(Obj_BeDeleted(obj) == false) {
		if(delay > 0) {
			Obj_SetSpeed(obj, 0);
			delay--;
		}
		else if(aim == false) {
			Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)) + var);
			ObjShot_SetGraphic(obj, graphic2);
			aim = true;
		}
		else if(Obj_GetSpeed(obj) < ev) {
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) + 0.025);
		}
		if(GetLife <= 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;
	}
}