#Title[榋巘奜摴懘偺擇]
#Text[僷僋僟丒僇僢僠儍乕儎僫]
#PlayLevel[EXTRA]
#Player[FREE]
#ScriptVersion[2]

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

	let name = "榋巘乽幍梫慺偺楐偗栚偵棊偪偨嫢恘乿";

	let xIni = cx;
	let yIni = cy;

	sub onInitialize {
		CutIn(YOUMU, name, bossCut, 1, 1, 463, 655);

		SetLife(2000);
		SetScore(2500000);
		SetTimer(80);
		SetDamageRate(0, 0);
		back = CSD ~ "img\back1.png";

		TMain;
	}

	sub onMainLoop {
		SetCollisionA(GetX, GetY, 32);
		yield;
	}

	sub onDrawLoop {
	}

	sub onFinalize {
		DeleteGraphic(bossImg);
		createItems(ITEM_SCORE, 32, 64);
	}

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

	task TMain {
		getReady(120);

		wait(5);

		Concentration01(60);
//		SetAction(ACT_SHOT_A, 600);
		wait(60);
		ascent(let i in 0..7) {
			CreateEnemyFromScript("Familiar", GetX, GetY, 0, 0, i);
		}

		SetDamageRate(5, 5);

		wait(570);

		SetDamageRate(15, 15);
		TRate;
		loop{
			SetAction(ACT_SHOT_B, 30);
			shot;
			wait(360);
		}
	}

	task TRate {
		let rate = 10;
		loop {
			wait(900);
			rate += 5;
			SetDamageRate(rate, rate);
		}
	}

	sub shot {
		let angle = rand(1, 360);
		let way = 54;
		if(((GetPlayerX - GetX)^2 + (GetPlayerY - GetY)^2)^(1/2) > cx - GetClipMinX) {
			way = 480;
		}
		let span = 360 / way;

		loop(way) {
			createNmlShot01_(GetX, GetY, 0.5, angle, GRAIN, WHITE, 2);
			angle += span;
		}
	}
	#include_function ".\lib\榋巘奜摴body.dnh"
}

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

script_enemy Familiar {
	#include_function ".\lib\榋巘奜摴Common_.dnh"
	let arg = GetArgument;

	let EARTH = 0;
	let WATER = 1;
	let FIRE  = 2;
	let WIND  = 3;
	let PAIN  = 4;
	let HAPPY = 5;
	let LIFE  = 6;
	let bullet = [ORANGE, BLUE, RED, GREEN, PURPLE, YELLOW, WHITE];

	let famType;
	let laserType;

	let maxR = cx - GetClipMinX - 5;

	@Initialize {
		SetLife(1000);
		SetDamageRateEx(0, 0, 2.5, 2.5);
		SetScore(20000);

		alternative(arg)
		case(EARTH) {
			famType = ORANGE;
			laserType = ORANGE01;
		}
		case(WATER) {
			famType = BLUE;
			laserType = BLUE01;
		}
		case(FIRE) {
			famType = RED;
			laserType = RED01;
		}
		case(WIND) {
			famType = GREEN;
			laserType = GREEN01;
		}
		case(PAIN) {
			famType = PURPLE;
			laserType = PURPLE01;
		}
		case(HAPPY) {
			famType = YELLOW;
			laserType = YELLOW01;
		}
		case(LIFE) {
			famType = WHITE;
			laserType = WHITE01;
		}
		others {}

		TMain;
	}

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

	@DrawLoop {
	}

	@Finalize {
	}

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

	task TMain {
		getReady_;
		createLaser;

		wait(30);
		FireShot(arg);
		wait(240);

		wait(40 * arg);

		loop {
			wait(40 * 7);
			createShot;
		}
	}

	sub getReady_ {
		let angle = -90 + (360 / 7 * arg);
		let speed = 2;
		let r = 0;
		let obj = createObjShot_(GetX, GetY, 0, angle, MIDDLE, famType, 2);
		ObjShot_SetBombResist(obj, true);
		Obj_SetCollisionToPlayer(obj, false);

		loop(360 / speed) {
			angle += speed;
			r += maxR / (360 / speed);
			SetMovePosition02(cx + cos(angle) * r, cy + sin(angle) * r, 1);
			Obj_SetPosition(obj, GetX, GetY);
			yield;
		}
		Obj_SetPosition(obj, GetX, GetY);
	}

	sub createLaser {
		let toX    = cx + cos(-90 + (360 / 7) * (arg + 1)) * maxR;
		let toY    = cy + sin(-90 + (360 / 7) * (arg + 1)) * maxR;
		let length = ((toX - GetX) ^ 2 + (toY - GetY) ^ 2) ^ (1 / 2);
		let angle = atan2(toY - GetY, toX - GetX);

		CreateLaserA(arg, GetX, GetY, 0, 10, laserType, 0);
		SetLaserDataA(arg, 1  , angle, 0, length / 240, 0, 0);
		SetLaserDataA(arg, 240, NULL , 0, 0           , 0, 0);
		PlaySE(seLaser);
	}

	sub createShot {
		let baseAngle = GetAngleToPlayer;
		let span = 20;
		let angle = baseAngle - span * 3;

		ascent(let i in 0..7) {
			let obj = createObjShot_(GetX, GetY, 0.8, angle, KNIFE, bullet[(arg + i + 4) % 7], 0);

			if(i == 3) {
				Obj_SetCollisionToPlayer(obj, false);
			}
			angle += span;
		}
	}
}