#TouhouDanmakufu
#Title["Gravitic Decree" -Lunatic-]
#Text[Serii arcanum.]
#Image[snapshot\graviticdecreelunatic.bmp]
#Player[player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.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 ImgFamiliar = "script\img\circle6_s.png";
	let frame = -120;
	let trig = 0;
	let turndir = 1;
	let movecounter = 0;
	let px = GetCenterX;
	let py = GetCenterY;
	let majoraxis = 0;
	let r = 0;
	@Initialize {
		SetLife(600);
		SetInvincibility(120);
		SetDamageRate(20, 10);
		SetTimer(51);
		SetScore(333333);
		CutIn(KOUMA, ""\""Gravitic Decree"\", "script\img\velonandserius.png", 0, 0, 0, 0);

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

		SetMovePosition03(GetCenterX, GetClipMinY + 150, 9, 4);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		SetShotAutoDeleteClip(100, 100, 100, 100);
		if(frame == -60) {
			Concentration01(60);
		}
		if(frame == 0) {
			SprayingFamiliar(0, 2);
			SprayingFamiliar(180, 2);
		}
		if(frame > 0) {
			SetPlayerY(GetPlayerY - 1);
			if(r < 180) {
				r++;
			}
		}
		if(frame%4 == 1) {
			CreateShot01(rand(GetClipMinX, GetClipMaxX), GetClipMinY, rand(0.5, 2.5), 90, RED52, 10);
		}
		yield;
		frame++;
	}
	task WhiteSpray(x, y, lv, hv, angle, var, num, int, graphic, delay) {
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 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);
		ObjEffect_SetVertexXY(obj, 0, -32, -32);
		ObjEffect_SetVertexXY(obj, 1, 32, -32);
		ObjEffect_SetVertexXY(obj, 2, 32, 32);
		ObjEffect_SetVertexXY(obj, 3, -32, 32);
		Obj_SetPosition(obj, x, y);
		loop(num) {
			CreateShot01(x, y, rand(lv, hv), angle + rand(var * -1, var), graphic, delay);
			loop(int) {
				ObjEffect_SetAngle(obj, 0, 0, ga);
				ga += 2;
				yield;
			}
		}
		Obj_Delete(obj);
	}
	task SprayingFamiliar(sa, v) {
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 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);
		ObjEffect_SetVertexXY(obj, 0, -32, -32);
		ObjEffect_SetVertexXY(obj, 1, 32, -32);
		ObjEffect_SetVertexXY(obj, 2, 32, 32);
		ObjEffect_SetVertexXY(obj, 3, -32, 32);
		while(Obj_BeDeleted(obj) == false) {
			Obj_SetPosition(obj, GetX + cos(sa) * r, GetY + sin(sa) * r / 3);
			ObjEffect_SetAngle(obj, 0, 0, ga);
			sa += v;
			ga += 2;
			if(frame%150 == 149) {
				ascent(a in 0..30) {
					CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 3, a * 12, -0.1, 1, PURPLE04, 0);
				}
				WhiteSpray(Obj_GetX(obj), Obj_GetY(obj), 1, 3, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)), 5, 20, 5, WHITE52, 10);
			}
			yield;
		}
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		loop(9) {
			CreateItem(ITEM_SCORE, GetX + rand(-50, 50), GetY - rand(20, 100));
		}
		CreateItem(ITEM_1UP, GetX + rand(-50, 50), GetY - rand(20, 100));
		DeleteGraphic(ImgBoss);
		DeleteGraphic(ImgFamiliar);
	}
}