#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 imgBoss2=csd~"img\dot_yousei3.png";
	let angle=0;
	let Lock=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);
	LoadGraphic(imgBoss2);
	LoadUserShotData(csd~"img\PLUS_shot.txt");
        TAnime1;
        }

        @MainLoop
        {
	if(count==150){
		Concentration01(60);
		}
	if(count==200){
		Lock=GetAngleToPlayer;
		let i=0;
		while(i<6){
			CreateEnemyFromScript("DOLL01",GetX,GetY,0,Lock+i*60,1);
			i++;
			}
		}
	if(count>=300&&count%300==0){
        	TAnime2;
		move;
		}
	if(count>=300&&count%50==0){
		PlaySE(shotseA);
        	SetShotDirectionType(PLAYER);
		let i=0;
		while(i<20){
			let shotA=0;
			CreateShotA(shotA,GetX(),GetY(),5);
			SetShotDataA(shotA,0,1,i*18,0,0,0,238);
			FireShot(shotA);
			i=i+1;
			}
		}
	if(count>200&&GetEnemyNum<7){
		Lock=GetAngleToPlayer;
		let i=0;
		while(i<6){
			CreateEnemyFromScript("DOLL01",GetX,GetY,0,Lock+i*60,1);
			i++;
			}
		}
        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);
		DeleteGraphic(imgBoss2);
        }

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

script_enemy DOLL01 {
        let count=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
	let Animation=0;
        let Motion=0;
	let imgAngle=0;
	let imgAlpha=255;
        let csd=GetCurrentScriptDirectory;
        let imgBoss2=csd~"img\dot_yousei3.png";
        let angle=GetAngle;
	let deathse=csd~"se\DEATH.wav";
	let shotseA=csd~"se\ShotSE02.wav";
	let Flag=false;

    @Initialize {
        SetLife(150);
        SetDamageRateEx(100,100,100,5);
        SetInvincibility(50);
	SetMovePosition01(GetEnemyX+cos(angle)*1000,GetEnemyY+sin(angle)*1000,1);
        TAnime;
	}

	@MainLoop {
	if(GetLife<=100&&Flag==false){
			SetMovePosition01(GetX,GetY+100,1);
			PlaySE(deathse);
			Motion=1;
			Flag=true;
			}
	if(Flag==true){
		imgAngle+=15;
		imgAlpha-=5;
		if(imgAlpha<=0){AddLife(-1000);}
		} else {
		SetCollisionA(GetX(),GetY(),10);
		SetCollisionB(GetX(),GetY(),10);
		}
	if(GetLife>100&&count>=100&&count%25==0){
        	SetShotDirectionType(ABSOLUTE);
		PlaySE(shotseA);
		let shotA=0;
		CreateShotA(shotA,GetX,GetY,0);
		SetShotDataA(shotA,0,1.75,angle+60,0,-0.1,1,66);
		FireShot(shotA);
		CreateShotA(shotA,GetX,GetY,0);
		SetShotDataA(shotA,0,1.75,angle-60,0,-0.1,1,61);
		FireShot(shotA);
		CreateShotA(shotA,GetX,GetY,0);
		SetShotDataA(shotA,0,1.5,angle-180,0,-0.1,1,67);
		FireShot(shotA);
		}

	if(GetLife>100&&count%2==0){
        	SetShotDirectionType(ABSOLUTE);
		let shotA=0;
		CreateShotA(shotA,GetX+rand(-5,5),GetY+rand(-5,5),20);
		SetShotDataA(shotA,0,0,0,0,0,0,67);
		SetShotKillTime(shotA,0);
		FireShot(shotA);
		}
        if(GetX>448)
        	{
		SetX(0);
		SetMovePosition01(GetEnemyX+cos(angle)*1000,GetEnemyY+sin(angle)*1000,1);
        	}
        if(GetX<0)
        	{
		SetX(448);
		SetMovePosition01(GetEnemyX+cos(angle)*1000,GetEnemyY+sin(angle)*1000,1);
        	}
        if(GetY>480)
        	{
		SetY(0);
		SetMovePosition01(GetEnemyX+cos(angle)*1000,GetEnemyY+sin(angle)*1000,1);
        	}
        if(GetY<0)
        	{
		SetY(480);
		SetMovePosition01(GetEnemyX+cos(angle)*1000,GetEnemyY+sin(angle)*1000,1);
        	}
	count++;
	yield;
	}

        @DrawLoop {
		SetTexture(imgBoss2);
		SetGraphicAngle(0,0,imgAngle);
		SetAlpha(imgAlpha);
		SetGraphicRect(Animation*32+Motion*96,0,31+Animation*32+Motion*96,31);
		DrawGraphic(GetX(),GetY());
	}

        @Finalize
        {
        } 

    	task TAnime{
		loop{
			Animation=0;
        		loop(10){yield;}
			Animation=1;
        		loop(10){yield;}
			Animation=2;
        		loop(10){yield;}
			}
		}
}