#東方弾幕風
#Title[波符「湖上円舞」]
#Text[大妖精のスペルカード]
#BackGround[User]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
        let count=0;
        let name    = "波符「湖上円舞」";
        let csd     = GetCurrentScriptDirectory;
        let imgBoss =csd~"img\dot_daiyousei.png";
	let imgEnemy=csd~"img\familiar2.png";
        let imgSPELL =csd~"img\SPELL_H.png";
	let big=0;
	let imgAngle=0;
	let imgALP1=255;
	let imgALP2=0;
	let imgBACK=0;
	let retune=0;
	let Lock=0;
	let l=1;
	let angle=0;
	let angleA=0;
	let angleB=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
	let AnimationA=0;
	let AnimationB=0;
	let AnimationC=0;
	let shotseA = csd~"se\ShotSE01.wav";
	let shotseB = csd~"se\Powar.wav";

    @Initialize {
        CutIn(YOUMU,name,0,0,0,0,0);
        SetLife(3500);
        SetTimer(60);
        SetScore(1000000);
        SetDamageRate(100,0);
        SetInvincibility(200);
        SetMovePosition02(cx,120,60);
	LoadGraphic(imgBoss);
	LoadGraphic(imgEnemy);
	LoadGraphic(imgSPELL);
	LoadUserShotData(csd~"img\PLUS_shot.txt");
        TAnime1;
        }

        @MainLoop
        {
	imgAngle+=5;
	if(imgAngle>=360){imgAngle=0;}
	if(retune==0&&big<99){big+=2;}
	if(retune==1&&big>1){big-=2;}
	imgBACK+=4;
	if(imgBACK==448){imgBACK=0;}

	if(count>=50&&count%100==0){
        	retune=1;
		}
	if(count>=50&&count%100==50){
        	retune=0;
		}
        if(!OnBomb){
        SetCollisionB(GetX(),GetY(),22);
	SetCollisionA(GetX(),GetY(),22);
        imgALP2=255;
        imgALP1=100;
	SetDamageRate(100,0);
        SetEnemyMarker(true);}
        else {
        imgALP2=75;
        imgALP1=255;
	SetDamageRate(0,0);
	SetEnemyMarker(false);}

	if(count==150){
		PlaySE(shotseB);
		Concentration02(60);
		}
	if(count>=250&&count%250==150){
        	TAnime2;
		move;
        	}
	if(count>=200&&count%250==200){
		PlaySE(shotseA);
			let i=0;
			while(i<72)
			{
			FiraShot(i*5,GetX,GetY);
			i++;
			}
        	}
	if(count>=200&&count%250==205){
		PlaySE(shotseA);
		angleA=rand(GetClipMinX+48,GetClipMaxX-48);
		angleB=rand(GetClipMinY+32,GetClipMinY+128);
			let i=0;
			while(i<72)
			{
			FiraShot(i*5,angleA,angleB);
			i++;
			}
        	}
	if(count>=200&&count%250==210){
		PlaySE(shotseA);
		angleA=rand(GetClipMinX+48,GetClipMaxX-48);
		angleB=rand(GetClipMinY+32,GetClipMinY+128);
			let i=0;
			while(i<72)
			{
			FiraShot(i*5,angleA,angleB);
			i++;
			}
        	}
	if(GetEnemyLife<2000||GetTimer<=40&&count>=200&&count%250==215){
		PlaySE(shotseA);
		angleA=rand(GetClipMinX+48,GetClipMaxX-48);
		angleB=rand(GetClipMinY+32,GetClipMinY+128);
			let i=0;
			while(i<72)
			{
			FiraShot(i*5,angleA,angleB);
			i++;
			}
        	}
	if(GetEnemyLife<1500||GetTimer<=30&&count>=200&&count%250==220){
		PlaySE(shotseA);
		angleA=rand(GetClipMinX+48,GetClipMaxX-48);
		angleB=rand(GetClipMinY+32,GetClipMinY+128);
			let i=0;
			while(i<72)
			{
			FiraShot(i*5,angleA,angleB);
			i++;
			}
        	}
        count++;
        yield;
        }

        @DrawLoop {
		SetRenderState(ALPHA);
		SetTexture(imgEnemy);
		SetGraphicRect(0,0,192,192);
		SetAlpha(imgALP1);
		SetColor(0,255,128);
		SetGraphicScale(0.01*big,1);
		SetGraphicAngle(0,0,imgAngle);
		DrawGraphic(GetX(),GetY());

		SetColor(0,128,255);
		SetGraphicAngle(0,0,-imgAngle);
		DrawGraphic(GetX(),GetY());

		SetTexture(imgBoss);
		SetGraphicScale(1,1);
		SetAlpha(imgALP2);
		SetColor(imgALP2,imgALP2,imgALP2);
		SetGraphicAngle(0,0,0);
		if(int(GetSpeedX())==0)
		{SetGraphicRect(128*AnimationC,128*AnimationB,127+128*AnimationC,127+128*AnimationB);}
		else if(GetSpeedX()>0){SetGraphicRect(256,256+128*AnimationA,383,383+128*AnimationA);}
		else if(GetSpeedX()<0){SetGraphicRect(256,128*AnimationA,383,127+128*AnimationA);}
		DrawGraphic(GetX(),GetY());
	}

	@BackGround
	{
		//背景描画
		SetGraphicRect(0,0,384,448);
		SetTexture(imgSPELL);
		SetRenderState(ALPHA);
		SetColor(255,255,255);
		SetAlpha(155);
		DrawGraphic(cx,cy-448+imgBACK);

		DrawGraphic(cx,cy+imgBACK);
	}
        
        @Finalize
        {
	loop(75)
	{
		angle=rand(0,360);
		CreateItem(ITEM_SCORE,GetX+cos(angle)*rand(35,50),GetY+sin(angle)*rand(35,50));
	}
        } 
	
    	task TAnime1{
			AnimationB=0;
			AnimationC=3;
        		loop(16){yield;}
			AnimationB=1;
        		loop(56){yield;}
			AnimationB=0;
        		loop(8){yield;}
		loop{
			AnimationB=0;
			AnimationC=0;
        		loop(10){yield;}
			AnimationB=1;
        		loop(10){yield;}
			AnimationB=2;
        		loop(10){yield;}
			AnimationB=3;
        		loop(10){yield;}
			}
		}

    	task TAnime2{
			AnimationA=0;
        		loop(10){yield;}
			AnimationA=1;
        		loop(30){yield;}
			AnimationA=0;
        		loop(10){yield;}
		}

task FiraShot(Angle,x,y){
	let obj = Obj_Create(OBJ_SHOT);

	Obj_SetPosition(obj,x,y);
	Obj_SetSpeed(obj,1);
	Obj_SetAngle(obj,Angle);
	ObjShot_SetGraphic(obj,56);
	ObjShot_SetDelay(obj,0);
    
	while(!Obj_BeDeleted(obj)) {
        //右端反射
		if(Obj_GetX(obj)>GetClipMaxX){
			ObjShot_SetGraphic(obj,53);
			Obj_SetAngle(obj,180-Obj_GetAngle(obj));
			Obj_SetSpeed(obj,1.5);
			break;
			}
        //右端反射
		if(Obj_GetX(obj)<GetClipMinX){
			ObjShot_SetGraphic(obj,53);
			Obj_SetAngle(obj,180-Obj_GetAngle(obj));
			Obj_SetSpeed(obj,1.5);
			break;
        }
        //上端反射
		if(Obj_GetY(obj)<GetClipMinY){
			ObjShot_SetGraphic(obj,53);
			Obj_SetAngle(obj,-Obj_GetAngle(obj));
			Obj_SetSpeed(obj,1.5);
			break;
		}
        //下端反射
		if(Obj_GetY(obj)>GetClipMaxY){
			Obj_SetPosition(obj,Obj_GetX(obj),GetClipMinY);
			ObjShot_SetGraphic(obj,54);
			Obj_SetSpeed(obj,2);
			break;
		}
		yield;
		}
	}

task move{
	let wMove = 50;
	moveToPlayer(rand(40,80),rand(-40,40),wMove,GetClipMinX+48,GetClipMinY+32,GetClipMaxX-48,GetClipMinY+128);
	wait(wMove);
	}

function moveToPlayer(xMove,yAdd,frame,left,top,right,bottom){
	let x;
	let y;

	if(GetPlayerX<GetX){//プレイヤーより右に敵がいれば、敵は左に動きます。
		x=GetX-xMove;

		//但し、敵が可動領域の左端よりも左にいくようなら、右に動きます。
			if(x<left){
				x=GetX+xMove;
 			}
		} else {
		//さもなくば、敵は右に動きます。
		x=GetX+xMove;

		// 但し、敵が可動領域の右端よりも右にいくようなら、左に動きます。
		if(right<x){
			x=GetX-xMove;
			}
		}
	y=GetY+yAdd;
	if(y<top) {
		y=top;
	} else if(bottom<y){
		y=bottom;
		}
	SetMovePosition02(x,y,frame);
	}
function wait(w) {
	loop(w){yield;}
	}
}