#TouhouDanmakufu
#Title["Self Destruction"]
#Text[Invitae arcanum.]
#Image[snapshot\selfdestruction.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 SeShot = "script\se\tan00.wav";
	let SeConcentrate = "script\se\power0.wav";
	let frame = -120;
	let trig = 0;
	let r = 0;
	let outAngle = 90;
	let sprayAngle = 0;
	let movecounter = 0;
	@Initialize {
		SetLife(800);
		SetInvincibility(300);
		SetDamageRate(20, 0);
		SetTimer(51);
		SetScore(555555);
		CutIn(KOUMA, ""\""Self Destruction"\", "script\img\desdemonaandinvita.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		LoadSE(SeShot);
		LoadSE(SeConcentrate);
		psrand(747030);
		shotinit;

		SetMovePosition03(GetCenterX, GetClipMinY + 150, 9, 5);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	function Flower(x, y, v1, accel, v2, num, sa, graphic) {
		loop(num) {
			CreateShot02(x, y, v1, sa, accel, v2, graphic, 0);
			if(num%2 == 0) {
				CreateShot02(x, y, v1, 180 - sa, accel, v2, graphic, 0);
			}
			else {
				CreateShot02(x, y, v1, 0 - sa, accel, v2, graphic, 0);
			}
			sa += (360 / num);
		}
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(GetTimeOfPlayerInvincibility > 0) {
			Slow(0);
			frame = -45;
		}
		else if(frame%360 == 1) {
			movecounter++;
			loop(222) {
				let vm = rand(0.1, 3);
				let vm2 = vm;
				if(vm < 1) {
					vm2++;
				}
				let rd = rand_int(0, 1);	
				if(rd == 0) {
					CreateShot02(GetX, GetY, vm + 3, rand(0, 360), -0.1, vm, PURPLE01, 60);
				}
				else {
					AimAmulet(GetX, GetY, vm + 3, vm, vm2, rand(0, 360), 0, WHITE54, RED54, 120);
				}
			}
			loop(222) {
				let vm = rand(0.1, 2);
				CreateShot02(GetX, GetY, vm + 3, rand(0, 360), -0.1, vm, PURPLE05, 60);
			}
		}
		else if(frame%360 == 61) {
			PlaySE(SeShot);
		}
		else if(frame%360 == 121) {
			PlaySE(SeConcentrate);
			Concentration01(60);
			Slow(2);
			MotionBlur(ALL, 60, 60);
		}
		else if(frame%360 == 181) {
			Slow(0);
		}
		yield;
		frame++;
	}
    task AimAmulet(x, y, sv, mv, ev, angle, var, graphic1, graphic2, delay) {
	let obj = Obj_Create(OBJ_SHOT);
	let aim = false;
	let objd = 60;
	Obj_SetPosition(obj, x, y);
	Obj_SetAngle(obj, angle);
	Obj_SetSpeed(obj, sv);
	ObjShot_SetGraphic(obj, graphic1);
	ObjShot_SetDelay(obj, 60);
	while(objd > 0) {
		objd--;
		yield;
	}
	while(Obj_BeDeleted(obj) == false) {
		if(delay > 0) {
			if(Obj_GetSpeed(obj) > mv) {
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) - 0.1);
			}
			else if(Obj_GetSpeed(obj) < 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);
			Obj_SetSpeed(obj, ev);
			aim = true;
		}
		else if(Obj_GetSpeed(obj) < ev) {
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) + 0.025);
		}
		if(GetLife <= 0) {
			Obj_Delete(obj);
		}
		yield;
	}
  }
	function TrailShot(x, y, v1, v2, angle, headshot, bodyshot, tailshot, delay) {
		CreateShot01(GetX, GetY, v2, angle, headshot, delay);
		loop(8) {
			CreateShot01(GetX, GetY, rand(v1 + 0.5, v2), angle + rand(-2.5, 2.5), bodyshot, delay);
			CreateShot01(GetX, GetY, rand(v1, v2 - 0.5), angle + rand(-2.5, 2.5), tailshot, delay);
		}
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
		DeleteSE(SeShot);
		DeleteSE(SeConcentrate);
	}
	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;
		}
	}
}