#TouhouDanmakufu
#Title["Propoganda Bomber"]
#Text[Invita arcanum.]
#Image[snapshot\propogandabomber.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 frame = -120;
	let trig = 0;
	let turndir = 1;
	let movecounter = 0;
	let counter = 0;
	let outAngle = 0;
	let hasmoved = false;
	@Initialize {
		SetLife(800);
		SetInvincibility(120);
		SetDamageRate(20, 5);
		SetTimer(61);
		SetScore(555555);
		CutIn(KOUMA, ""\""Propoganda Bomber"\", "script\img\desdemonaandinvita.png", 0, 0, 0, 0);

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

		SetMovePosition03(GetCenterX, GetClipMinY + 150, 10, 3);
	}

	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		SetShotAutoDeleteClip(32, 32, 32, 32);
		trig += 2.4;
		if(frame%360 == 1) {
			BomberWings(GetX, GetY, 5, 90, 30, PURPLE03);
		}
		if(frame%360 == 121) {
			SetMovePosition03(GetCenterX + cos(trig) * 60, GetClipMinY + 120, 10, 3);
		}
		if(frame%360 == 181) {
			ReflectionBomber(GetX, GetY, 2.5, 5, PURPLE03, PURPLE02, PURPLE01);
		}
		if(frame%360 == 241) {
			Slow(1.5);
			MotionBlur(ALL, 120, 75);
		}
		else if(frame%360 == 1) {
			Slow(0);
		}
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
	task BomberWings(x, y, v, sa, life, graphic) {
		ascent(a in 1..18) {
			PropogandaBomber(x, y, v, sa + a * 30, life, 13, graphic, 0);
			PropogandaBomber(x, y, v, sa - a * 30, life, 13, graphic, 0);
			loop(5) { yield; }
		}
	}
	task ReflectionBomber(x, y, minv, maxv, headgraphic, bodygraphic, tailgraphic) {
		ascent(a in 1..18) {
			ReflectionBomb(x, y, minv, maxv, GetAngleToPlayer + a * 30, headgraphic, bodygraphic, tailgraphic, 0);
			ReflectionBomb(x, y, minv, maxv, GetAngleToPlayer - a * 30, headgraphic, bodygraphic, tailgraphic, 0);
			loop(5) { yield; }
		}
	}
	task ReflectionBomb(x, y, minv, maxv, angle, headgraphic, bodygraphic, tailgraphic, delay) {
		Ricochet(x, y, maxv, angle, 0, headgraphic, delay);
		loop(5) {
			Ricochet(x, y, rand(minv + maxv / 10, maxv), angle + rand(-3, 3), 0, bodygraphic, delay);
			Ricochet(x, y, rand(minv, maxv - maxv / 10), angle + rand(-3, 3), 0, tailgraphic, delay);
		}
	}
	task Ricochet(x, y, v, angle, count, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let lifeCounter = 0;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj, true);
		while(Obj_BeDeleted(obj) == false) {
			yield;
			if(Obj_GetX(obj) <= GetClipMinX || Obj_GetX(obj) >= GetClipMaxX) {
				if(count > 0) {
					count--;
					if(Obj_GetAngle(obj) == 0 || Obj_GetAngle(obj) == 180) {
						Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)), count, graphic, 0);
					}
					else {
						Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), count, graphic, 0);
					}
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), graphic, 0);
					Obj_Delete(obj);
				}
				yield;
			}
			else if(Obj_GetY(obj) <= GetClipMinY) {
				if(count > 0) {
					count--;
					Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, 0 - Obj_GetAngle(obj), count, graphic, 0);
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 0 - Obj_GetAngle(obj), graphic, 0);
					Obj_Delete(obj);
				}
				yield;
			}
		}
	}
	task PropogandaBomber(x, y, v, angle, life, snum, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		Obj_SetSpeed(obj, v);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		loop(5) {
			CreateShotA(0, x, y, delay);
			SetShotDataA(0, 0, rand(v - 2, v), angle + rand(-3, 3), 0, 0, v - 0.25, PURPLE02);
			SetShotKillTime(0, life);
			FireShot(0);
			CreateShotA(0, x, y, delay);
			SetShotDataA(0, 0, rand(v - 2, v), angle + rand(-3, 3), 0, 0, v - 0.25, PURPLE01);
			SetShotKillTime(0, life);
			FireShot(0);
		}
		while(life > 0) {
			life--;
			yield;
		}
/*		loop(snum) {
			let xs = rand(-1, 1);
			let ys = rand(-2, -0.5);
			CreateShotA(0, Obj_GetX(obj), Obj_GetY(obj), 60);
			SetShotDataA_XY(0, 0, xs, ys, 0, ys / -50, xs, ys * -1, graphic - 164);
			FireShot(0);
		} */
		ascent(a in 0..snum) {
			CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 1, a * (360 / snum), PURPLE05, 60);
		}
		Obj_Delete(obj);
		yield;
	}
}