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

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){
	if(INV){ /* 無敵のとき */
		playEnemySound(SOUND_NODAMAGE);
		switch(MotionNum){
			case STAND : /* 無敵で待機中 */
				var self_id;
				self_id = id; /* 自分のインスタンスIDを保存 */
				/* 攻撃してきた奴が範囲内にいて */
				if( !air && collision_line(x,y,x-500*Direction,y,objPlayerDoll,0,1)){
					with(objPlayerDoll){
						with(objNana_Gun){
							if(Bullet<=1){ /* 弾が一発しかないなら */
								/* 突撃 */
								with(self_id){MotionChange(WALK,0,1);}
							}
						}
					}
				}
				break;
		}
	}
	else playEnemySound(SOUND_DAMAGE);
}

INV = 0;
WEAK = 0;
if(!ChangeMotion){
	switch(MotionNum){
		case STAND :
			INV = 2;
			/* 相手の方を向く */
			if(objPlayerDoll.x > x){
				if(Direction){ MotionChange(RETURN,0,2); break; }
				/* Direction = Right; */
			}else{
				if(!Direction){ MotionChange(RETURN,0,2); break; }
				/* Direction = Left; */
			}

			/* 近づいたら歩く */
			if( collision_line(x,y,x-100*Direction,y,objPlayerDoll,0,1) ){ MotionChange(ATTACK,0,1); break; }
			else if( collision_line(x,y,x-250*Direction,y,objPlayerDoll,0,1) ){ MotionChange(WALK,0,1); break; }
			else if( isPlayerDown() && collision_line(x,y,x-600*Direction,y,objPlayerDoll,0,1) ){ MotionChange(WALK,0,1); break; }
			break;
		case WALK :
			INV = 2;
			switch(KeyFrmNum){
				case  1 : if(place_free(x-24*Direction,y)) x -= 24.0*Direction; break;
				case  2 : if(place_free(x-12*Direction,y)) x -= 12.0*Direction; break;
				case  3 : if(place_free(x-6*Direction,y)) x -=  6.0*Direction; break;
				case  4 : if(place_free(x-24*Direction,y)) x -= 24.0*Direction; break;
				case  5 : if(place_free(x-12*Direction,y)) x -= 12.0*Direction; break;
				case  6 : if(place_free(x-6*Direction,y)) x -=  6.0*Direction; break;
			}

			if( collision_line(x,y,x-100*Direction,y,objPlayerDoll,0,1) ){ MotionChange(ATTACK,0,1); break; }

			/* 壁にぶつかったり離れたりしたら止まる */
			if( !place_free(x-24*Direction,y) && !collision_line(x,y,x-600*Direction,y,objPlayerDoll,0,1) ){ MotionChange(STAND,0,5); break; }
			break;
		case RETURN :
			INV = 2;
			switch(KeyFrmNum){
				case  1 : if(place_free(x- 8.0*Direction,y)) x -=  8.0*Direction; break;
				case  2 : if(place_free(x-16.0*Direction,y)) x -= 16.0*Direction; break;
				case  3 :
						if(KEYFRAME) Direction = -Direction;
						if(place_free(x-32.0*Direction,y)) x -= 32.0*Direction;
						break;
				case  4 : if(place_free(x-10.0*Direction,y)) x += 10.0*Direction; break;
				case  5 : if(place_free(x- 8.0*Direction,y)) x +=  8.0*Direction; break;
				case  6 : if(place_free(x- 4.0*Direction,y)) x +=  4.0*Direction; break;
				case  7 : if(place_free(x- 2.0*Direction,y)) x +=  2.0*Direction; break;
				case  8 : if(place_free(x- 1.0*Direction,y)) x +=  1.0*Direction; break;
			}
			if(MOTIONEND){ MotionChange(STAND,0,5); break; }
			break;
		case ATTACK :
			/* 基本的には無敵 */
			INV = 2;
			switch(KeyFrmNum){
				case  1 :
					WEAK = 1;
				case  2 :
				case  3 :
					/* この間だけ攻撃が通る */
					if(Direction){ /* 左向き */
						if(135<playerDirection && playerDirection<225) INV = 0;
						else INV = 2;
					}else{ /* 右向き */
						if(45<playerDirection && playerDirection<315) INV = 2;
						else INV = 0;
					}
					if(place_free(x-16*Direction,y)) x -=  1.0*Direction; break;
				case  4 : 
					/* この間だけ攻撃が通る */
					if(Direction){ /* 左向き */
						if(135<playerDirection && playerDirection<225) INV = 0;
						else INV = 2;
					}else{ /* 右向き */
						if(45<playerDirection && playerDirection<315) INV = 2;
						else INV = 0;
					}
					if(place_free(x-16*Direction,y)) x -= 16.0*Direction;
					if(KEYFRAME){
						sound_play(sndHit00);
						col = collision_circle( x-64*Direction,y, 80,objPlayerDoll,0,1)
						with( col ){
							if(!INV){
								DamageVoiceRandom_High();
								with(other){ playEnemySound(SOUND_BITE); }
								Fluid(x,y,COLOR_BLOOD,15,2,20, 80,20,0.5);
								Fluid(x,y,COLOR_BLOOD,15,2,20,260,20,0.5);
								Flash(c_red,0.2,0.05);
								Quake(15,15);

								if(BINDED){
									addDamage(80, -1, -1, -1, -1, -1, -1);
								}else{
									addDamage(80,DAMAGE_0, 0, 1, DOWN_1, 0, 2);

									if(random(100)<50){
										Fluid(x,y,c_red,15,5,20,0,180,0.5);
										if(Direction != other.Direction){
											with(other){
												startBind( col,BIND_CATCH_FLOOR,0,other.BINDED_WANIO_CATCH_FLOOR,0,1 );
												MotionChange(CATCH_FLOOR,0,1);
												Quake(15,15);
											}
										}else{
											addDamage(80,DAMAGE_0, 0, 1, DOWN_1, 0, 2);
											with(other){
												startBind( col,BIND_CATCH_FLOOR,0,other.BINDED_WANIO_CATCH_CUT,0,1 );
												MotionChange(CATCH_CUT,0,1);
												Quake(15,15);
											}
										}
									}
								}
							}
						}
					}
					break;
			}
			if(MOTIONEND){ MotionChange(STAND,0,5); break; }
			break;
		case CATCH_CUT :
			/* キャラより手前に */
			depth = objPlayerDoll.depth - 1000;
			for(i=0;i<PARTS_MAX;i+=1) with(Draw[i]){ depth = other.depth - other.i*3; }
			Process[ProcessNum[SPRITE_HEAD02]].depth	= objPlayerDoll.depth + 1000;

			with(BindTarget){
				if(0<KeyFrmNum && KeyFrmNum<22) Fluid(FOOT2_ABOVE.x,FOOT2_ABOVE.y,COLOR_BLOOD,2+random(5),0,5,0,360,0.5);
				FOOT2_ABOVE.image_alpha = 0;
				FOOT3_ABOVE.image_alpha = 0;
				with(objCommon_Foot_Above){ image_alpha = 0; }
			}

			switch(KeyFrmNum){
				case  1 : 
					if(KEYFRAME){
						addDamage(150,-1, -1, -1, -1, -1, -1);
						Quake(15,15);
						if(BindTarget) Fluid(BindTarget.FOOT2_ABOVE.x,BindTarget.FOOT2_ABOVE.y,COLOR_BLOOD,50,0,20,0,360,0.5);
						Flash(c_red,1.0,0.1);
					}
					break;
				case  6 : 
				case  7 : 
				case  8 : 
				case 12 : 
				case 13 : 
				case 17 : 
				case 18 : 
				case 19 : 
					if(BindTarget) Fluid(BindTarget.BODY_TOP.x,BindTarget.BODY_TOP.y,COLOR_BLOOD,3,0,5,0,360,0.5);
					break;
				case  5 : 
				case 11 : 
				case 16 : 
				case 22 : 
					if(KEYFRAME){
						DamageVoiceRandom_Mid();
						addDamage(20,-1, -1, -1, -1, -1, -1);
						Quake(15,15);
						if(BindTarget) Fluid(BindTarget.BODY_TOP.x,BindTarget.BODY_TOP.y,COLOR_BLOOD,20,0,20,0,360,0.5);
						Flash(c_red,0.5,0.1);
						playEnemySound(SOUND_BITE);
					}
					break;
			}
			if(MOTIONEND){
				startBind( BindTarget,BIND_CATCH_FLOOR,0,BindTarget.BINDED_WANIO_CATCH_CUT,49,1 );
				MotionChange(CATCH_CUT,25,1);
			}
			break;
		case CATCH_FLOOR :
			/* キャラより手前に */
			depth = objPlayerDoll.depth - 1000;
			for(i=0;i<PARTS_MAX;i+=1) with(Draw[i]){ depth = other.depth - other.i*3; }
			Process[ProcessNum[SPRITE_HEAD02]].depth	= objPlayerDoll.depth + 1000;

			switch(KeyFrmNum){
				case  4 : 
				case  6 : 
				case  8 : 
					if(BindTarget) Fluid(BindTarget.FOOT2_ABOVE.x,BindTarget.FOOT2_ABOVE.y,COLOR_BLOOD,3,0,5,0,360,0.5);
					break;
				case  10 : 
					if(KEYFRAME){
						DamageVoiceRandom_Low();
						addDamage(500,-1, -1, -1, -1, -1, -1);
						Quake(15,15);
						Flash(c_red,0.5,0.1);
						playEnemySound(SOUND_CLASH);
					}
					break;
			}
			if(MOTIONEND){
				if(isGameOver()){
					startBind( BindTarget,BIND_CATCH_FLOOR,0,BindTarget.BINDED_WANIO_CATCH_FLOOR_FINISH,0,1 );
					MotionChange(CATCH_FLOOR_FINISH,0,1);
				}
			}
			break;
		case CATCH_FLOOR_FINISH :
			/* キャラより手前に */
			depth = objPlayerDoll.depth - 1000;
			for(i=0;i<PARTS_MAX;i+=1) with(Draw[i]){ depth = other.depth - other.i*3; }
			Process[ProcessNum[SPRITE_HEAD02]].depth	= objPlayerDoll.depth + 1000;

			switch(KeyFrmNum){
				case  18 : 
					if(KEYFRAME){
						Macro_CutHand1_Above();
					}
				case  2 : 
				case  8 : 
				case  12 : 
				case  23 : 
					if(KEYFRAME){
						Quake(15,15);
						Flash(c_red,1.0,0.1);
						playEnemySound(SOUND_BITE);
						if(BindTarget) Fluid(BindTarget.x,BindTarget.y,COLOR_BLOOD,30,0,25,0,360,0.5);
					}
					break;
			}
			if(MOTIONEND){
				startBind( BindTarget,BIND_CATCH_FLOOR,0,BindTarget.BINDED_WANIO_CATCH_FLOOR_FINISH,19,1 );
				MotionChange(CATCH_FLOOR_FINISH,28,1);
			}
			break;
		case DEAD_MOTION :
			if(KeyFrmNum==1 && KEYFRAME){
				sound_play(sndHit02);
				Fluid(x,y,COLOR_BLOOD,35,5,20,0,180,0.5);
				Quake(5,5);
			}
			if(MOTIONEND){
				effect_create_above(ef_explosion,x,y,1,COLOR_BLOOD);
				Flash(c_white,0.1,0.05);
				Fluid(x,y,COLOR_BLOOD,5,5,20,0,180,0.5);
				Quake(5,5);
				sound_play(sndFluid);
				instance_destroy();
			}
			break;
	}
}