#Title[「お迎え体験版」]
#Text[小野塚小町のスペルカード]
#BackGround[User]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
        let count=0;
        let name="「お迎え体験版」";
        let csd=GetCurrentScriptDirectory;
        let imgBoss=csd~"img\dot_komachi.png";
	let angle=0;
	let r=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
	let AnimationA=0;
	let AnimationB=0;
	let AnimationC=0;
	let shotseA=csd~"se\ShotSE04.wav";
	let shotseB=csd~"se\ShotSE02.wav";
	let shotseD = csd~"se\BOM.wav";
	let Flag=false;

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

        SetLife(4000);
        SetTimer(60);
        SetScore(2500000);
        SetDamageRate(100,50);
        SetInvincibility(200);
	SetMovePosition02(cx,150,60);
	LoadGraphic(imgBoss);
	LoadUserShotData(csd~"img\PLUS_shot.txt");
	SetEffectForZeroLife(30,0,0);
        TAnime1;
        }

        @MainLoop
        {
	if(count==150){
		Concentration01(60);
		}
	if(count>=150&&count<250){
		if(GetX>GetPlayerX){SetPlayerX(GetPlayerX+0.5);}
		if(GetX<GetPlayerX){SetPlayerX(GetPlayerX-0.5);}
		if(GetY>GetPlayerY){SetPlayerY(GetPlayerY+0.5);}
		if(GetY<GetPlayerY){SetPlayerY(GetPlayerY-0.5);}
		}
	if(count>=150&&count<250&&count%2==0){
        	SetShotDirectionType(ABSOLUTE);
		angle=GetAngleToPlayer;
		PlaySE(shotseA);
		let i=0;
		while(i<10){
			let shotA=0;
			CreateShotA(shotA,GetX+cos(i*36+angle+18)*15,GetY+sin(i*36+angle+18)*15,5);
			SetShotDataA(shotA,0,12,i*36+angle+18,0,0,0,96);
			SetShotDataA(shotA,252-count,0,NULL,0,0,0,96);
			SetShotDataA(shotA,rand_int(260,275)-count,0,i*22.5+angle-rand(160,200),0,0,0,104);
			SetShotDataA(shotA,300-count,0.5,NULL,0,0.01,1,104);
			FireShot(shotA);
			i++;
			}
		}
	if(count==300){
        	TAnime2;
		move;
		}
	if(count>=300&&count<360&&count%5==0){
        	SetShotDirectionType(PLAYER);
		PlaySE(shotseB);
		let i=0;
		while(i<20){
			let shotA=0;
			CreateShotA(shotA,GetX,GetY,5);
			SetShotDataA(shotA,0,0.5,i*18,0,0,0,67);
			FireShot(shotA);
			CreateShotA(shotA,GetX,GetY,5);
			SetShotDataA(shotA,0,1.5,i*18,0,0,0,67);
			FireShot(shotA);
			i++;
			}
		}
	if(count==400){
        	count=149;
		}
        if(GetEnemyLife<1&&!Flag){
		Flag=true;
		Explosion01(GetX(),GetY(),1/60*255,0.3,60);
		PlaySE(shotseD);
        	}
        SetCollisionB(GetX(),GetY(),18);
	SetCollisionA(GetX(),GetY(),18);
        count++;
        yield;
        }

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

    	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;}
			}
		}

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

task move{
	let wMove = 60;
	moveToPlayer(rand(60,120),rand(-50,50),wMove,GetClipMinX+98,GetClipMinY+132,GetClipMaxX-98,GetClipMinY+158);
	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;}
	}
}