#Title[人形「大江戸爆薬からくり人形」]
#Text[アリスのスペルカード]
#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
        let count=0;
        let name="人形「大江戸爆薬からくり人形」";
        let csd=GetCurrentScriptDirectory;
        let imgBoss=csd~"img\dot_EXalice.png";
        let imgBoss2=csd~"img\dot_yousei.png";
        let angle=0;
	let AnimationA=0;
	let AnimationB=0;
	let AnimationC=0;
	let imgAngle=0;

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

        SetLife(600);
        SetTimer(40);
        SetScore(1500000);
        SetDamageRate(20,20);
        SetInvincibility(200);
        SetMovePosition02(GetCenterX(),150,50);
	LoadGraphic(imgBoss);
	LoadGraphic(imgBoss2);
	LoadUserShotData(csd~"img\PLUS_shot.txt");
        TAnime1;
    }

        @MainLoop
        {
	if(count==150){
		Concentration01(60);
		}
	if(count>=200&&count%100==0){
		CreateEnemyFromScript("DOLLA",GetX,GetY,0,0,0);
		}
	if(count>=400&&count%200==0){
                TAnime2;
		move;
		}
	if(count>=400&&count%400==60){
        	TAnime3(20);
        	}
	if(count>=400&&count%400==260){
        	TAnime3(-20);
        	}
        SetCollisionA(GetX(),GetY(),18);
        SetCollisionB(GetX(),GetY(),18);
        count++;
        yield;

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

        @DrawLoop {
		SetRenderState(ALPHA);
		SetTexture(imgBoss);
		SetGraphicScale(1,1);
		SetAlpha(255);
		SetColor(255,255,255);
		SetGraphicAngle(0,0,imgAngle);
		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 {SetGraphicRect(256,128*AnimationA,383,127+128*AnimationA);}
		DrawGraphic(GetX(),GetY());
		}

    	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 TAnime3(AP){
        		loop(36){imgAngle+=AP;
			yield;}
		}

task move {
    let wMove = 60;

    moveToPlayer(rand(60,120),rand(-80,80),wMove,GetClipMinX+48,GetClipMinY+64,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 DOLLA{
        let count=0;
        let cx=GetCenterX();
        let cy=GetCenterY();
	let Animation=0;
        let csd=GetCurrentScriptDirectory;
        let imgBoss2=csd~"img\dot_yousei.png";
	let angle=0;
	let imgAngle=0;
	let imgAlpha=255;
	let deathse=csd~"se\DEATH.wav";
	let Flag=false;
	let shotseA=csd~"se\BOM.wav";
	let shotseB=csd~"se\ShotSE02.wav";

    @Initialize {
        SetLife(255);
        SetDamageRateEx(100,100,20,20);
        SetInvincibility(50);
	SetMovePosition02(rand(100,348),rand(100,200),50);
        TAnime;
	}

	@MainLoop {
        if(GetX<20||GetX>428||GetY>460||GetY<20&&Flag==false){
		Flag=true;
		Bomb;
		}
	if(GetLife<=100&&Flag==false){
		Flag=true;
		PlaySE(deathse);
		Bomb;
		}
	if(Flag==true){
		imgAngle+=15;
		imgAlpha-=5;
		if(imgAlpha<=0){VanishEnemy();}
		} else {
		SetCollisionA(GetX(),GetY(),10);
		SetCollisionB(GetX(),GetY(),10);
		}
	if(count==50&&Flag==false){
        	SetMovePosition01(GetPlayerX*10000-GetX*10000+GetX,GetPlayerY*10000-GetY*10000+GetY,2);
		}
	if(count>=50&&count%2==0&&Flag==false){
		PlaySE(shotseB);
        	Hibana;
		}
        count++;
	yield;
	}

        @DrawLoop {
		SetTexture(imgBoss2);
		SetGraphicAngle(0,0,imgAngle);
		SetColor(255,GetLife,GetLife);
		SetAlpha(imgAlpha);
		if(int(GetSpeedX())==0)
		{SetGraphicRect(Animation*32,0,31+Animation*32,31)}
		else if(GetSpeedX()>0){SetGraphicRect(128+Animation*32,32,159+Animation*32,63);}
		else if(GetSpeedX()<0){SetGraphicRect(Animation*32,32,31+Animation*32,63);}
		DrawGraphic(GetX(),GetY());
	}

        @Finalize
        {
        } 

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

	task Hibana{
		let id=0;
		SetShotDirectionType(ABSOLUTE);
		CreateShotA(id,GetX,GetY,0);
		SetShotDataA(id,0,rand(0.5,2),rand_int(0,180)*2,0,0,0,122);
		let j=19;
		while(j<20){
			let id2=1;
			CreateShotA(id2,0,0,20);
			SetShotDataA(id2,0,0,rand_int(0,120)*3,0,0,0,1);
			SetShotKillTime(id2,0);
			AddShot(j,id,id2,20);
			j++;
		}
		SetShotKillTime(id,20);
		FireShot(id);
		yield;
	}

    	task Bomb{
		SetMovePosition01(GetX,GetY,1);
		TShotA;
		}

    task TShotA {
        	shotBomA;
		Explosion01(GetX(),GetY(),1/60*255,0.3,60);
		PlaySE(shotseA);
        	loop(10){yield;}
        	shotBomB;
        	loop(10){yield;}
        	shotBomC;
        	loop(10){yield;}
        	shotBomD;
	}
	sub shotBomA {
        	let i=0;
        	while(i<10){
			let id=0;
			CreateShotA(id,GetX,GetY,0);
			SetShotDataA(id,0,2,i*36,0,0,2,31);
			SetShotKillTime(id,30);
			FireShot(id);
        		i++;
			}
		yield;
		}
	sub shotBomB {
        	let j=0;
        	while(j<10)
        	{
        	let i=0;
        	while(i<15)
        	{
		let id=0;
        	SetShotDirectionType(ABSOLUTE);
		CreateShotA(id,GetX+cos(angle+36*j)*40,GetY+sin(angle+36*j)*40,0);
		SetShotDataA(id,0,2,i*24,0,0,0,21);
		SetShotKillTime(id,30);
		FireShot(id);
        	i++;
		}
        	j++;
		}
	yield;
    }
    sub shotBomC {
        	let j=0;
        	while(j<15)
        	{
        	let i=0;
        	while(i<20)
        	{
		let id=0;
        	SetShotDirectionType(ABSOLUTE);
		CreateShotA(id,GetX+cos(angle+24*j)*60,GetY+sin(angle+24*j)*60,0);
		SetShotDataA(id,0,rand(2,2.5),i*18,0,0,0,41);
		SetShotKillTime(id,30);
		FireShot(id);
        	i++;
		}
        	j++;
		}
	yield;
    }
    sub shotBomD {
        	let i=0;
        	while(i<18)
        	{
		let id=0;
        	SetShotDirectionType(PLAYER);
		CreateShotA(id,GetX,GetY,5);
		SetShotDataA(id,0,1,i*20,0,0,1,61);
		FireShot(id);
        	i++;
		}
	yield;
    }
}