#Title[バラモン教]
#Text[]
#PlayLevel[EXTRA]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
	#include_function ".\lib\youmu.dnh"
	
	let name = "身分「混じってはならない超えられぬ壁」";
	
	xIni = cx;
	yIni = GetClipMinY + 64;
	
	let laser = [];
	
	sub onInitialize {
		SetLife(10000);
		
		CutIn(YOUMU, name, bossCut, 1, 1, 530, 650);
		SetScore(500000);
		SetDamageRate(10, 0);
		SetTimer(50);
		back = CSD ~ "\img\back00.png";

		let wIni = 30;
		SetMovePosition02(xIni, yIni, wIni);

		TMain;
	}

	sub onMainLoop {
		if(!OnEvent) {
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 24);
		}
		yield;
	}

	sub onDrawLoop {
	}

	sub onFinalize {
		createItems(ITEM_SCORE, 16, 42);
	}

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

	task TMain {
		setLaser;
		SetAction(ACT_CHARGE, 250);
		wait(250);
		PlaySE(seLaser);
		SetAction(ACT_SLASH_V, 50);
		wait(50);
		
		SetDamageRate(300, 450);
		SetAction(ACT_CHARGE, 120);
		wait(120);
		TShot;
	}
	
	sub setLaser {
		let width = (cx - (GetClipMinX + 8)) / 4;
		
		let top    = GetClipMinY + 4;
		let height = ((GetClipMaxY - 4) - top) / 4;
		let length = ((height - 8)^2 + (width)^2)^(1 / 2);
		
		let color = [BLUE01, GREEN01, YELLOW01, RED01];
		
		ascent(let i in 0..4) {
			setLaser(cx - width *  i     , top + 2         , color[i],   0, width * 2 * i);
			setLaser(cx + width *  i     , top + 2         , color[i],  65, length);
			setLaser(cx + width * (i + 1), top + height - 2, color[i], 180, width * 2 * (i + 1));
			setLaser(cx - width * (i + 1), top + height - 2, color[i], 295, length);
			top += height;
		}
		
		function setLaser(x, y, color, angle, length) {
			let obj = createObjLaser_(x, y, length + 30, 10, angle, color, 250, -1);
			laser = laser ~ [obj];
		}
	}
	
	task TShot {
		loop {
			SetAction(ACT_SLASH_H, 30);
			if(((GetAngleToPlayer - 90)^2)^(1 / 2) > 45) {
				let angle = rand(0, 360);
				let way = 256;
				let span = 360 / way;
				loop(way) {
					createNmlShot01_(GetX, GetY, 0.25, angle, GRAIN, WHITE, 0);
					angle += span;
				}
			}else {
				let baseAngle = GetAngleToPlayer;
				let way = 4;
				let span = 18;
				let angle = baseAngle - (way - 1) / 2 * span;
				loop(way) {
					TTShot(angle);
					angle += span;
				}
			}
			
			wait(30);
			SetAction(ACT_STAND, 0);
			wait(90);
			SetAction(ACT_CHARGE, 60);
			wait(60);
		}
		
		task TTShot(_angle) {
			let obj = createObjShot_(GetX, GetY, 3, _angle, MIDDLE, BLUE, 0);
			
			loop {
				ascent(let i in 0..length(laser)) {
					if(Collision_Obj_Obj(obj, laser[i])) {
						Obj_Delete(obj);
						let span = 20;
						let way = 5;
						let nAngle = Obj_GetAngle(laser[i]) - (way - 1) / 2 * span - 90;
						loop(way) {
							TTTShot(Obj_GetX(obj), Obj_GetY(obj), nAngle);
							nAngle += span;
						}
						return;
					}
				}
				yield;
			}
			
			task TTTShot(__x, __y, __angle) {
				let _obj = createObjShot_(__x, __y, 1.75, __angle, SMALL, GREEN, 0);
				
				wait(30);
				
				loop {
					ascent(let i in 0..length(laser)) {
						if(Collision_Obj_Obj(_obj, laser[i])) {
							Obj_Delete(_obj);
							let _span = 15;
							let _way = 7;
							let _nAngle = Obj_GetAngle(laser[i]) - (_way - 1) / 2 * _span - 90;
							loop(_way) {
								TTTTShot(Obj_GetX(_obj), Obj_GetY(_obj), _nAngle);
								_nAngle += _span;
							}
							return;
						}
					}
					yield;
				}
				
				task TTTTShot(___x, ___y, ___angle) {
					let __obj = createObjShot_(___x, ___y, 1, ___angle, WEDGE, YELLOW, 0);
					
					wait(60);
					
					loop {
						ascent(let i in 0..length(laser)) {
							if(Collision_Obj_Obj(__obj, laser[i])) {
								Obj_Delete(__obj);
								let __span = 10;
								let __way = 7;
								let __nAngle = Obj_GetAngle(laser[i]) - (__way - 1) / 2 * __span - 90;
								loop(__way) {
									TTTTTShot(Obj_GetX(__obj), Obj_GetY(__obj), __nAngle);
									__nAngle += __span;
								}
								return;
							}
						}
						yield;
					}
					
					function TTTTTShot(____x, ____y, ____angle) {
						createNmlShot01_(____x, ____y, 0.5, ____angle, KUNAI, RED, -1);
					}
				}
			}
		}
	}
	#include_function ".\lib\六師外道body.dnh"
}
