#TouhouDanmakufu
#Title["Cursed Inheritance"]
#Text[Casu arcanum.]
#Image[snapshot\cursedinheritance.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh]
#PlayLevel[Hard]
#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 ang = 0;
	let turnswitch = 1;
	let velo = 0;
	let freq = 120;
	let fire = false;
	let fire2 = false;
	let count = 0;
	let sc = 0;
	let m = -1;
	let sprayx = -150;
	let turnmarker = 1;
	let trig = 0;
	@Initialize {
		SetLife(1000);
		SetInvincibility(211);
		SetDamageRate(20, 5);
		SetTimer(44);
		SetScore(444444);
		CutIn(KOUMA, ""\""Cursed Inheritance"\", "script\img\feliciaandcasus.png", 0, 0, 0, 0);

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

		SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame == 0) {
			PennySaved(GetX, GetY, 4, GetAngleToPlayer, SP08, 3);
		}
		if(frame%240 == 90) {
			sprayx = 150 * turnmarker;
			turnmarker = turnmarker * -1;
		}
		if(frame%360 == 61) {
//			WhiteSpray(GetX, GetY, 0.5, 4, GetAngleToPlayer, 15, 60, 1, SP10, 10);
		}
		if(frame%180 == 179) {
			SetMovePosition03(GetClipMaxX - prand(60, GetClipMaxX - 60), prand(80, 180), 10, 3.5);
		}
		yield;
		frame++;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		loop(12) {
			CreateItem(ITEM_SCORE, GetX + rand(-50, 50), GetY - rand(20, 100));
		}
		DeleteGraphic(ImgBoss);
	}
	task WhiteSpray(x, y, lv, hv, angle, var, num, int, graphic, delay) {
		loop(num) {
			CreateShot01(x, y, rand(lv, hv), angle + rand(var * -1, var), graphic, delay);
			loop(int) {
				yield;
			}
		}
	}
	task PennySaved(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let g = 0;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, 0);
		ObjShot_SetBombResist(obj, true);
		while(v > 0) {
			v = v - 0.05;
			Obj_SetSpeed(obj, v);
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			Obj_SetSpeed(obj, 0);
			if(frame%delay == 1) {
				loop(2) {
					Ricochet(Obj_GetX(obj), Obj_GetY(obj), 1, rand(0, 360), 1, SP08, SP09, g%8 + 113, delay * 6);
					g++;
				}
			}
			yield;
		}
	}
	task Ricochet(x, y, v, angle, count, graphic1, graphic2, graphic3, 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, graphic1);
		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, graphic2, graphic2, graphic3, 0);
					}
					else {
						Ricochet(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), count, graphic2, graphic2, graphic3, 0);
					}
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), graphic3, 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, graphic2, graphic2, graphic3, 0);
					Obj_Delete(obj);
				}
				else {
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 0 - Obj_GetAngle(obj), graphic3, 0);
					Obj_Delete(obj);
				}
				yield;
			}
		}
	}
	task CreateShot03(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		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);
		yield;
	}
}