#TouhouDanmakufu[Single]
#Title[Koishi - Spell #7]
#BackGround[User(.\bgNon.png, 0, 0)]
#BGM[.\music0.mp3]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {

	#include_function ".\Include2.~ath"

	task codeInit {
		SetLife(10000); SetTimer(64);
		FadeOutMusic(GCSD ~ "music0.mp3", 20); attack0;
		SetMovePosition03(GetCenterX, GetCenterY, 10, 4);
		cutin("Subconscious Beauty "\""Artificial Reality"\",imgBossCut,0,0,255,511); SetScore(1000000); SetDurableSpellCard;
		LoadUserShotData(GCSD ~ "ShotDefine2.~ath");
		SetInvincibility(240);
		wait(240);
		PlayMusic(GCSD ~ "music1.mp3");
		field;
		wait(600);
		bullet;
		net;
		grid;
		grid2;
	}
	task MoveAroundEasy {
		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj, 640-112, 240+128);
		ObjEffect_SetTexture(obj, imgMoveEasy);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetLayer(obj, 8);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -96, -96);
		ObjEffect_SetVertexXY(obj, 1, 96, -96);
		ObjEffect_SetVertexXY(obj, 2, 96, 96);
		ObjEffect_SetVertexXY(obj, 3, -96, 96);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 192, 0);
		ObjEffect_SetVertexUV(obj, 2, 192, 192);
		ObjEffect_SetVertexUV(obj, 3, 0, 192);
		ascent(i in 0..360) {
			ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, 255*sin(i/2), 255, 255, 255); }
			yield;
		}
	}
	let posX = 0;
	let posY = 0;
	let ttf = 150;
	task attack0 {
		let unfocSpeed = GetPlayerInfo(PLAYER_SPEED_HIGH)/2;
		let focusSpeed = GetPlayerInfo(PLAYER_SPEED_LOW)/2;
		function isFocusing { return GetKeyState(VK_SLOWMOVE)!=KEY_FREE; }
		function v { return [unfocSpeed, focusSpeed][isFocusing]; }
		loop {
			SetPlayerX(GetCenterX); SetPlayerY(GetCenterY);
			if(GetKeyState(VK_RIGHT)!=KEY_FREE && GetKeyState(VK_DOWN)!=KEY_FREE) {
				posX+=v*cos(45); posY+=v*sin(45);
			} else if(GetKeyState(VK_RIGHT)!=KEY_FREE && GetKeyState(VK_UP)!=KEY_FREE) {
				posX+=v*cos(-45); posY+=v*sin(-45);
			} else if(GetKeyState(VK_LEFT)!=KEY_FREE && GetKeyState(VK_DOWN)!=KEY_FREE) {
				posX+=v*cos(180-45); posY+=v*sin(180-45);
			} else if(GetKeyState(VK_LEFT)!=KEY_FREE && GetKeyState(VK_UP)!=KEY_FREE) {
				posX+=v*cos(180+45); posY+=v*sin(180+45);
			} else if(GetKeyState(VK_RIGHT)!=KEY_FREE) {
				posX+=v*cos(0); posY+=v*sin(0);
			} else if(GetKeyState(VK_DOWN)!=KEY_FREE) {
				posX+=v*cos(90); posY+=v*sin(90);
			} else if(GetKeyState(VK_LEFT)!=KEY_FREE) {
				posX+=v*cos(180); posY+=v*sin(180);
			} else if(GetKeyState(VK_UP)!=KEY_FREE) {
				posX+=v*cos(-90); posY+=v*sin(-90);
			}
			if(posX > 75) { posX = 75; }
			if(posX < -75) { posX = -75; }
			if(posY > 75) { posY = 75; }
			if(posY < -75) { posY = -75; }
			if(OnPlayerMissed) { posX = 0; posY = 0; }
			if(GetPlayerBomb) { AddBomb(-1); }
			wait(1);
		}
	}
	task field {
		let a = 0;
		let n = 0;
		loop(4) { n++; loop(60*(1+n*2)/6) {
			ascent(i in 0..n) { CreateLaser(75*cos(a+i*360/n)+100*cos(a+i*360/n+90), 75*sin(a+i*360/n)+100*sin(a+i*360/n+90), 75*cos(a+i*360/n)+100*cos(a+i*360/n+270), 75*sin(a+i*360/n)+100*sin(a+i*360/n+270)); }
			a+=32/pi; a%=360;
			wait(5+n);
		} }
	}
	function bullet {
		MoveAroundEasy;
		loop(9*60/15) {
			loop(3) { CreateBullet(rand(-75,75),rand(-75,75)); }
			let a = rand(0,360); let b = rand(0,360); let x = rand(-60, 60); let y = rand(-60, 60);
			ascent(i in 0..4) { ascent(j in 0..2) {
				CreateBullet(x+(j+1)*7*cos(a+i*360/4), y+(j+1)*7*sin(a+i*360/4));
			} }
			wait(15);
		}
		loop(12*60/30) {
			let a = rand(0,360); let b = rand(0,360); let x = -posX+rand(-20, 20); let y = -posY+rand(-20, 20);
			ascent(i in 0..14) { ascent(j in 0..4) {
				CreateBullet(x+(j+1)*11*cos(a+i*360/10+j*7), y+(j+1)*11*sin(a+i*360/10+j*7));
			} }
			wait(30);
		}
		wait(150);
	}
	function net {
		ttf = 210;
		loop(600/4) {
			let a = rand(0, 360); let b = rand(-40, 40);
			CreateLaser(400*cos(a), 400*sin(a), 400*cos(a+180+b), 400*sin(a+180+b));
			wait(4);
		}
		wait(120);
	}
	function grid {
		loop(13) {
			let x = -posX+rand(-20,20); let y = -posY+rand(-20,20); let a = rand(0,360);
			ascent(i in -4..5) { ascent(j in -4..5) {
				CreateBullet(x+10*i*cos(a)+10*j*cos(a+90), y+10*i*sin(a)+10*j*sin(a+90));
			} }
			wait(45);
		}
		wait(60);
	}
	function grid2 {
		let x = -posX+rand(-20,20); let y = -posY+rand(-20,20);
		ascent(i in 0..6) { ascent(j in 0..5) { CreateLaser(x+(15+j*10)*cos(i*360/6)+100*cos(i*360/6+90), y+(15+j*10)*sin(i*360/6)+100*sin(i*360/6+90), x+(15+j*10)*cos(i*360/6)+100*cos(i*360/6+270), y+(15+j*10)*sin(i*360/6)+100*sin(i*360/6+270)); } }
	}
	task CreateLaser(x, y, x2, y2) {
		let obj = Obj_Create(OBJ_LASER);
		ObjShot_SetGraphic(obj, BLUE11);
		ObjShot_SetBombResist(obj, true);
		Obj_SetAutoDelete(obj, false);
		ObjLaser_SetSource(obj, false);
		let alph = 0;
		ascent(time in 0..ttf) {
			let currX = GetCenterX-(posX+x)*(time^5/(ttf^5)*3);
			let currY = GetCenterY-(posY+y)*(time^5/(ttf^5)*3);
			let currX2 = GetCenterX-(posX+x2)*(time^5/(ttf^5)*3);
			let currY2 = GetCenterY-(posY+y2)*(time^5/(ttf^5)*3);
			Obj_SetPosition(obj, currX, currY);
			Obj_SetAngle(obj, atan2(currY2-currY, currX2-currX));
			ObjLaser_SetLength(obj, ((currY2-currY)^2+(currX2-currX)^2)^0.5);
			ObjLaser_SetWidth(obj, alph*15);
			Obj_SetAlpha(obj, 155*(time^3/(ttf^3)));
			Obj_SetCollisionToPlayer(obj, alph >= 1);
			if(time >= ttf-5) { ObjShot_SetGraphic(obj, RED11); }
			if(alph < 1) { alph+=1/(ttf-2); }
			wait(1);
		}
		Obj_Delete(obj);
	}
	task CreateBullet(x, y) {
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, GCSD ~ "supershot.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetLayer(obj,4);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 256,189); ObjEffect_SetVertexUV(obj, 1, 320,189); ObjEffect_SetVertexUV(obj, 2, 320,255); ObjEffect_SetVertexUV(obj, 3, 256,255);
		ObjEffect_SetVertexXY(obj, 0, -16, -16); ObjEffect_SetVertexXY(obj, 1, 16, -16); ObjEffect_SetVertexXY(obj, 2, 16, 16); ObjEffect_SetVertexXY(obj, 3, -16, 16);
		ascent(time in 0..ttf) {
			let currX = GetCenterX-(posX+x)*(time^5/(ttf^5)*10);
			let currY = GetCenterY-(posY+y)*(time^5/(ttf^5)*10);
			Obj_SetPosition(obj, currX, currY);
			ObjEffect_SetScale(obj, time^5/(ttf^5) * 3, time^5/(ttf^5) * 3);
			//ascent(i in 0..4) { ObjEffect_SetVertexColor(obj, i, 255, cos((ttf-time)*ttf/90)*255, cos((ttf-time)*ttf/90)*255, cos((ttf-time)*ttf/90)*255); }
			wait(1);
			if(time == ttf-1) {
				if(((GetPlayerX-Obj_GetX(obj))^2+(GetPlayerY-Obj_GetY(obj))^2)^0.5 < 20*3) {
					if(GetTimeOfPlayerInvincibility == 0) {
						PlaySE("seGraze.wav");
						AddGraze(1);
					}
				}
				SetCollisionB(Obj_GetX(obj), Obj_GetY(obj), 12*3);
			}
		}
		Obj_Delete(obj);
	}
}