#TouhouDanmakufu
#Title["Will to Fight"]
#Text[Invitae arcanum.]
#Image[snapshot\willtofight.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.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 ImgFamiliar = "script\img\circle6_s.png";
	let frame = -120;
	let trig = 0;
	let r = 0;
	let outAngle = 0;
	@Initialize {
		SetLife(1000);
		SetInvincibility(210);
		SetDamageRate(20, 5);
		SetTimer(61);
		SetScore(555555);
		CutIn(KOUMA, ""\""Will to Fight"\", "script\img\desdemonaandinvita.png", 0, 0, 0, 0);

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

		SetMovePosition03(GetCenterX, GetCenterY, 9, 5);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	function TurningFlower(x, y, v1, accel, v2, num, sa, turndir, graphic) {
		loop(num) {
			CreateShotA(0, x, y, 0);
			SetShotDataA(0, 0, v1, sa, 0, accel * -1, 0, graphic);
			SetShotDataA(0, 120, NULL, sa + turndir, 0, accel, v2, graphic);
			FireShot(0);
			CreateShotA(0, x, y, 0);
			SetShotDataA(0, 0, v1, 180 - sa, 0, accel * -1, 0, graphic);
			SetShotDataA(0, 120, NULL, 180 - sa + turndir, 0, accel, v2, graphic);
			FireShot(0);
			sa += (360 / num);
		}
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		outAngle+=0.5;
		if(frame == 0) {
			ascent(a in 0..4) {
				Familiar(a * 90, 250);
			}
		}
		if(frame%20 == 1) {
			TurningFlower(GetX, GetY, 3, 0.0175, 1, 6, outAngle, 150, PURPLE21);
			TurningFlower(GetX, GetY, 2, 0.02, 1, 6, outAngle + 5, -90, RED21);
		}
		trig += 0.5;
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
		DeleteGraphic(ImgFamiliar);
	}
	task Familiar(mtrig, radius) {
		let obj = Obj_Create(OBJ_EFFECT);
		let core = Obj_Create(OBJ_SHOT);
		let assignVertex = 0;
		let fx = 0;
		let fy = 0;
		ObjShot_SetBombResist(core, true);
		ObjShot_SetGraphic(core, PURPLE03);
		Obj_SetAutoDelete(core, false);
		ObjEffect_SetTexture(obj, ImgFamiliar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_CreateVertex(obj, 4);
		ascent(y in 0..2) {
			ascent(x in 0..2) {
				ObjEffect_SetVertexUV(obj, assignVertex, (x + 1) * 64, (y + 1) * 64);
				ObjEffect_SetVertexXY(obj, assignVertex, x * 64 - 32, y * 64 - 32);
				assignVertex++;
			}
		}
		while(Obj_BeDeleted(obj) == false) {
			Obj_SetPosition(obj, GetX + cos(mtrig) * radius, GetY + sin(mtrig) * radius);
			Obj_SetAngle(obj, atan2(GetY - Obj_GetY(obj), GetX - Obj_GetX(obj)) - 90);
			Obj_SetPosition(core, GetX + cos(mtrig) * radius, GetY + sin(mtrig) * radius);
			if(frame%3 == 1) {
				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 1, Obj_GetAngle(obj) + 180, WHITE31, 0);
			}
			if(frame%300 == 299) {
//				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 2, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)), 254, 0);
				Slow(2);
				MotionBlur(ALL, 120, 128);
			}
			else if(frame%300 == 119) {
				Slow(0);
			}
			mtrig += 2;
			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;
		}
	}
}