#TouhouDanmakufu
#Title["Turnback Time"]
#Text[Nihili arcanum.]
#Image[snapshot\turnbacktime.bmp]
#Player[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 frame = -120;
	let trig = 0;
	let turndir = 1;
	let movecounter = 0;
	let oa = 0;
	let r = 0;
	let sx = GetCenterX - 80;
	let sy = GetCenterY - 80;
	let yx = GetCenterX + 80;
	let yy = GetCenterY + 80;
	@Initialize {
		SetLife(700);
		SetInvincibility(361);
		SetDamageRate(20, 5);
		SetTimer(61);
		SetScore(444444);
		CutIn(KOUMA, ""\""Turnback Time"\", "script\img\emilycutin.png", 0, 0, 0, 0);

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

		SetMovePosition02(GetCenterX, GetCenterY, 120);
		TurnbackField;
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame%300 == 180 && r >= 80) {
			TimeStop(60, 1, 1, 1);
		}
		yield;
		frame++;
	}
	function LemmingDelay(x, y) {
		CreateShotA(0, x, y, 30);
		SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE57);
		SetShotKillTime(0, 0);	
		FireShot(0);
	}
	task TurnbackField {
		yield;
		let d = 300;
		loop(120) {
			d--;
			yield;
		}
		let c = 0;
		loop {
			ascent(a in 0..8) {
				let bulletx = sx + cos(trig + a * 45) * r;
				let bullety = sy + sin(trig + a * 45) * r;
				if(frame%60 == 1 && r >= 80) {
					AimAmulet(bulletx, bullety, 0.5, 2, atan2(sy - bullety, sx - bulletx), rand(-3, 3), WHITE05, BLUE12, d);
					CreateShot01(bulletx, bullety, 0.5, atan2(sy - bullety, sx - bulletx), WHITE05, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE03);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			ascent(a in 0..8) {
				let bulletx = yx + cos(trig * -1 + a * 45) * r;
				let bullety = sy + sin(trig * -1 + a * 45) * r;
				if(frame%60 == 1 && r >= 80) {
					AimAmulet(bulletx, bullety, 0.5, 2, atan2(sy - bullety, yx - bulletx), rand(-3, 3), WHITE05, BLUE12, d);
					CreateShot01(bulletx, bullety, 0.5, atan2(sy - bullety, yx - bulletx), WHITE05, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE03);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			ascent(a in 0..8) {
				let bulletx = sx + cos(trig + a * 45) * r;
				let bullety = yy + sin(trig + a * 45) * r;
				if(frame%60 == 1 && r >= 80) {
					AimAmulet(bulletx, bullety, 0.5, 2, atan2(yy - bullety, sx - bulletx), rand(-3, 3), WHITE05, BLUE12, d);
					CreateShot01(bulletx, bullety, 0.5, atan2(yy - bullety, sx - bulletx), WHITE05, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE03);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			ascent(a in 0..8) {
				let bulletx = yx + cos(trig * -1 + a * 45) * r;
				let bullety = yy + sin(trig * -1 + a * 45) * r;
				if(frame%60 == 1 && r >= 80) {
					AimAmulet(bulletx, bullety, 0.5, 2, atan2(yy - bullety, yx - bulletx), rand(-3, 3), WHITE05, BLUE12, d);
					CreateShot01(bulletx, bullety, 0.5, atan2(yy - bullety, yx - bulletx), WHITE05, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE03);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			if(r < 80) {
				r++;
			}
			if(frame%60 == 1) {
				c++;
			}
			d -= 0.9;
			if(frame%300 == 181) {
				d = 299;
				c = 0;
			}
			trig ++;
			yield;
		}
	}
    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_BeDeleted(obj) == false) {
		if(delay > 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;
	}
  }
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
}