#TouhouDanmakufu
#Title[Desdemona Valtiel Attack 2]
#Text[Desdemonae arcanum.]
#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 turndir = 1;
	@Initialize {
		SetLife(4000);
		SetInvincibility(120);
		SetDamageRate(100, 100);
		SetTimer(46);
//		SetScore(333333);
//		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 + 180, 10, 4);
		MainTask;
	}
	task MainTask {
		loop(121) {
			yield;
		}
		loop {
			BomberCircle(GetX, GetY, 4, 15, turndir, 5, 30, 22, PURPLE03, 0);
			turndir = turndir * -1;
			loop(180) {
				yield;
			}
			if(movecounter%4 == 0) {
				SetMovePosition03(GetCenterX - 50, GetClipMinY + 100, 9, 3);
			}
			else if(movecounter%4 == 2) {
				SetMovePosition03(GetCenterX + 50, GetClipMinY + 100, 9, 3);
			}
			else {
				SetMovePosition03(GetCenterX, GetClipMinY + 180, 9, 3);
			}
			movecounter++;
			Concentration01(60);
			loop(60) {
				yield;
			}
			UnBombCircle(GetX, GetY, 4, 15, turndir, 5, PURPLE03, PURPLE02, PURPLE01, 0);
			turndir = turndir * -1;
			loop(180) {
				yield;
			}
			if(movecounter%4 == 0) {
				SetMovePosition03(GetCenterX - 50, GetClipMinY + 100, 9, 3);
			}
			else if(movecounter%4 == 2) {
				SetMovePosition03(GetCenterX + 50, GetClipMinY + 100, 9, 3);
			}
			else {
				SetMovePosition03(GetCenterX, GetClipMinY + 180, 9, 3);
			}
			movecounter++;
			Concentration01(60);
			loop(60) {
				yield;
			}
		}
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
	task BomberCircle(x, y, v, num, dir, int, life, snum, graphic, delay) {
		ascent(a in 0..num) {
			PropogandaBomber(x, y, v, a * (360 / num) * dir, life, snum, graphic, delay);
			loop(int) {
				yield;
			}
		}
	}
	task UnBombCircle(x, y, v, num, dir, int, graphic1, graphic2, graphic3, delay) {
		ascent(a in 0..num) {
			TrailShot(x, y, v - 3, v, a * 360 / num, graphic1, graphic2, graphic3, delay);
			loop(int) {
				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);
		}
	}
	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(8) {
			CreateShotA(0, x, y, delay);
			SetShotDataA(0, 0, rand(v - 3, v), angle + rand(-2.5, 2.5), 0, 0, v - 0.25, PURPLE02);
			SetShotKillTime(0, life);
			FireShot(0);
			CreateShotA(0, x, y, delay);
			SetShotDataA(0, 0, rand(v - 3.5, v - 0.5), angle + rand(-2.5, 2.5), 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 / -100, xs, ys * -1, graphic - 164);
			FireShot(0);
		}
		Obj_Delete(obj);
		yield;
	}
}