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

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

Process[ProcessNum[SPRITE_SAW]].PartPicture = Blood;

/*-----キャラクタとの情報の取得---------------*/
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_DAMAGE1);
	else	 playEnemySound(SOUND_DAMAGE2);
}

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

			/* 近づいたら歩く */
			if( !air && 100<playerDistance && playerDistance<600){
				if(random(100)<15) MotionChange(WALK,0,2);
				if( (objPlayerDoll.x > x && !Direction) || (objPlayerDoll.x < x && Direction) ){
					if(random(100)<5){
						HitFlag = false;
						MotionChange(ATTACK,0,2);
					}
				}
			}
			break;
		case WALK :
			if(place_free(x-5*Direction,y)) x -= 5.0*Direction;
			/* 近づきすぎたり離れたりしたら止まる */

			if( playerDistance>650){ MotionChange(STAND,0,2); break; }
			else if( (objPlayerDoll.x > x && !Direction) || (objPlayerDoll.x < x && Direction) ){
				if(random(100)<(500-playerDistance)/10){
					HitFlag = 0;
					MotionChange(ATTACK,0,2);
				}
			}
			if(!place_free(x-16*Direction,y)){ MotionChange(STAND,0,2); break; }
			break;
		case ATTACK : 
			if(random(100)<40) INV = 2;
			if(!place_free(x-16*Direction,y)){ MotionChange(STAND,0,2); break; }
			if(objPlayerDoll.x > x &&  Direction && playerDistance>50) { MotionChange(WALK,0,2); break; }
			if(objPlayerDoll.x < x && !Direction && playerDistance>50) { MotionChange(WALK,0,2); break; }
			switch(KeyFrmNum){
				case 1 : if(place_free(x-16*Direction,y)) x -= 1.0*Direction; if(KEYFRAME) playEnemySound(SOUND_SAW1); break;
				case 2 : if(place_free(x-16*Direction,y)) x -= 2.0*Direction; break;
				case 3 : if(place_free(x-16*Direction,y)) x -= 3.0*Direction; break;
				case 4 : if(place_free(x-16*Direction,y)) x -= 4.0*Direction; if(KEYFRAME) playEnemySound(SOUND_SAW2); break;
				case 5 : if(place_free(x-16*Direction,y)) x -= 8.0*Direction; break;
				case 6 : if(place_free(x-16*Direction,y)) x -=16.0*Direction; break;
				default :
					col = collision_circle( x-48*Direction,y, 32,objPlayerDoll,0,1)
					with( col ){
						if(!INV && !BINDED){
							DamageVoiceRandom_High();
							global.voiceInterval = 10;
							/* おなかのパーツに大ダメージ！ */
							with(objCommon_Body){
								with(DrawTarget){
								PartsDamage(other.id,-random(5),0);
								PartsBloodDamage(other.id,-random(5),0);
								}
							}
							sound_play(sndHit00)
							Fluid(x,y,COLOR_BLOOD,15,5,10,45,45,0.3);
							Fluid(x,y,COLOR_BLOOD, 5,5, 5,90,45,0.3);
							Flash(c_red,0.2,0.05);
							Quake(15,15);
							if(other.HitFlag){
								other.HitFlag += 1;
								addDamage(1,DAMAGE_0, 0, 1, DOWN_0, 0, 1);
							}else{
								other.HitFlag = 1;
								addDamage(20,DAMAGE_0, 0, 1, DOWN_0, 0, 1);
								if(isGameOver()){
									DamageVoiceRandom_High();
									SpecialVoice(V_MUSE,0);
									if(Direction == other.Direction){
										with(other){
											startBind( col,BIND_ROOT,0,other.BINDED_SAW_DEATHCUT2,0,1 );
											MotionChange(DEATHCUT2,0,1);
											Quake(15,15);
										}
									}else{
										with(other){
											Blood = 1;
											startBind( col,BIND_ROOT,0,other.BINDED_SAW_DEATHCUT,0,1 );
											MotionChange(DEATHCUT,0,1);
											Quake(15,15);
										}
									}
									break;
								}
							}
						}
					}

					if(col){
						with( col ){ if(!BINDED && place_free(x-4.0*other.Direction,y)) x -=4.0*other.Direction; }
						if(place_free(x-4*Direction,y)) x -=8.0*Direction;
					}else{
						if(place_free(x-16*Direction,y)) x -=16.0*Direction;
					}

					if(Direction) SparkPosPlus(x,y+16,5,4,4,0.9,45,45,3,15,4,8);
					else		  SparkPosPlus(x,y+16,5,4,4,0.9,90,45,3,15,4,8);
					break;
			}
			if(MOTIONEND){ MotionChange(ATTACK,7,1); 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;
		case DEATHCUT :
			HitFlag += 1;
			addDamage(5, -1, -1, -1, -1, -1, -1);
			if(place_free(x-16*Direction,y)) x -= 5.0*Direction;
			sound_play(sndHit00)
			Fluid(x-Direction*16,y,COLOR_BLOOD, 5,15,10,80,10,0.3);
			Fluid(x-Direction*48,y,COLOR_BLOOD, 5, 5, 5,90,45,0.3);
			Flash(c_red,0.2,0.05);
			Quake(15,15);
			if(HitFlag > 30){
				startBind( BindTarget,BIND_ROOT,0,BindTarget.BINDED_SAW_FINISH,0,1 );
				MotionChange(FINISH,0,1);
				Quake(15,15);
				break;
			}
			break;
		case FINISH :
			switch(KeyFrmNum){
				case  1 : if(place_free(x-16*Direction,y)) x -= 8.0*Direction; break;
				case  2 : if(place_free(x-16*Direction,y)) x -= 6.0*Direction; break;
				case  3 : if(place_free(x-16*Direction,y)) x -= 4.0*Direction; break;
				case  4 : if(place_free(x-16*Direction,y)) x -= 2.0*Direction; break;
				case  5 : break;
				case  6 : if(place_free(x+3*Direction,y)) x += 3.0*Direction; break;
				case  7 : if(place_free(x+3*Direction,y)) x += 2.5*Direction; break;
				case  8 : if(place_free(x+3*Direction,y)) x += 2.0*Direction; break;
				case  9 : if(place_free(x+3*Direction,y)) x += 1.5*Direction; break;
				case 10 : if(place_free(x+3*Direction,y)) x += 0.5*Direction; break;
			}
			switch(KeyFrmNum){
				case 1 : 
				case 2 : 
				case 3 : 
				case 4 : 
					Fluid(x-Direction*48,y,COLOR_BLOOD,3,0,5,45,45,0.3);
					Fluid(x-Direction*48,y,COLOR_BLOOD,1,0,5,90,45,0.3);
					break;
				case 5 : 
					Fluid(x-Direction*48,y,COLOR_BLOOD,1,0,5,45,45,0.3);
					Fluid(x-Direction*48,y,COLOR_BLOOD,1,0,5,90,45,0.3);
					if(random(100)<10) Fluid(BindTarget.HEAD.x,BindTarget.HEAD.y,COLOR_BLOOD,3,0,5,0,0,0.5);
					
					if(random(100)<5){
						with(BindTarget){
							MotionChange(BINDED_SAW_FINISH,11,1);
						}
					}
					with(BindTarget){if(KeyFrmNum==14) MotionChange(BINDED_SAW_FINISH,13,1);}
					break;
				case 6 : 
					if(KEYFRAME){
						with(BindTarget){
							MotionChange(BINDED_SAW_FINISH,14,1);
						}
					}
					break;
				case 8 : 
					endBind();
					break;
			}
			if(MOTIONEND){
				MotionChange(STAND,0,2);
			}
			break;
		/* ========================================================= */
		case DEATHCUT2 :
			HitFlag += 1;
			addDamage(5, -1, -1, -1, -1, -1, -1);
			if(place_free(x-16*Direction,y)) x -= 5.0*Direction;
			sound_play(sndHit00)
			Fluid(x-Direction*16,y,COLOR_BLOOD, 5,15,10,80,10,0.3);
			Fluid(x-Direction*48,y,COLOR_BLOOD, 5, 5, 5,90,45,0.3);
			Flash(c_red,0.2,0.05);
			Quake(15,15);
			if(HitFlag > 30){
				/* 頭ないのに暴れられても怖い */
				check = false;
				with(objPlayerDoll){ other.check = (HEAD.image_alpha > 0); }
				if(check){
					startBind( BindTarget,BIND_ROOT,0,BindTarget.BINDED_SAW_FINISH2,0,1 );
					MotionChange(FINISH2,0,1);
				}else{
					startBind( BindTarget,BIND_ROOT,0,BindTarget.DOWN_1,0,1 );
					MotionChange(WALK,0,1);
					endBind();
				}
				Quake(15,15);
				break;
			}
			break;
		case FINISH2 :
			switch(KeyFrmNum){
				case  1 : if(place_free(x-16*Direction,y)) x -= 8.0*Direction; break;
				case  2 : if(place_free(x-16*Direction,y)) x -= 6.0*Direction; break;
				case  3 : if(place_free(x-16*Direction,y)) x -= 4.0*Direction; break;
				case  4 : if(place_free(x-16*Direction,y)) x -= 2.0*Direction; break;
				case  5 : break;
				case  6 : if(KEYFRAME) playEnemySound(SOUND_SAW1); break;
				case  7 : if(KEYFRAME) playEnemySound(SOUND_SAW2); break;
				case  8 : break;
				case  9 : if(KEYFRAME) CryingVoiceRandom_High();break;
				case 10 : break;
				default : if(place_free(x-3*Direction,y)) x -= 1.0*Direction; break;
			}
			switch(KeyFrmNum){
				case 1 : 
				case 2 : 
				case 3 : 
				case 4 : 
					Fluid(x-Direction*48,y,COLOR_BLOOD,3,0,5,45,45,0.3);
					Fluid(x-Direction*48,y,COLOR_BLOOD,1,0,5,90,45,0.3);
					break;
				case 5 : 
					Fluid(x-Direction*48,y+32,COLOR_BLOOD,1,0,5,45,45,0.3);
					Fluid(x-Direction*48,y+32,COLOR_BLOOD,1,0,5,90,45,0.3);
					if(random(100)<10) Fluid(BindTarget.HEAD.x,BindTarget.HEAD.y,COLOR_BLOOD,3,0,5,0,0,0.5);
					break;
				case 6 : 
				case 7 : 
				case 8 : 
					break;
				case 9 : 
					if(KEYFRAME){
						Blood = 1;
						playEnemySound(SOUND_FINISH);
						endBind();
					}
					break;
				default :
					/* おなかのパーツに大ダメージ！ */
					with(objCommon_Body){
						with(DrawTarget){
							PartsDamage(other.id,-random(5),0);
							PartsBloodDamage(other.id,-random(5),0);
						}
					}
					sound_play(sndHit00)
					Fluid(x,y,COLOR_BLOOD,15,5,10,45,45,0.3);
					Fluid(x,y,COLOR_BLOOD, 5,5, 5,90,45,0.3);
					Flash(c_red,0.2,0.05);
					Quake(15,15);
					break;
			}
			if(!place_free(x-16*Direction,y)){ MotionChange(STAND,0,2); break; }
			if(objPlayerDoll.x > x &&  Direction && playerDistance>50) { MotionChange(WALK,0,2); break; }
			if(objPlayerDoll.x < x && !Direction && playerDistance>50) { MotionChange(WALK,0,2); break; }
			if(MOTIONEND){
				MotionChange(FINISH2,11,1);
			}
			break;
	}
}