#Title[毒花「ラフレシヴァ」]
#Text[メディスン・メランコリーのスペルカード]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {
        let count=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
        let csd=GetCurrentScriptDirectory;
        let imgBoss=csd~"img\dot_medicine.png";
        let name="毒花「ラフレシヴァ」";
	let AnimationA=0;
	let AnimationB=0;
	let AnimationC=0;
        let angle=0;
        let angleA=0;
	let shotseB=csd~"se\ShotSE04.wav";

	@Initialize {
		CutIn(YOUMU,name,0,0,0,0,0);

		SetLife(600);
		SetTimer(55);
		SetScore(1750000);
		SetDamageRate(20,20);
		SetInvincibility(150);
		SetMovePosition02(cx,120,60);
		LoadGraphic(imgBoss);
		LoadUserShotData(csd~"img\PLUS_shot.txt");
		TAnime1;
	}

        @MainLoop
        {
	if(count==100){
		SetColor(255,0,255);
		Concentration01(100);
		}
	if(count>=200&&count%50==0){
		PlaySE(shotseB);
		angle=rand_int(0,360);
		let i=0;
		while(i<30){
			SetShotDirectionType(ABSOLUTE);
			let shot01=1;
			CreateShotA(shot01,GetX+cos(angle)*50,GetY+sin(angle)*50,1);
			SetShotDataA(shot01,0,rand(0.5,3),rand(0,360),0,0.01,4,127);
			FireShot(shot01);
			CreateShotA(shot01,GetX+cos(angle)*50,GetY+sin(angle)*50,2);
			SetShotDataA(shot01,0,rand(0.5,3),rand(0,360),0,0.01,4,117);
			FireShot(shot01);
			CreateShotA(shot01,GetX+cos(angle)*50,GetY+sin(angle)*50,0);
			SetShotDataA(shot01,0,rand(0.5,3),rand(0,360),0,0.01,4,217);
			FireShot(shot01);
			i++;
			}
		}
	if(count>=400&&count%50==0){
        	SetShotDirectionType(ABSOLUTE);
		let i=0;
		while(i<20){
        		let shot01=1;
        		CreateShotA(shot01,GetX+cos(angle)*50,GetY+sin(angle)*50,0);
        		SetShotDataA(shot01,0,1,i*18,0,0.01,2,67);
        		FireShot(shot01);
			i++;
			}
		}
	if(count>=800&&count%50==0){
        	SetShotDirectionType(ABSOLUTE);
		let i=0;
		while(i<20){
        		let shot01=1;
        		CreateShotA(shot01,GetX+cos(angle)*50,GetY+sin(angle)*50,0);
        		SetShotDataA(shot01,0,2,i*18,0,0.01,3,67);
        		FireShot(shot01);
			i++;
			}
		}
	if(count>=350&&count%200==0){
        	TAnime2;
		move;
		}
	SetCollisionA(GetX(),GetY(),18);
	SetCollisionB(GetX(),GetY(),18);
        count++;
        yield;
        }

       @DrawLoop {
		SetRenderState(ALPHA);
		SetTexture(imgBoss);
		SetGraphicScale(1,1);
		SetAlpha(255);
		SetColor(255,255,255);
		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());
	}

	@Finalize {
		if(!IsTimeOut){
			loop(50){
				angle=rand(0,360);
				CreateItem(ITEM_SCORE,GetX+cos(angle)*rand(35,45),GetY+sin(angle)*rand(35,45));
			}
		}
		DeleteGraphic(imgBoss);
	}

    	task TAnime1{
		loop{
			AnimationB=0;
			AnimationC=0;
        		loop(10){yield;}
			AnimationB=1;
        		loop(10){yield;}
			AnimationB=2;
        		loop(10){yield;}
			}
		}

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


task move{
	let wMove=80;
	moveToPlayer(rand(50,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;}
	}
}