#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 bngle=0;
	let cngle=0;
	let Enemycount=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
	let AnimationA=0;
	let AnimationB=0;
	let AnimationC=0;
	let shotseA=csd~"se\ShotSE02.wav";

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

        SetLife(800);
        SetTimer(60);
        SetScore(2500000);
        SetDamageRate(20,20);
        SetInvincibility(200);
	SetMovePosition02(cx,150,60);
	LoadGraphic(imgBoss);
	LoadUserShotData(csd~"img\PLUS_shot.txt");
        TAnime1;
        }

        @MainLoop
        {
	if(count>=100&&count%5==0){
		PlaySE(shotseA);
        	SetShotDirectionType(ABSOLUTE);
		let i=0;
		while(i<2){
			let shot01=1;
			CreateShotA(shot01,rand(100,348),-5,0);
			SetShotDataA(shot01,0,0.5,90+angle,bngle,cngle,rand(2,3),65);
			SetShotDataA(shot01,50,NULL,NULL,0,0,0,65);
			FireShot(shot01);
        		i++;
        		}
		}
	if(count>=100&&count%10==0){
		let shot01=1;
		CreateShotA(shot01,32,rand(10,470),50);
		SetShotDataA(shot01,0,0.5,rand(-90,90),0,0,0,58);
		let j=299;
		while(j<300){
			let id2=2;
			CreateShotA(id2,0,0,20);
			SetShotDataA(id2,0,0,rand_int(0,120)*3,0,0,0,58);
			SetShotKillTime(id2,0);
			AddShot(j,shot01,id2,300);
			j++;
		}
		SetShotKillTime(shot01,300);
		FireShot(shot01);
		CreateShotA(shot01,416,rand(10,470),50);
		SetShotDataA(shot01,0,0.5,rand(90,270),0,0,0,58);
		let j=299;
		while(j<300){
			let id2=2;
			CreateShotA(id2,0,0,20);
			SetShotDataA(id2,0,0,rand_int(0,120)*3,0,0,0,58);
			SetShotKillTime(id2,0);
			AddShot(j,shot01,id2,300);
			j++;
		}
		SetShotKillTime(shot01,300);
		FireShot(shot01);
		}
	if(count>=90&&count%10==0&&Enemycount==0){
		angle=angle+rand(2,4);
		bngle=rand(0.5,0.1);
		cngle=rand(0.1,0.5);}
		else if(count>=190&&count%10==0&&Enemycount==1){
		angle=angle-rand(2,4);
		bngle=rand(-0.5,-0.1);
		cngle=rand(0.1,0.5);}
	if(angle>=45&&Enemycount==0){
		Enemycount=1;
		}
	if(angle<=-45&&Enemycount==1){
		Enemycount=0;
		}
	if(count>=100&&GetX>GetPlayerX){
		SetPlayerX(GetPlayerX+0.5);
		} else if(count>=100&&GetX<GetPlayerX){
		SetPlayerX(GetPlayerX-0.5);
		}
	if(count>=300&&count%300==150){
        	TAnime2;
		move;
		}
        SetCollisionB(GetX(),GetY(),18);
	SetCollisionA(GetX(),GetY(),18);
        count++;
        yield;
        }

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

        @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{
			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(30){yield;}
			AnimationA=0;
        		loop(10){yield;}
		}

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