#e
#Title[_ՁuGNXpfbhEIoVv]
#Text[] 
#BackGround[Default]
#ScriptVersion[2]

script_enemy_main{

	#include_function ".\lib\lib_anime_Sanae.txt"
	let imgShot  = GetCurrentScriptDirectory ~ "img\shot_all.png";
	let imgBoss = GetCurrentScriptDirectory() ~ "img\dot_sanae.png";
	let shot  =GetCurrentScriptDirectory~"SE\arrow01.wav";
	let shot2  =GetCurrentScriptDirectory~"SE\kira01_r.wav";
	let laser  =GetCurrentScriptDirectory~"SE\byoro07.wav";
	let Concentration  =GetCurrentScriptDirectory~"SE\eco00_r.wav";
	let Back=GetCurrentScriptDirectory~"img\kikuBack.png";
	let sippou=GetCurrentScriptDirectory~"img\sippou.png";
	let r  =105;
	let g  =105;
	let b  =105;
	let wIni = 120;

	@Initialize{
	LoadGraphic(imgBoss);
	LoadGraphic(Back);
	LoadGraphic(sippou);
	LoadUserShotData(GetCurrentScriptDirectory ~ "img\shot_All.txt");
        SetMovePosition02(GetCenterX(), 150, wIni/2);
        SetAction(ACT_MOVE, wIni/2);
	SetShotAutoDeleteClip(64,64,64,255);
        SetLife(200);
        SetTimer(70);
        SetInvincibility(wIni*2);
        SetDamageRate(8.5, 1.5);
	LoadGraphic(GetCurrentScriptDirectory~"img\back06_01.png");	
	InitializeAction();
	GMain;
	TMain;
	}
		
	@MainLoop{
		SetCollisionA(GetX(), GetY(), 32);
		SetCollisionB(GetX(), GetY(), 24);
	    yield;
	}
	
let BGx=0;
let BGy=0;
@BackGround{
		SetTexture(Back);
		SetGraphicRect(0,0,500,500);
		SetGraphicAngle(0, 0, 0);
		SetAlpha(155);
		SetColor(200,200,200);
		SetGraphicExpansion(1,1);
		DrawGraphic(224, 240); 

		SetTexture(sippou);
		SetGraphicRect(BGx,BGy,BGx+450,BGy+50000);
		SetRenderState(ADD);
		SetGraphicAngle(0, 0, 0);
		SetAlpha(100);
		SetColor(r,g,b);
		SetGraphicExpansion(1,1);
		DrawGraphic(224, 240);
		BGy-=1;
	}	  
	@Finalize{
	DeleteGraphic(imgBoss);
	}
	
		task GMain{
		loop{
		loop(50){
		r+=2;
		b-=2;
	    loop(4){ yield; }
			}
		loop(50){
		g+=2;
		r-=2;
	    loop(4){ yield; }
			}
		loop(50){
		b+=2;
		g-=2;
	    loop(4){ yield; }
			}
			}
			}
	task TMain{
	    SetAction(ACT_MOVE, wIni/2);	    
	    yield;
	    loop(wIni/2){ yield; }
	Concentration01(120);
        SetAction(ACT_SHOT_B,12000);
        CutIn(YOUMU, "_ՁuGNXpfbhEIoVv", "", 0, 0, 0, 0);
        SetScore(14000000/3);
	    loop(wIni/2){ yield; }
	    TMain2;
	    loop{
	    		let X=32;
	    		let X2=416;
	    		let S=12;
			move01;
        		SetAction(ACT_MOVE,60);
	    loop(11){
		EffectLaser(X, 0, S, 90, 450, 20, 1, 10, "ALPHA", "");
		EffectLaser(X2, 0, S, 90, 450, 20, 1, 10, "ALPHA", "");
		X+=16.1;
		X2-=16.1;
		S-=0.70;
                PlaySE(laser);
	        loop(15){ yield; }
		}
	        loop(180){ yield; }
	    }
			}
	task TMain2{
	    loop{
let angle=90;
                PlaySE(shot);
	    loop(8){
		CreateShotA(1,GetX,GetY,15);
		SetShotDirectionType(ABSOLUTE);
		SetShotDataA(1,0,4,angle,0,-0.075,0,94);
		SetShotDirectionType(PLAYER);
		SetShotDataA(1,55,1,0,0,-0.027,0,94);
		SetShotDirectionType(PLAYER);
		SetShotDataA(1,85,1,0,0,-0.027,0,94);
		SetShotDirectionType(PLAYER);
		SetShotDataA(1,115,1,0,0,0,0,94);
		FireShot(1);
		SetShotDirectionType(ABSOLUTE);
		angle+=45;
		}
	        loop(55){ yield; }
                PlaySE(shot2);
	        loop(5){ yield; }
		}
			}
			sub move01 {
	    if(GetX()>=304)
	    {
	        SetMovePosition02(GetX()-rand(25,75),rand(130,170),60);
	    }
	    else if(GetX()<=144)
	    {
	        SetMovePosition02(GetX()+rand(25,75),rand(130,170),60);
	    }
	    else if(GetX()<=GetPlayerX())
	    {
	        SetMovePosition02(GetX()+rand(25,75),rand(130,170),60);
	    }
	    else if(GetX()>=GetPlayerX())
	    {
	        SetMovePosition02(GetX()-rand(25,75),rand(130,170),60);
	    }
	}
	task DelayEff(type, x, y, speed, angle, color, scale, deltime) {
		if(OnPlayerMissed == true) { return; }

		let Obj  = Obj_Create(OBJ_SHOT);
		if(scale < 3){scale = 3;}
		if(scale <= deltime && type != 1){deltime = scale-1;}

		Obj_SetPosition(Obj, x, y);
		ObjShot_SetGraphic(Obj, color);
		ObjShot_SetDelay(Obj, scale);
		ObjShot_SetBombResist(Obj, true);
		Obj_SetCollisionToPlayer(Obj, false);

		ascent(i in 0..deltime) {
			Obj_SetPosition(Obj, Obj_GetX(Obj) + speed*cos(angle), Obj_GetY(Obj) + speed*sin(angle));
			if(type == 1 && i < deltime - (scale-2)) { ObjShot_SetDelay(Obj, scale); }

			yield;
		}

		Obj_Delete(Obj);
	}
	task sbshot(let x, let y, let speed, let angle, let way) {
		loop(way){
		CreateShotA(1,x,y,0);
		SetShotDataA(1,0,speed,angle,0,0,0,225);
		FireShot(1);
		loop(way/speed/1.5){yield;}
			}
	}


	task EffectLaser(x, y, speed, angle, Length, Width, color, delay, Render, GrazeSE) {
		if(OnPlayerMissed == true) {
			DelayEff(1, x, y, 0, 0, color, 15, (Length/speed));
			return;
				}
		let Obj     = Obj_Create(OBJ_EFFECT);
		let check   = Obj_Create(OBJ_SHOT);
		let check2  = false;
		let grazeC  = 0;

		let length  = 0;
		let graL    = [0, 30, 60, 90, 120, 150, 180, 210];
		let graT    = 50;
		let graR    = [29, 59, 89, 119, 149, 179, 209, 239];
		let graB    = 79;
		let alpha   = 255;
		if(GrazeSE == "") { GrazeSE = "seGraze.wav"; }

	        loop(delay){ yield; }
		sbshot(x, y, speed, angle, Length/20);
		Obj_SetPosition(Obj, x, y);
		Obj_SetAngle(Obj, angle);

		ObjEffect_SetTexture(Obj, imgShot);

		if(Render == "ALPHA")			{ ObjEffect_SetRenderState(Obj, ALPHA);
		} else if(Render == "ADD")		{ ObjEffect_SetRenderState(Obj, ADD);
		} else if(Render == "MULTIPLY") { ObjEffect_SetRenderState(Obj, MULTIPLY);
		} else if(Render == "SUBTRACT") { ObjEffect_SetRenderState(Obj, SUBTRACT);
		}

		ObjEffect_SetPrimitiveType(Obj, PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetAngle(Obj, 0, 0, angle-90);
		ObjEffect_SetScale(Obj, 1, 0);
		ObjEffect_SetLayer(Obj, 3);

		ObjEffect_CreateVertex(Obj, 4);
		ObjEffect_SetVertexXY(Obj, 0, -(Width/2), 0);
		ObjEffect_SetVertexXY(Obj, 1,  (Width/2), 0);
		ObjEffect_SetVertexXY(Obj, 2, -(Width/2), Length);
		ObjEffect_SetVertexXY(Obj, 3,  (Width/2), Length);

		ObjEffect_SetVertexUV(Obj, 0, graL[color-1], graB);
		ObjEffect_SetVertexUV(Obj, 1, graR[color-1], graB);
		ObjEffect_SetVertexUV(Obj, 2, graL[color-1], graT);
		ObjEffect_SetVertexUV(Obj, 3, graR[color-1], graT);

		Obj_SetPosition(check, Obj_GetX(Obj)+(length/2)*cos(angle), Obj_GetY(Obj)+(length/2)*sin(angle));
		Obj_SetAngle(check, angle);
		Obj_SetSpeed(check, 0);
		ObjShot_SetBombResist(check, true);
		ObjShot_ToItem(check, false);
		Obj_SetCollisionToPlayer(check, false);

		DelayEff(1, x, y, 0, 0, color, 15, (Length/speed));

		loop(int(Length/speed)) {
			if(Obj_BeDeleted(Obj) == true || Obj_BeDeleted(check) == true) { break; }

			if(OnPlayerMissed == true) {
				loop(60){
					alpha -= 255/60;

					ObjEffect_SetVertexColor(Obj, 0, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 1, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 2, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 3, alpha, 255, 255, 255);

					yield;
				}

				ObjEffect_SetVertexColor(Obj, 0, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 1, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 2, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 3, 0, 255, 255, 255);

				Obj_Delete(Obj);
				Obj_Delete(check);
			}


			length += speed;

			ObjEffect_SetScale(Obj, 1, length/Length);
			Obj_SetPosition(check, Obj_GetX(Obj)+(length/2)*cos(angle), Obj_GetY(Obj)+(length/2)*sin(angle));

			if( Collision_Line_Circle(Obj_GetX(Obj)+      (-20)*cos(angle),
									  Obj_GetY(Obj)+      (-20)*sin(angle),
									  Obj_GetX(Obj)+(length-20)*cos(angle),
									  Obj_GetY(Obj)+(length-20)*sin(angle),
									  Width, GetPlayerX, GetPlayerY, 0 ) == true &&
									  GetTimeOfPlayerInvincibility == 0) { ShootDownPlayer; }

			if( Collision_Line_Circle(Obj_GetX(Obj)+       (5)*cos(angle),
									  Obj_GetY(Obj)+       (5)*sin(angle),
									  Obj_GetX(Obj)+(length+5)*cos(angle),
									  Obj_GetY(Obj)+(length+5)*sin(angle),
									  Width+30, GetPlayerX, GetPlayerY, 0 ) == true &&
									  GetTimeOfPlayerInvincibility == 0) {		if(grazeC%6 == 0) { AddGraze(1); PlaySE(GrazeSE); }
																				grazeC++;
			} else { grazeC = 0; }

			yield;
		}

		while(!(Obj_GetX(Obj) < GetClipMinX - 100) && !(Obj_GetX(Obj) > GetClipMaxX + 100) &&
				!(Obj_GetY(Obj) < GetClipMinY - 100)  && !(Obj_GetY(Obj) > GetClipMaxY + 100) &&
				Obj_BeDeleted(Obj) == false && Obj_BeDeleted(check) == false) {

			if(OnPlayerMissed == true) {
				loop(60){
					alpha -= 255/60;

					ObjEffect_SetVertexColor(Obj, 0, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 1, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 2, alpha, 255, 255, 255);
					ObjEffect_SetVertexColor(Obj, 3, alpha, 255, 255, 255);

					yield;
				}

				ObjEffect_SetVertexColor(Obj, 0, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 1, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 2, 0, 255, 255, 255);
				ObjEffect_SetVertexColor(Obj, 3, 0, 255, 255, 255);

				Obj_Delete(Obj);
				Obj_Delete(check);
			}


			x = Obj_GetX(Obj) + speed*cos(angle);
			y = Obj_GetY(Obj) + speed*sin(angle);

			Obj_SetPosition(Obj, x, y);
			Obj_SetPosition(check, Obj_GetX(Obj)+(length/2)*cos(angle), Obj_GetY(Obj)+(length/2)*sin(angle));

			if( Collision_Line_Circle(Obj_GetX(Obj)+      (-20)*cos(angle),
									  Obj_GetY(Obj)+      (-20)*sin(angle),
									  Obj_GetX(Obj)+(length-20)*cos(angle),
									  Obj_GetY(Obj)+(length-20)*sin(angle),
									  Width, GetPlayerX, GetPlayerY, 0 ) == true &&
									  GetTimeOfPlayerInvincibility == 0) { ShootDownPlayer; }

			if( Collision_Line_Circle(Obj_GetX(Obj)+       (5)*cos(angle),
									  Obj_GetY(Obj)+       (5)*sin(angle),
									  Obj_GetX(Obj)+(length+5)*cos(angle),
									  Obj_GetY(Obj)+(length+5)*sin(angle),
									  Width+30, GetPlayerX, GetPlayerY, 0 ) == true &&
									  GetTimeOfPlayerInvincibility == 0) {		if(grazeC%6 == 0) { AddGraze(1); PlaySE(GrazeSE); }
																				grazeC++;
			} else { grazeC = 0; }

			yield;
		}	
		Obj_Delete(Obj);
		Obj_Delete(check);
	}
	@DrawLoop{
	  DrawBoss(imgBoss);
		DrawGraphic(GetX(),GetY());
	}
        

}
