#e
#Title[΂ɁAȂŉ]
#Text[ႫɂȂƌĔ`ȂꂳȂĂ̐ɂȂ]
#PlayLevel[Normal]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
	#include_function ".\lib\Common.dnh"
	#include_function ".\img\lib_honoka.dnh"
	
	let name = "ԁu킵̔閧v";
	let bossImg = CSD ~ "img\dot_honoka.png";
	let bossCut = "";
	let back = CSD ~ "img\back01.png";
	let imgList = [bossImg, bossCut, back];
	
	xIni = cx;
	yIni = TOP + 128;
	let wIni = 180;
	
	let STATE_STAND = 0;
	let STATE_SHOT = 1;
	let STATE_LEFT = 2;
	let STATE_RIGHT = 3;
	let state = 0;
	let alpha = 255;
	
	@Initialize {
		SetLife(2000);
		SetDamageRate(0, 0);
		SetTimer(77);
		SetScore(7000000);
		
		ascent(let i in 0..length(imgList)) {
			LoadGraphic(imgList[i]);
		}
		
		LoadUserShotData(CSD ~ "\img\shot_all.dnh");
		
		CutIn(YOUMU, name, "", 0, 0, 0, 0);
		
		TMain;
	}

	@MainLoop {
		if(GetPlayerY > cy + 58) {
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 32);
			alpha = min(255, alpha + 10);
		}else {
			alpha = max(96, alpha - 3);
		}
		yield;
	}

	@DrawLoop {
		SetAlpha(alpha);
		DrawBoss(bossImg);
	}

	@Finalize {
		ascent(let i in 0..length(imgList)) {
			DeleteGraphic(imgList[i]);
		}
	}

	@BackGround {
		SetTexture(back);
		SetGraphicRect(0, 0, 384, 448);
		SetAlpha(alpha / 4 + 50);
		DrawGraphic(cx, cy);
	}

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

	task TMain {
		getReady(wIni);
		
		shot01;
		
		wait(240);
		
		SetDamageRate(50, 50);
		shot02;
	}
	
	task shot01 {
		// ق̂Az`[tƂe
		// Ԍo߂Ƃɒ͒AԊu͒Z
		
		let long = 12;		// @v͒e12R
		let interval = 80;		// Ԋu@Pʂ̓t[
		let count = 0;		// ܂łɉxꂽJE^ϐ
		loop {
			shot(rndColor, long);
			wait(interval);
			
			count++;
			if(count % round(long / 3) == 0) {
				if(count % round(long / 6) == 0) {
					interval = max(40, interval - 2);
					count = 0;
				}
				long += 4;
			}
			
			yield;
		}
		
		task shot(color, lo) {
			let x = GetX;
			let y = GetY;
			let angle = GetAngleToPlayer;
			let maxS = 2.5;
			let minS = 1.5;
			let speed = [rand(minS, maxS)];
			let disp = rand(0, 360);
			let Ddisp = 3;
			
			let obj = [];
			ascent(let i in 0..lo) {
				obj = obj ~ [Obj_Create(OBJ_SHOT)];
				Obj_SetPosition(obj[i], x, y);
				Obj_SetSpeed(obj[i], speed[0]);
				Obj_SetAngle(obj[i], angle + 90);
				ObjShot_SetGraphic(obj[i], color + RICE);
				ObjShot_SetDelay(obj[i], i + 10);
			}
			
			let _count = 0;
			loop {
				descent(let i in 0..min(lo, _count)) {
					if(i == 0) {
						if(speed[0] < minS) {
							speed[0] = speed[0] + rand(-0.1, 0.2);
						}else if(speed[0] > maxS) {
							speed[0] = speed[0] + rand(-0.2, 0.1);
						}else {
							speed[0] = speed[0] + rand(-0.2, 0.2);
						}
						Obj_SetSpeed(obj[0], speed[0]);
						
						disp += Ddisp;
						Obj_SetAngle(obj[0], Obj_GetAngle(obj[0]) + cos(disp) - 90);
					}else {
						if(length(speed) <= i) {
							speed = speed ~ [speed[i - 1]];
						}else {
							speed[i] = speed[i - 1];
						}
//						Obj_SetSpeed(obj[i], speed[i]);
						Obj_SetAngle(obj[i], Obj_GetAngle(obj[i - 1]) - 90);
					}
				}
				
				ascent(let i in 0..min(lo, _count)) {
					Obj_SetPosition(obj[i], Obj_GetX(obj[i]) + speed[i] * cos(Obj_GetAngle(obj[i])), Obj_GetY(obj[i]) + speed[i] * sin(Obj_GetAngle(obj[i])));
					Obj_SetSpeed(obj[i], 0);
					Obj_SetAngle(obj[i], Obj_GetAngle(obj[i]) + 90);
					if(Obj_BeDeleted(obj[i])) {
						Obj_SetAngle(obj[i], Obj_GetAngle(obj[i]) - 90);
						break;
					}
				}
				
				if(_count >= lo) {
					if(Obj_BeDeleted(obj[lo - 1])) {
						return;
					}
				}
				
				_count++;
				yield;
			}
		}
	}
	
	task shot02 {
		let span = 30;
		let interval = 120;
		loop {
			let x = LEFT - rand(0, span);
			
			while(x < RIGHT) {
				shot(x);
				x += span;
			}
			
			wait(interval);
			interval = max(60, interval - 2);
		}
		
		task shot(_x) {
			let obj = [];
			ascent(let i in 0..2) {
				obj = obj ~ [Obj_Create(OBJ_SHOT)];
				Obj_SetPosition(obj[i], _x, BOTTOM - 4);
				Obj_SetSpeed(obj[i], 1);
				Obj_SetAngle(obj[i], -90 + 45 * [1, -1][i == 0]);
				ObjShot_SetGraphic(obj[i], RED + SMALLEX);
				ObjShot_SetDelay(obj[i], 20);
			}
			
			while(!Obj_BeDeleted(obj[0]) && !Obj_BeDeleted(obj[1])) {
				ascent(let i in 0..2) {
					Obj_SetAngle(obj[i], Obj_GetAngle(obj[i]) + 0.05 * [1, -1][i == 0]);
				}
				yield;
			}
		}
	}
}