#Title[։u@ `iv̕ʂ`v]
#Text[u܂ς܂@̗t̏ŏ舧v
܂ςAہAʂƂȂ̐gł͍ĉ͊ʂI
lcǓƊƍŝȂ{炦II]
#PlayLevel[LastWard]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
	#include_function ".\lib\Common.dnh"
	#include_function ".\lib\lib_anime_Mokou.dnh"
	
	let bossImg = CSD ~ "img\dot_mokou.png";
	let name = "։u@ `iv̕ʂ`v";
	let back = CSD ~ "img\back2.png";
	let bgm = CSD ~ "bgm\bgm.wav";
	
	let xIni = cx;
	let yIni = HEIGHT / 3;
	let wIni = 180;
	
	let del = [2500, 2000, 1500, 800, 300];
	let TYPE = [GRAIN, SMALL, RICE, MIDDLE, BUTTER, AMULET, KUNAI];
	let COLOR = [RED, BLUE, PURPLE];
	
	let phase = -1;
	let phase_ = phase;
	
	@Initialize {
		SetLife(2500);
		SetDamageRate(0, 0);
		SetScore(2000000);
		SetTimer(120);
		CutIn(YOUMU, name, "", 0, 0, 0, 0);
		
		LoadUserShotData(CSD ~ "\img\shot_All.dnh");
		LoadGraphic(bossImg);
		LoadGraphic(back);
		LoadGraphic(imgExp);
		
		LoadMusic(bgm);
		PlayMusic(bgm);
		LastSpell;
		
		if(GetKeyState(VK_SKIP) == KEY_HOLD) {	// BR}h@Ԃs\
			SetTimer(60);
			SetLife(3000);
			SetInvincibility(60*60);
			SetDurableSpellCard;
			phase = 2;
			ExtendPlayer(-4);
		}
		
		
		InitializeAction;
		rand_initialize(3);
		
		TMain;
	}

	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 32);
		yield;
	}

	@DrawLoop {
		DrawBoss(bossImg);
	}

	@Finalize {
		DeleteGraphic(bossImg);
	}

	@BackGround {
		SetGraphicRect(0, 0, 384, 448);
		SetTexture(back);
		DrawGraphic(cx, cy);
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////
	
	task TMain {
		getReady(wIni);
		yield;
		
		setPhase;
		TRate;
		
		bunshin;
		wait(180);
		
		let x = GetX;
		let y = GetY;
		loop {
			if(phase >= 3) {
				let way = 36;
				let angle = GetAngleToPlayer;
				loop(36) {
					CreateShot01(GetX, GetY, 1, angle, RED + SMALL, 20);
					angle += (360 / way);
				}
			}
			
			shot(x, y);
			if(phase >= 4) {
				x = cx + (cx - x);
				shot(x, y);
			}
			x = random(LEFT+32, RIGHT-32);
			y = random(TOP+16, cy-16);
			wait(40);
			moveToPlayer(WIDTH/7, rand(-16, 16), 40, WIDTH/10, 64, WIDTH/10, HEIGHT/2+64);
			SetAction(ACT_MOVE, 40);
			wait(80 - phase * 15);
		}
	}
	
	task TRate {
		let rate = 10;
		loop {
			SetDamageRate(rate, 0);
			wait(600 + rate * 10);
			rate += 2.25;
			if(phase >= 4) {
				SetDamageRate(9.5, 0);
				break;
			}
		}
	}
	
	task bunshin {
		// g̃GtFNgAړyяł̐s
		// 0.(cx, cy-HEIGHT/8)Ɉړ@ @0bɏ
		// 1.(WIDTH*3/8, HEIGHT/6)Ɉړ@21bɏ
		// 2.E(WIDTH*5/8, HEIGHT/6)Ɉړ@39bɏ
		// 3.(WIDTH*1/8, HEIGHT/2)Ɉړ@54bɏ
		// 4.E(WIDTH*7/8, HEIGHT/2)Ɉړ@66bɏ
		let obj = [];
		let toGo = [[cx, cy-HEIGHT/18], [LEFT+WIDTH*3/8, TOP+HEIGHT/6], [LEFT+WIDTH*5/8, TOP+HEIGHT/6], [LEFT+WIDTH*1/8, TOP+HEIGHT/2], [LEFT+WIDTH*7/8, TOP+HEIGHT/2]];
		ascent(let i in 0..5) {
			obj = obj ~ [Obj_Create(OBJ_EFFECT)];
			Obj_SetPosition(obj[i], GetX, GetY);
			Obj_SetCollisionToPlayer(obj[i], false);
			ObjEffect_SetTexture(obj[i], bossImg);
			ObjEffect_SetRenderState(obj[i], ALPHA);
			ObjEffect_SetPrimitiveType(obj[i], PRIMITIVE_TRIANGLEFAN);
			ObjEffect_CreateVertex(obj[i], 4);
			ascent(let j in 0..4) {
				ObjEffect_SetVertexColor(obj[i], j, 255, 127, 127, 127);
			}
			ObjEffect_SetVertexXY(obj[i], 0, -64, -64);
			ObjEffect_SetVertexXY(obj[i], 1,  64, -64);
			ObjEffect_SetVertexXY(obj[i], 2,  64,  64);
			ObjEffect_SetVertexXY(obj[i], 3, -64,  64);
			anime(obj[i]);
		}
		
		ascent(i in 0..5) {
			Obj_SetSpeed(obj[i],  distance(Obj_GetX(obj[i]), Obj_GetY(obj[i]), toGo[i][0], toGo[i][1]) / 120);
			Obj_SetAngle(obj[i], direction(Obj_GetX(obj[i]), Obj_GetY(obj[i]), toGo[i][0], toGo[i][1]))
		}
		
		yield;
		wait(120);
		
		ascent(i in 0..5) {
			Obj_SetSpeed(obj[i], 0);
		}
		
		ascent(i in 0..5) {
			while(phase_ < i) {
				yield;
			}
			delete(obj[i], i);
		}
		
		task anime(_obj) {
			let actionCount = 0;
			let RECT_STAND0		= [0, 0];
			let RECT_STAND1		= [0, 128];
			let RECT_STAND2		= [0, 2 * 128];
			let RECT_BLINK0		= [128, 0];
			let RECT_BLINK1		= [128, 128];
			
			while(!Obj_BeDeleted(_obj)) {
				let cutFrame = 12;
				let count = actionCount % (cutFrame * 12);
				let number = trunc(count / cutFrame);
				let anime = [ 0, 1, 2, 0, 1, 2, 0, 3, 4, 0, 1, 2, 2 ][ number ];
				let rect = [ RECT_STAND0, RECT_STAND1, RECT_STAND2,
					RECT_BLINK0, RECT_BLINK1 ][ anime ];
				
				ObjEffect_SetVertexUV(_obj, 0, rect[0], rect[1]);
				ObjEffect_SetVertexUV(_obj, 1, rect[0]+128, rect[1]);
				ObjEffect_SetVertexUV(_obj, 2, rect[0]+128, rect[1]+128);
				ObjEffect_SetVertexUV(_obj, 3, rect[0], rect[1]+128);
				
				actionCount++;
				yield;
			}
		}
		
		task delete(_obj, _i) {
			let alpha = 255;
			while(alpha > 0) {
				alpha = max(alpha - 1, 0);
				ascent(let j in 0..4) {
					ObjEffect_SetVertexColor(_obj, j, alpha, alpha/2, alpha/2, alpha/2);
				}
				if(alpha == 128) {
					EXP(Obj_GetX(_obj), Obj_GetY(_obj), EXP_DOT, 50+10*(_i+1));
				}
				yield;
			}
			CreateShot01(Obj_GetX(_obj), Obj_GetY(_obj), 0.5, direction(Obj_GetX(_obj), Obj_GetY(_obj), GetPlayerX, GetPlayerY), FIRE, 20);
			Obj_Delete(_obj);
		}
	}
	
	task shot(_x, _y) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, _x, _y);
		Obj_SetSpeed(obj, 0.2);
		Obj_SetAngle(obj, direction(_x, _y, GetPlayerX, GetPlayerY));
		ObjShot_SetGraphic(obj, RED + RING);
		
		acceleration(obj);
		
		while(!Obj_BeDeleted(obj)) {
			let r = rand_int(0, 100);
			let type = 0;
			let color = 0;
			
			if(r <= 20 + phase * 3) {
				alternative(phase)
				case(0) {
					alternative(r % 5)
					case(0, 1) {
						type = TYPE[0];
					}case(2, 3) {
						type = TYPE[1];
					}case(4) {
						type = TYPE[2];
					}
					
					alternative(r % 10)
					case(0) {
						color = COLOR[1];
					}others {
						color = COLOR[0];
					}
				}case(1) {
					alternative(r % 10)
					case(0, 1, 2) {
						type = TYPE[0];
					}case(3, 4, 5) {
						type = TYPE[1];
					}case(6, 7) {
						type = TYPE[2];
					}case(8) {
						type = TYPE[3];
					}case(9) {
						type = TYPE[4];
					}
					
					alternative(r % 10)
					case(0, 1, 2) {
						color = COLOR[1];
					}others {
						color = COLOR[0];
					}
				}case(2) {
					alternative(r % 15)
					case(0, 1, 2, 3, 4, 5) {
						type = TYPE[0];
					}case(6, 7, 8) {
						type = TYPE[1];
					}case(9, 10) {
						type = TYPE[2];
					}case(11, 12) {
						type = TYPE[3];
					}case(13) {
						type = TYPE[4];
					}case(14) {
						type = TYPE[5];
					}
											
					alternative(r % 10)
					case(0, 1, 2) {
						color = COLOR[1];
					}case(3, 4) {
						color = COLOR[2];
					}others {
						color = COLOR[0];
					}
				}case(3) {
					alternative(r % 15)
					case(0, 1, 2, 3) {
						type = TYPE[0];
					}case(4, 5, 6) {
						type = TYPE[1];
					}case(7, 8) {
						type = TYPE[2];
					}case(9, 10) {
						type = TYPE[3];
					}case(11, 12) {
						type = TYPE[4];
					}case(13) {
						type = TYPE[5];
					}case(14) {
						type = TYPE[6];
					}
											
					alternative(r % 10)
					case(0, 1, 2, 3) {
						color = COLOR[1];
					}case(4, 5, 6) {
						color = COLOR[2];
					}others {
						color = COLOR[0];
					}
				}case(4, 5, 6, 7, 8, 9) {
					alternative(r % 15)
					case(0, 1) {
						type = TYPE[0];
					}case(2, 3) {
						type = TYPE[1];
					}case(4, 5) {
						type = TYPE[2];
					}case(6) {
						type = TYPE[3];
					}case(7, 8, 9, 10, 11) {
						type = TYPE[4];
					}case(12, 13) {
						type = TYPE[5];
					}case(14) {
						type = TYPE[6];
					}
											
					alternative(r % 10)
					case(0, 1, 2, 3) {
						color = COLOR[1];
					}case(4, 5, 6, 7, 8) {
						color = COLOR[2];
					}others {
						color = COLOR[0];
					}
				}
				
				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), max(Obj_GetSpeed(obj), 2) * rand(0.5 - 0.05 * phase, 0.6), Obj_GetAngle(obj) + rand(-15 - 3 * phase, 15 + 3 * phase), type + color, 5);
			}
			yield;
		}
		
		task acceleration(_obj) {
			let speed = Obj_GetSpeed(_obj);
			let accel = 0.2;
			let maxSp = 3;
			
			while(speed < maxSp) {
				speed = min(speed + accel, maxSp);
				Obj_SetSpeed(_obj, speed);
				yield;
			}
		}
	}
	
	task setPhase {
		loop {
			if(phase <= 3 && GetLife <= del[phase+1]) {
				phase_++;
				wait(255);
				phase++;
			}
			if(GetTimer <= 25) {
				phase = 5;
				phase_ = 5;
				break;
			}
			yield;
		}
	}
}