#Title[四十九日の瞑想]
#Text[ゴウタマ･シッダルタ]
#PlayLevel[EXTRA]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
	#include_function ".\lib\yuyuko.dnh"

	let name = "釈迦「大きな菩提樹の木の下で」";

	xIni = cx;
	yIni = cy - 16;

	let x = (GetClipMaxX - GetClipMinX) / 7;
	let y = (cy - GetClipMinY) / 4;
	let i = 1;
	
	let alpha = 255;

	sub onInitialize {
		CutIn(YOUMU, name, bossCut, 1, 1, 463, 655);
		SetScore(10000000);
		SetLife(10000);
		SetTimer(900);
		SetDurableSpellCard;
		SetEffectForZeroLife(60, 0, 0);
		back = CSD ~ ".\img\back6.png";

//		SetColor(0, 0, 0);
		TMain;
		TEnd;
	}

	sub onMainLoop {
		if(i < 49) {
			SetTimer(50 - i);
		}
		yield;
	}

	sub onDrawLoop {
//		SetAction(ACT_SHOT_A, 1);
	}

	sub onFinalize {
		PlaySE(seFin);
	}

/////////////////////////////////////////////////////////////////////

	task TMain {
		getReady_;

		let count = 1;

		Concentration01(360);
		wait(390);

		loop {
			TShot(i, count);
			if(i == 7) {
				TShotA;
			}
			if(i == 14) {
				TShotB;
				count = 2;
			}
			if(i == 21) {
				TShotC;
			}
			if(i == 28) {
				TShotD;
			}
			if(i == 35) {
				TShotE;
				count = 3;
			}
			if(i == 42) {
				TShotF;
			}

			wait(195 * count - 125);
			Concentration01(120);
			wait(120);
			i++;
		}
	}

	sub getReady_ {
		SetX(xIni);
		SetY(yIni);
		SetTimer(50);
	}

	task TEnd {
		while(GetTimer >= 1) {
			yield;
		}

		Slow(1);
		MotionBlur(ALL, 180, 128);
		wait(60);
		Slow(2);
	}

	task TShot(i, count) {
		let baseAngle = 90;
		let way = i;
		let span = 360 / way;
		let angle = baseAngle;

		loop(count) {
			loop(way) {
				createNmlShot01_(GetX, GetY, 1, angle, RICE, WHITE, 0);
				angle += span;
			}
			wait(195);
		}
	}

	task TShotA {
		let earth = ORANGE;
		let water = BLUE;
		let fire  = RED;
		let wind  = GREEN;

		let alpha = 200;

		let object = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(object, x * 3, y);
		Obj_SetSpeed(object, 0);
		Obj_SetAngle(object, 0);
		ObjShot_SetGraphic(object, MIDDLE + RED);
		Obj_SetAlpha(object, alpha);
		ObjShot_SetBombResist(object, true);

		loop{
			shot;
			wait(380);
			Explosion01(x * 3, y, 3, 0.5, 180);
			wait(110);
		}

		sub shot {
			let baseAngle = atan2(GetPlayerY - y, GetPlayerX - x * 3);
			let span = 30;
			let angle = baseAngle - (span * (3 / 2));

			TCreateShot(angle, earth);
			angle += span;
			TCreateShot(angle, water);
			angle += span;
			TCreateShot(angle, fire);
			angle += span;
			TCreateShot(angle, wind);

			task TCreateShot(_angle, type) {
				let _x = x * 3 + cos(_angle) * 8;
				let _y = y + sin(_angle) * 8;
				let speed = 12;
				let delay = 0;
				let obj = createObjShot_(_x + cos(_angle) * 10, _y + sin(_angle) * 10, speed / 5, _angle, SMALL, type, -1);
				Obj_SetAlpha(obj, 160);

				while(isInScreen(_x, _y)) {
					if(delay % 3 == 0) {
						TShot(_x, _y, delay, type, _angle);
					}
					_x += cos(_angle) * speed;
					_y += sin(_angle) * speed;
					delay += 5;
					wait(5);
				}

				task TShot(__x, __y, _delay, _type, __angle) {
					let _obj = createObjShot_(__x, __y, 0, 0, SMALL, _type, 0);
					Obj_SetAlpha(_obj, 160);

					wait(400 - delay);

					Obj_SetSpeed(_obj, rand(0.5, 0.8));
					Obj_SetAngle(_obj, getRandAngle(__angle));
					Obj_SetCollisionToPlayer(_obj, false);

					wait(60);

					Obj_SetCollisionToPlayer(_obj, true);

//					wait(630);
//					ObjShot_FadeDelete(_obj);

					function getRandAngle(___angle) {
						return  __angle + rand(30, 150) * [1, -1][rand_int(0, 1) == 0];
					}
				}
			}
		}
	}

	task TShotB {
		let bullet = [ORANGE, BLUE, RED, GREEN, PURPLE, YELLOW, WHITE];

		let object = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(object, x * 5, y);
		Obj_SetSpeed(object, 0);
		Obj_SetAlpha(object, 200);
		ObjShot_SetGraphic(object, MIDDLE + BLUE);
		ObjShot_SetBombResist(object, true);

		loop {
			let random = rand_int(0, 6);
			let baseAngle = atan2(GetPlayerY - y, GetPlayerX - x * 5);
			let way = 7;
			let span = 1;
			let angle = baseAngle - span * (way - 1) / 2;
			loop(way) {
				let _obj = createObjShot_(x * 5, y, 0.6, angle, KNIFE, bullet[random % 7], -1);
				Obj_SetAlpha(_obj, 160);
				angle += span;
				random++;
			}
			wait(180);
		}
	}

	task TShotC {
		let object = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(object, x * 2, y * 2);
		Obj_SetSpeed(object, 0);
		Obj_SetAlpha(object, 200);
		ObjShot_SetGraphic(object, MIDDLE + GREEN);
		ObjShot_SetBombResist(object, true);

		loop {
			let baseAngle = atan2(GetPlayerY - y * 2, GetPlayerX - x * 2);
			let way = 3;
			let span = 360 / way;
			let angle = baseAngle;

			loop(way) {
				shot(angle);
				angle += span;
			}
			wait(900);
		}

		task shot(_angle) {
			let obj = createObjShot_(x * 2, y * 2, 0.4, _angle, RING, rand_int(0, 7), -1);
			Obj_SetAlpha(obj, 160);

			loop(3600) {
				if(!isInScreen(Obj_GetX(obj), Obj_GetY(obj))) {
					alternative(isInScreen2(Obj_GetX(obj), Obj_GetY(obj)))
					case(1, 2) {
						Obj_SetAngle(obj, 0 - (Obj_GetAngle(obj) + 90) - 90);
					}
					case(3, 4) {
						Obj_SetAngle(obj, 0 - Obj_GetAngle(obj));
					}
					others {}
				}
				yield;
			}
			ObjShot_FadeDelete(obj);
		}
	}

	task TShotD {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x * 6, y * 2);
		Obj_SetSpeed(obj, 0);
		Obj_SetAlpha(obj, 200);
		ObjShot_SetGraphic(obj, MIDDLE + YELLOW);
		ObjShot_SetBombResist(obj, true);

		createBall;
		wait(1480);
		createBall;
		wait(740);
		createBall;

		loop {
			wait(380);
			createBall;
		}

		sub createBall {
			ascent(let i in 0..2) {
				CreateEnemyFromScript("woolBall", x * 6, y * 2, 0, i * 180, 0);
			}
		}

	}

	task TShotE {
		let object = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(object, x, y * 3);
		Obj_SetSpeed(object, 0);
		Obj_SetAlpha(object, 200);
		ObjShot_SetGraphic(object, MIDDLE + BLUE);
		ObjShot_SetBombResist(object, true);

		let count = 0;

		loop {
			createShot(cy, count);
			wait(210 - count);
			count++;
		}

		function createShot(maxY, count) {
			let way = 6 + count;

			loop(way) {
				shot;
			}

			task shot {
				let obj = createObjShot_(rand(GetClipMinX, GetClipMaxX), rand(GetClipMinY, maxY), 0.3, rand(-100, -80),
										BUTTER, [BLUE, rand_int(0, 7)][rand_int(0, 20) == 0], -1);
				Obj_SetAlpha(obj, rand(96, 194));

				let G = [rand(-0.1, 0), rand(0.3, 0.5)][rand_int(0, 3) == 0];
				let dir = [1, -1][rand_int(0, 1) == 0];
				let dAng = rand(0.1, 0.3) * dir;

				while(isInScreen(Obj_GetX(obj), Obj_GetY(obj))) {
					let x = Obj_GetX(obj) + cos(Obj_GetAngle(obj)) * Obj_GetSpeed(obj);
					let y = Obj_GetY(obj) + sin(Obj_GetAngle(obj)) * Obj_GetSpeed(obj) - G;

					let angle = atan2(y - Obj_GetY(obj), x - Obj_GetX(obj)) + dAng;
					let speed = [((x - Obj_GetX(obj))^2 + (y - Obj_GetY(obj))^2)^(1/2), 1][((x - Obj_GetX(obj))^2 + (y - Obj_GetY(obj))^2)^(1/2) > 1];

					Obj_SetAngle(obj, angle);
					Obj_SetSpeed(obj, speed);
					yield;
				}
			}
		}
	}

	task TShotF {
		let count = 0;

		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x * 7, y * 3);
		Obj_SetSpeed(obj, 0);
		Obj_SetAlpha(obj, 194);
		ObjShot_SetGraphic(obj, MIDDLE + WHITE);
		ObjShot_SetBombResist(obj, true);

		loop {
			loop(count * 2 + 6) {
				CreateEnemyFromScript("creature", x * 7, y * 3, rand(1, 2), rand(0, 360), 0);
			}
			wait(540 - count * 15);
			count++;
		}
	}
	#include_function ".\lib\六師外道body.dnh"
}

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

script_enemy woolBall {
	#include_function ".\lib\六師外道Common_.dnh"

	let obj = createObjShot_(GetX, GetY, 0, GetAngle, RING, GREEN, -1);

	@Initialize {
		SetLife(500);
		SetDamageRateEx(30, 30, 0, 0);
		SetScore(30000);

		TMain;
	}

	@MainLoop {
		if(Obj_BeDeleted(obj)) {
			SetLife(0);
		}
		yield;
	}

	@DrawLoop {
	}

	@Finalize {
		Obj_Delete(obj);
	}

/////////////////////////////////////////////////////////////////////

	task TMain {
		ObjShot_SetBombResist(obj, true);

		TBall;
		TShot;

		loop {
			SetX(Obj_GetX(obj));
			SetY(Obj_GetY(obj));
			yield;
		}

		task TBall {
			let angle = Obj_GetAngle(obj);
			let speed = 2.5;
			let dAngle = 360 / 120;
			let dSpeed = 0;

			loop(120) {
				angle += dAngle;
				setObjData(angle, speed);
				yield;
			}

			angle += 45;
			dSpeed = -0.05;

			loop(60) {
				speed = max(speed + dSpeed, 1.5);
				setObjData(angle, speed);
				yield;
			}

			angle += 90;
			dAngle = 1.8;
			speed = 2;

			loop(200) {
				angle += dAngle;
				setObjData(angle, speed);
				yield;
			}

			angle += 180;
			dAngle = -1;
			dSpeed = 0.02;

			loop(360) {
				angle += dAngle;
				speed = max(speed + dSpeed, 2.5);
				setObjData(angle, speed);
				yield;
			}

			Obj_Delete(obj);

			function setObjData(_angle, _speed) {
				Obj_SetAngle(obj, _angle);
				Obj_SetSpeed(obj, _speed);
			}
		}

		task TShot {
			loop {
				createNmlShot01_(GetX, GetY, 0.4, Obj_GetAngle(obj) + 180, RICE, GREEN, -1);
				wait(8);
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

script_enemy creature {
	#include_function ".\lib\六師外道Common_.dnh"

	let angle = GetAngle;
	let speed = GetSpeed;

	let x = GetX;
	let y = GetY;

	let obj = createObjShot_(x, y, speed, angle, BUTTER, rand_int(0, 7), 1);

	@Initialize {
		SetScore(50000);
		SetLife(1);
		SetDamageRate(0, 0);

		TMain;
	}

	@MainLoop {
		x += cos(angle) * speed;
		y += sin(angle) * speed;
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		SetX(x);
		SetY(y);
		SetCollisionA(x, y, 8);
		yield;
	}

	@DrawLoop {}

	@Finalize {
		if(GetEnemyLife > 1) {
			loop(180) {
				createNmlShot01_(x, y, rand(0.5, 3), rand(0, 360), GRAIN, WHITE, -1);
			}
		}
		Obj_Delete(obj);
	}

/////////////////////////////////////////////////////////////////////

	task TMain {
		wait(90);
		SetDamageRate(100, 100);

		angle = atan2(GetPlayerY - y, GetPlayerX - x);
		speed = 1;
	}
}