/*-----重力設定--------------------------*/
if( air ){
	gravity = global.G;
	gravity_direction = global.Gdir;
}else{
	gravity = 0;
	gravity_direction = 0;
}

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 : if(place_free(x-3*Direction,y)) x -= 0.2*Direction; break;
				case 1 : if(place_free(x-3*Direction,y)) x -= 1.5*Direction; break;
				case 2 : if(place_free(x-3*Direction,y)) x -= 0.5*Direction; break;
				case 3 : if(KEYFRAME) playEnemySound(SOUND_STEP);
						 if(place_free(x-3*Direction,y)) x -= 2.5*Direction; break;
				case 6 : if(place_free(x-3*Direction,y)) x -= 1.5*Direction; break;
				case 7 : if(place_free(x-3*Direction,y)) x -= 0.5*Direction; break;
				case 8 : if(KEYFRAME) playEnemySound(SOUND_STEP);
						 if(place_free(x-3*Direction,y)) x -= 1.0*Direction; break;
				case 9 : if(place_free(x-3*Direction,y)) x -= 0.5*Direction; 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){
			DamageVoiceRandom_Low();
			if(BINDED) addDamage(10, -1, -1, -1, -1, -1, -1);
			else{
				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;
	}
}