#TouhouDanmakufu
#Title["Bottomless Wallet" -Easy-]
#Text[Casu arcanum.]
#Image[snapshot\bottomlesswalleteasy.bmp]
#Player[FREE]
#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 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(800);
		SetInvincibility(181);
		SetDamageRate(20, 5);
		SetTimer(44);
		SetScore(444444);
		CutIn(KOUMA, ""\""Bottomless Wallet"\", "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%240 >= 1 && frame%240 <= 61 && frame%5 == 1) {
			ascent(a in -2..3) {
				CoinRicochet(GetX, GetY, 3, -0.03, -3, GetAngleToPlayer + a * 20 + rand(-10, 10), 0, SP10, 0);
			}
		}
		if(frame%240 == 90) {
			sprayx = 150 * turnmarker;
			turnmarker = turnmarker * -1;
		}
		if(frame%240 >= 91 && frame%240 <= 161 && frame%10 == 0) {
			CoinRing(GetCenterX + sprayx, GetCenterY, 2, -0.03, -2, 9, 1, SP09, 10);
			sprayx += 50 * turnmarker;
		}
		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 CoinRing(x, y, v, accel, ev, num, count, graphic, delay) {
		ascent(a in 0..num) {
			CoinRicochet(x, y, v, accel, ev, a * (360 / num), count, graphic, delay);
		}
	}
	task FiveRicochet(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--;
					FiveRicochet(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj), count, graphic, 0);
					Obj_Delete(obj);
				}
				else {
					ascent(a in -2..3) {
						CreateShot01(Obj_GetX(obj), Obj_GetY(obj), v, 180 - Obj_GetAngle(obj) + a * 5, graphic, 0);
					}
					Obj_Delete(obj);
				}
				yield;
			}
			else if(Obj_GetY(obj) <= GetClipMinY) {
				if(count > 0) {
					count--;
					FiveRicochet(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 CoinRicochet(x, y, v, accel, ev, 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(delay > 0) {
				delay--;
			}
			else if((Obj_GetSpeed(obj) > ev && accel < 0) || (Obj_GetSpeed(obj) < ev && accel > 0)) {
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) + accel);
			}
			if(Obj_GetX(obj) <= GetClipMinX || Obj_GetX(obj) >= GetClipMaxX) {
				if(count > 0) {
					count--;
					CoinRicochet(Obj_GetX(obj), Obj_GetY(obj), Obj_GetSpeed(obj), accel, ev, 180 - Obj_GetAngle(obj), count, graphic, 0);
					Obj_Delete(obj);
				}
				else {
					ev += (ev / absolute(ev)) * -1;
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), ev, 180 - Obj_GetAngle(obj), graphic, 0);
					Obj_Delete(obj);
				}
				yield;
			}
			else if(Obj_GetY(obj) <= GetClipMinY) {
				if(count > 0) {
					count--;
					CoinRicochet(Obj_GetX(obj), Obj_GetY(obj), Obj_GetSpeed(obj), accel, ev, 0 - Obj_GetAngle(obj), count, graphic, 0);
					Obj_Delete(obj);
				}
				else {
					ev += (ev / absolute(ev)) * -1;
					CreateShot01(Obj_GetX(obj), Obj_GetY(obj), ev, 0 - Obj_GetAngle(obj), graphic, 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;
	}
}