/*-----重力設定--------------------------*/
GSet(global.G,global.Gdir,1,1, 0,0,1,1);

with(objEnemyEdit){ if(Edit_Play) exit; }

/*-----キャラクタとの情報の取得---------------*/
playerDistance = point_distance(x,y,objPlayerDoll.x,objPlayerDoll.y);
playerDirection = point_direction(x,y,objPlayerDoll.x,objPlayerDoll.y);

/*-----モーション毎の処理--------------------*/
if(RECV_DAMAGE) playEnemySound(SOUND_DAMAGE);

if(!ChangeMotion){
	switch(MotionNum){
		case STAND :
			/* 相手の方を向く */
			if(objPlayerDoll.x > x) Direction = Right;
			else Direction = Left;

			/* 近づいたら歩く */
			if( !air && 100<playerDistance && playerDistance<350){ MotionChange(WALK,0,5); break; }

			/* 場合によっては撃つ */
			if( !air && 200>playerDistance && !instance_exists(Bullet) ){ 
				if( 10 < playerDirection && playerDirection < 170){
					MotionChange(SHOT_UE,0,3);
				}else{
					MotionChange(SHOT_YOKO,0,5);
				}
				break;
			}
			break;
		case WALK :
			switch(KeyFrmNum){
				case 0 : Move(-0.2*Direction,0); break;
				case 1 : Move(-1.5*Direction,0); break;
				case 2 : Move(-0.5*Direction,0); break;
				case 3 : if(KEYFRAME) playEnemySound(SOUND_STEP);
						 Move(-2.5*Direction,0); break;
				case 6 : Move(-1.5*Direction,0); break;
				case 7 : Move(-0.5*Direction,0); break;
				case 8 : if(KEYFRAME) playEnemySound(SOUND_STEP);
						 Move(-1.0*Direction,0); break;
				case 9 : Move(-0.5*Direction,0); break;
			}
			/* 近づきすぎたり離れたりしたら止まる */
			if( 100>playerDistance || playerDistance>350){ MotionChange(STAND,0,5); break; }

			/* 場合によっては撃つ */
			if( !air && 200>playerDistance && !instance_exists(Bullet) ){ 
				if( 10 < playerDirection && playerDirection < 170){
					MotionChange(SHOT_UE,0,3);
				}else{
					MotionChange(SHOT_YOKO,0,5);
				}
				break;
			}
			break;
		case SHOT_YOKO :
			switch(KeyFrmNum){
				case 4 : 
					if(KEYFRAME){
						Quake(5,5);
						sound_play(sndBullet2);

						/* 弾の作成 */
						Bullet = createBullet(Process[PARTS_CANON].x+20*Direction*-1,Process[PARTS_CANON].y);
						Bullet.speed		= 40;
						Bullet.direction	= Process[PARTS_CANON].image_angle+180;
						Bullet.hspeed		*= Direction;
						Bullet.Picture		= SPRITE_BULLET;

						/* 薬莢の作成 */
						new = instance_create(x,y,objBulletCase);
						new.hspeed = (5+random(5))*Direction;
						new.vspeed = (-10-random(5));
						new.image_index = 1;
					}
					break;
			}
			if(MOTIONEND){ MotionChange(STAND,0,5); break; }
			break;
		case SHOT_UE :
			switch(KeyFrmNum){
				case 4 : 
					if(KEYFRAME){
						Quake(5,5);
						sound_play(sndBullet2);

						/* 弾の作成 */
						Bullet = createBullet(Process[PARTS_CANON].x+20*Direction*-1,Process[PARTS_CANON].y);
						Bullet.speed		= 40;
						Bullet.direction	= Process[PARTS_CANON].image_angle+180;
						Bullet.hspeed		*= Direction;
						Bullet.Picture		= SPRITE_BULLET;

						/* 薬莢の作成 */
						new = instance_create(x,y,objBulletCase);
						new.hspeed = (5+random(5))*Direction;
						new.vspeed = (-10-random(5));
						new.image_index = 1;
					}
					break;
			}
			if(MOTIONEND){ MotionChange(STAND,0,5); break; }
			break;
		case DEAD_MOTION :
			switch(KeyFrmNum){
				case 1 : 
					if(KEYFRAME) effect_create_above(ef_smokeup,x,y,0,c_black);
					break;
			}
			if(MOTIONEND){
				instance_create(x,y,objExplosionSmall);
				Flash(c_white,0.1,0.05);

				Quake(5,5);
				sound_play(sndBomb);
				instance_destroy();
			}
			break;
	}
}

/*-----飛び道具の処理--------------------*/
with(Bullet){
	image_angle = direction;
	if( checkBlock(x,y) ){
		var Num,VarianceX,VarianceY,Gravity,AngleMin,AngleRange,SpeedMin,SpeedRange,TimeMin,TimeRange;
		Num = 10+random(20);
		Gravity		= 1;
		VarianceX	= 8;
		VarianceY	= 8;
		AngleMin	= 180+direction-10;
		AngleRange	= 20;
		SpeedMin	= 0;
		SpeedRange	= 5;
		TimeMin		= 5;
		TimeRange	= 5;
		Spark(Num,VarianceX,VarianceY,Gravity,AngleMin,AngleRange,SpeedMin,SpeedRange,TimeMin,TimeRange);
		destroyBullet();
	}
	with(checkPlayer(x,y)){
		if(!INV && !BINDED){
			DamageVoiceRandom_Low();
			addDamage(10,DAMAGE_0, 0, 1, DOWN_1, 0, 2); 
			hspeed = other.hspeed/2;
			if(other.hspeed>0) 	Fluid(x,y,COLOR_BLOOD,10,2,10,90, 180,0.5);
			else				Fluid(x,y,COLOR_BLOOD,10,2,10,90,-180,0.5);
			sound_play(sndBulletHit);
			Quake(5,5);
			with(other){ destroyBullet(); }
		}
	}
	if(Time){
		new = instance_create(x,y,objLine);
		new.xpos = x-hspeed;
		new.ypos = y-vspeed;
	}
}