#Title[Eternal Forth Blizzard]
#Text[͎]
#Image[]
#PlayLevel[Death]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
	#include_function ".\lib\Common.dnh"
	
	let bossImg = CSD ~ "img\dot_cirno.png";
	let back = CSD ~ "img\back2.png";
	let name = "IuG^[iEtH[XEuU[hv";
	
	let xIni = cx;
	let yIni = GetClipMinY + HEIGHT / 3;
	let wIni = 120;

	@Initialize {
		SetLife(10);
		SetDamageRate(0, 0);
		SetEffectForZeroLife(80, 32, 0);
		SetTimer(82);
		SetScore(40000000);
		
		SetDurableSpellCard;
		CutIn(KOUMA, name, "", 0, 0, 0, 0);
		
		LoadUserShotData(CSD ~ "img\shot_All.dnh");
		LoadGraphic(bossImg);
		LoadGraphic(back);
		LoadGraphic(imgExp);
		
		TMain;
	}

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

	@DrawLoop {
		SetTexture(bossImg);
		SetGraphicRect(0, 0, 128, 128);
		DrawGraphic(GetX, GetY);
	}

	@Finalize {
		DeleteGraphic(bossImg);
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
	
	task TMain {
		getReady(wIni);
		
		TSnow;
		
		shot1;
		wait(60);
		
		shot2;
		wait(600);
		
		shot3;
		wait(580);
		
		shot1;
		wait(50);
		
		shot4;
		wait(420);
		
		shot2;
		wait(380);
		
		shot3;
		wait(560);
		
		shot1;
		wait(40);
		
		shot3;
		wait(320);
		
		shot2;
		wait(300);
		
		shot4;
		wait(180);
		
		shot3;
		wait(180);
		
		shot2;
		wait(120);
		
		shot4;
		wait(480);
		
		shot1;
		slow;
	}
	
	task TSnow {
		let rect = [64, 0];
		let obj = [];
		let id = 0;
		yield;
		
		loop {
			obj = obj ~ [Obj_Create(OBJ_EFFECT)];
			Obj_SetPosition(obj[id], rand(LEFT, RIGHT), rand(TOP, cy - HEIGHT / 12));
			Obj_SetSpeed(obj[id], rand(0.8, 1.5));
			Obj_SetAngle(obj[id], rand(80, 100));
			
			ObjEffect_SetTexture(obj[id], imgExp);
			ObjEffect_SetRenderState(obj[id], ALPHA);
			ObjEffect_SetPrimitiveType(obj[id], PRIMITIVE_TRIANGLESTRIP);
			ObjEffect_CreateVertex(obj[id], 4);
			let zAngle = rand(0, 360);
			let addAngle = rand(-10, 10);
			ObjEffect_SetAngle(obj[id], 0, 0, zAngle);
			let scale = rand(0.3, 0.5);
			ObjEffect_SetScale(obj[id], scale, scale);
			
			ascent(let i in 0..4) {
				ObjEffect_SetVertexXY(obj[id], i, [-32, 32][trunc(i / 2)], [-32, 32][i % 2]);
				ObjEffect_SetVertexUV(obj[id], i, rect[0] + [0, 64][trunc(i / 2)], rect[1] + [0, 64][i % 2]);
				ObjEffect_SetVertexColor(obj[id], i, rand(32, 96), rand(230, 255), rand(230, 255), rand(230, 255));
			}
			
			spin(id, zAngle, addAngle);
			
			id++;
			wait(8);
		}
		
		task spin(_id, _z, _add) {
			let zAngle = _z;
			while(!Obj_BeDeleted(obj[_id])) {
				zAngle += _add / 10;
				ObjEffect_SetAngle(obj[_id], 0, 0, zAngle);
				yield;
			}
		}
	}
	
	sub slow {
		Slow(1);
		wait(60);
		Slow(2);
	}
	
	// 2.133b(128F)
	sub shot1 {
		let way0 = 24;
		let way1 = 8;
		let way2 = 16;
		let baseAngle = GetAngleToPlayer;
		let angle = baseAngle;
		let span0 = 360 / way0;
		let span1 = 360 / (way0 * way1);
		
		loop(way2) {
			loop(way1) {
				loop(way0) {
					CreateShot01(GetX + cos(angle) * 5, GetY + sin(angle) * 5, 2, angle, KUNAI + BLUE, 10);
					angle += span0;
				}
				angle += span1;
				yield;
			}
		}
	}
	
	// 10.00b(600F)
	task shot2 {
		let a = 0.05;
		let x = GetX;
		let y = GetY;
		let angle = 90;
		
		loop(60) {
			let obj = createLaser(x, y, 0, angle, WEDGEEX + AQUA, 128, 16, false, 5);
			fall(obj);
			wait(10);
			
			x = rand(LEFT, RIGHT);
			y = rand(TOP, cy - 64);
			angle = rand(75, 105);
		}
		
		task fall(_obj) {
			let speed = 0;
			let angle = Obj_GetAngle(_obj);
			
			while(!Obj_BeDeleted(_obj)) {
				Obj_SetPosition(_obj, Obj_GetX(_obj) + cos(angle) * speed, Obj_GetY(_obj) + sin(angle) * speed);
				speed += a;
				yield;
			}
		}
	}
	
	// 10.00b(600F)
	task shot3 {
		let way = 6;
		let angle = 0;
		let obj = [];
		
		ascent(let i in 0..way) {
			obj = obj ~ [createLaser(GetX + cos(angle) * 64, GetY + sin(angle) * 64, 0, angle, MIDDLEEX + AQUA, HEIGHT, 16, true, 60)];
			angle += 360 / way;
		}
	
		loop(240) {
			let a = rand(0, 360);
			let r = rand(1, 64);
			let x = GetX + cos(a) * r;
			let y = GetY + sin(a) * r;
			angle = rand(0, 60);
			let span = 360 / way;
			
			loop(way) {
				CreateShot01(x, y, 1.5, angle, LUMP + [BLUE, AQUA][rand_int(0, 3) == 0], 15);
				angle +=span;
			}
			wait(2);
		}
		
		wait(120);
		
		ascent(i in 0..way) {
			ObjShot_FadeDelete(obj[i]);
		}
	}
	
	// 10.00b(600F)
	task shot4 {
		let angle = 0;
		loop(150) {
			shot(angle);
			wait(4);
			angle += 20;
		}
		
		task shot(_angle) {
			let obj = [];
			let way = [5, 6][rand_int(0, 1)];
			let angle0 = _angle + rand(-15, 15);;
			let dAngle0 = rand(-0.1, 0.1);
			let x = GetX + cos(angle0) * 30;
			let y = GetY + sin(angle0) * 30;
			let angle1 = rand(0, 360);
			let dAngle1 = rand(-3, 3);
			
			let speed = rand(0.5, 1.0);
			let dSpeed = [];
			loop(way) {
				dSpeed = dSpeed ~ [rand(0.001, 0.005)];
			}
			
			let type = LUMP + [WHITE, BLUE][way == 5];
			
			ascent(let i in 0..way) {
				obj = obj ~ [Obj_Create(OBJ_SHOT)];
				Obj_SetPosition(obj[i], x + cos(angle1) * 10, y + sin(angle1) * 10);
				Obj_SetAngle(obj[i], angle1);
				Obj_SetSpeed(obj[i], 0);
				ObjShot_SetGraphic(obj[i], type);
				ObjShot_SetDelay(obj[i], 20);
				
				angle1 += 360 / way;
			}
			
			let j = 1;
			loop {
				let k = 0;
				angle0 += dAngle0;
				angle1 += dAngle1;
				ascent(i in 0..way) {
					if(Obj_BeDeleted(obj[i])) {
						k++;
					}
					
					Obj_SetX(obj[i], x + j * cos(angle0) * (speed + j * dSpeed[i]) + cos(angle1) * 10);
					Obj_SetY(obj[i], y + j * sin(angle0) * (speed + j * dSpeed[i]) + sin(angle1) * 10);
					Obj_SetAngle(obj[i], angle1);
					angle1 += 360 / way;
				}
				j++;
				
				if(k == way) {
					break;
				}
				
				yield;
			}
		}
	}
}