/*-----重力設定--------------------------*/
GSet(global.G,global.Gdir,0.1,1,0,0.95,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(MotionNum == STAND){
		/* 相手の方を向く */
		if(objPlayerDoll.x > x) Direction = Right;
		else Direction = Left;
		MotionChange(WALK,2,1);
	}
}

switch(DeadKind){
	case CATCH_HEAD : 
		with(objPlayerDoll){
			if(KeyFrmNum>=14){
				if(HEAD.image_alpha){
					if(random(100)< 100+global.PlayerHP) FluidWithScale(HEAD.x+Direction*4,HEAD.y,c_white,1,0,3,0,360,0.5,0.5,HEAD.depth-1000);
				}
			}
			if(KeyFrmNum>=22){
				addDamage(0.75,-1,-1,-1,-1,-1,-1);
				if(random(800)<100+global.PlayerHP) MotionChange(MotionNum,24,1);
			}
			if(MOTIONEND) MotionChange(MotionNum,25,1);
		}
		break;
	case CATCH_BODY : 
		with(objPlayerDoll){
			if(KeyFrmNum>=19){
				if(HEAD.image_alpha){
					if(random(100)< 100+global.PlayerHP) FluidWithScale(HEAD.x+Direction,HEAD.y,c_white,1,0,0,0,360,0.3,0.3,HEAD.depth-1000);
					if(random(100)< 100+global.PlayerHP) FluidWithScale(HEAD.x+Direction,HEAD.y,COLOR_BLOOD,1,0,0,0,360,0.3,0.3,HEAD.depth-1000);
				}
			}
			if(KeyFrmNum>=28){
				addDamage(0.75,-1,-1,-1,-1,-1,-1);
				if(random(800)<100+global.PlayerHP) MotionChange(MotionNum,30,1);
			}
			if(MOTIONEND) MotionChange(MotionNum,32,1);
		}
		break;
	case CATCH_HAND : 
		with(objPlayerDoll){
			if(KeyFrmNum>=8){
				if(HEAD.image_alpha){
					if(random(100)< 5) FluidWithScale(HEAD.x+Direction,HEAD.y,COLOR_BLOOD,1,0,0,0,360,0.3,0.3,HEAD.depth-1000);
				}
			}
			if(KeyFrmNum>=25){
				addDamage(0.75,-1,-1,-1,-1,-1,-1);
				if(random(800)<100+global.PlayerHP) MotionChange(MotionNum,33,1);
			}
			if(MOTIONEND) MotionChange(MotionNum,35,1);
		}
		break;
}

switch(MotionNum){
	case STAND :
		break;
	case WALK : 
		switch(KeyFrmNum){
			case  3 : 
			case  4 : 
			case  5 : 
			case  6 : Move(-15.0*Direction,0); break;
		}
		break;
	case CATCH_HEAD :
		Process[ProcessNum[PARTS_TAIL2]].depth = objPlayerDoll.depth - 1001;
		if(KeyFrmNum<12){
			with(objController){
				global.BindMax = 5+0.1*(100-global.PlayerHP);
				with(CtrlTarget){
					if( other.BindCounter>5+0.1*(100-global.PlayerHP) ){
						other.BindCounter = 0;
						move_outside_afterBind();
						BINDED = 0;
					}
				}
			}
		}
		break;
	case CATCH_BODY :
		DepthSet(objPlayerDoll.depth - 1000);
		if(KeyFrmNum<9){
			Process[ProcessNum[PARTS_TAIL2]].depth = objPlayerDoll.depth + 1002;
			Process[ProcessNum[PARTS_TAIL1]].depth = objPlayerDoll.depth + 1001;
		}
		if(KeyFrmNum<19){
			with(objController){
				with(CtrlTarget){
					global.BindMax = 30+0.1*(100-global.PlayerHP);
					if( other.BindCounter>30+0.1*(100-global.PlayerHP) ){
						other.BindCounter = 0;
						move_outside_afterBind();
						BINDED = 0;
					}
				}
			}
		}
		break;
	case CATCH_HAND:
		if(KeyFrmNum<27) Process[ProcessNum[PARTS_TAIL2]].depth = objPlayerDoll.depth - 1001;
		if(KeyFrmNum<19){
			with(objController){
				with(CtrlTarget){
					global.BindMax = 30+0.1*(100-global.PlayerHP);
					if( other.BindCounter>30+0.1*(100-global.PlayerHP) ){
						other.BindCounter = 0;
						move_outside_afterBind();
						BINDED = 0;
					}
				}
			}
		}
		break;
}

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

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

			/* それなりに近づいたら攻撃 */
			if( !air && 120>playerDistance ) MotionChange(ATTACK,0,5);

			break;
		case WALK : 
			switch(KeyFrmNum){
				case  1 : Move(-4.0*Direction,0); break;
				case  2 : Move(-8.0*Direction,0); break;
				case  6 : if(KEYFRAME) MotionChange(MotionNum,3,3); break;
			}
			/* 近づいたら歩く */
			if( !air && playerDistance>550 || !place_free(x-Direction,y)) MotionChange(STAND,0,5);

			/* それなりに近づいたら攻撃 */
			if( !air && 300>playerDistance ) MotionChange(ATTACK,2,5);
			break;
		case ATTACK :
			switch(KeyFrmNum){
				case 3 : Move((playerDistance/300)*-20.0*Direction,0); break;
				case 4 : Move((playerDistance/300)*-15.0*Direction,0); break;
				case 5 : 
					Move(-10.0*Direction,0);
					if(KEYFRAME){
						sound_play(sndSwing00);
						with( collision_circle( x-96*Direction, y, 80,objPlayerDoll,0,1) ){
							if(!INV && !BINDED){
								switch(irandom(3)){
									case 0 :
										DamageVoiceRandom_Mid();
										if(Direction!=other.Direction)	addDamage(10,FLYING_0, 0, 1, FLYING_0, 0, 2);
										else							addDamage(10,FLYING_1, 0, 1, FLYING_1, 0, 2);
										hspeed = 50*other.Direction*-1;
										vspeed = -6;
										sound_play(sndHit00);
										Quake(15,15);
										break;
									case 1 :
										addDamage(5,-1,-1,-1,-1,-1,-1);
										VoreVoiceRandom_Mid();
										sound_play(sndHit00);
										Quake(15,15);
										Direction = other.Direction*-1;
										with(other){
											startBind( objPlayerDoll,BIND_CATCH,0,other.BINDED_HEBIKO_CATCHHEAD,0,1 );
											MotionChange(CATCH_HEAD,0,1);
										}
										break;
									case 2 :
										addDamage(5,-1,-1,-1,-1,-1,-1);
										DamageVoiceRandom_Mid();
										sound_play(sndHit00);
										Quake(15,15);
										Direction = other.Direction*-1;
										with(other){
											startBind( objPlayerDoll,BIND_CATCH,0,other.BINDED_HEBIKO_CATCHBODY,0,1 );
											MotionChange(CATCH_BODY,0,1);
										}
										break;
									case 3 :
										addDamage(5,-1,-1,-1,-1,-1,-1);
										DamageVoiceRandom_Mid();
										sound_play(sndHit00);
										Quake(15,15);
										Direction = other.Direction*-1;
										with(other){
											startBind( objPlayerDoll,BIND_CATCH,0,other.BINDED_HEBIKO_CATCHHAND,0,1 );
											MotionChange(CATCH_HAND,0,1);
										}
										break;
								}
							}
						}
					}
					break;
				case 6 : Move(-2.0*Direction,0); break;
				case 7 : Move(-1.0*Direction,0); break;
			}
			if(MOTIONEND) MotionChange(STAND,0,1);
			break;

		case CATCH_HEAD : 
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						DamageVoiceRandom_Low();
						with(BindTarget){ MotionChange(STAND_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}
			switch(KeyFrmNum){
				case 0 : Move(-2.0*Direction,0); break;
				case 1 : Move(-2.0*Direction,0); break;
				case 2 : Move(-1.0*Direction,0); break;
				case 3 : Move(-0.5*Direction,0); break;
				case  6 :
					if(KEYFRAME){
						addDamage(5,-1,-1,-1,-1,-1,-1);
						VoreVoiceRandom_Mid(); 
					}
					break;
				case 13 :
					if(KEYFRAME){
						DeadKind = CATCH_HEAD;
						VoreVoiceRandom_Mid();
						SpecialVoice(V_MUSE,0);
						global.PlayerHP = 0;
						addDamage(1,-1,-1,-1,-1,-1,-1);
						Flash(c_red,0.5,0.01);
						sound_play(sndBone00);
						HitStop(5,null);
					}
					break;
			}
			if(MOTIONEND) MotionChange(WAIT_STAND,0,10);
			break;
		case CATCH_BODY :
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						StopVoice(V_MUSE,2);
						DamageVoiceRandom_Low();
						with(BindTarget){ MotionChange(STAND_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}
			switch(KeyFrmNum){
				case 0 : Move(-2.0*Direction,0); break;
				case 1 : Move(-2.0*Direction,0); break;
				case 2 : Move(-1.0*Direction,0); break;
				case 3 : Move(-0.5*Direction,0); break;
				case 4 :
				case 9 :
					if(KEYFRAME){
						addDamage(5,-1,-1,-1,-1,-1,-1);
						DamageVoiceRandom_Mid(); 
						sound_play(sndGishi00);
					}
					break;
				case 13 : if(KEYFRAME) SpecialVoice(V_MUSE,2); break;
				case 14 : case 15 :
					addDamage(0.25,-1,-1,-1,-1,-1,-1);
					if(global.PlayerHP<0){
						MotionChange(MotionNum,17,5);
						with(BindTarget){ MotionChange(MotionNum,17,5); }
					}
					Quake(2,10);
					if(random(100)<2){ sound_play(sndGishi00);	Quake(10,10); Flash(c_red,0.1,0.01);}
					if(random(100)<2){ sound_play(sndBone00);	Quake(10,10); Flash(c_red,0.1,0.01);}
					break;
				case 16 : 
					MotionChange(MotionNum,14,1);
					with(BindTarget){ MotionChange(MotionNum,14,1); }
					break;
				case 19 :
					if(KEYFRAME){
						with(BindTarget){ FluidWithScale(HEAD.x,HEAD.y-8,COLOR_BLOOD,35,5,10,70,40,0.3,0.3,HEAD.depth-1000); }
						Quake(25,25);
						DeadKind = CATCH_BODY;
						StopVoice(V_MUSE,2);
						SpecialVoice(V_MUSE,1);
						Flash(c_red,0.8,0.01);
						sound_play(sndBone00);
						sound_play(sndGusha);
						HitStop(10,null);
						global.PlayerHP = 0;
						addDamage(1,-1,-1,-1,-1,-1,-1);
					}
					break;
			}
			if(MOTIONEND) MotionChange(WAIT_STAND,0,10);
			break;

		case CATCH_HAND :
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						StopVoice(V_MUSE,2);
						DamageVoiceRandom_Low();
						with(BindTarget){ MotionChange(STAND_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}
			switch(KeyFrmNum){
				case 0 : Move(-2.0*Direction,0); break;
				case 1 : Move(-2.0*Direction,0); break;
				case 2 : Move(-1.0*Direction,0); break;
				case 3 : Move(-0.5*Direction,0); break;

				case 8 :
				case 17 :
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : playEnemySound(SOUND_SLASH0); break;
							case 1 : playEnemySound(SOUND_SLASH1); break;
							case 2 : playEnemySound(SOUND_SLASH2); break;
							case 3 : playEnemySound(SOUND_SLASH3); break;
						}
						addDamage(5,-1,-1,-1,-1,-1,-1);
						if(random(100) < 50+global.PlayerHP) DamageVoiceRandom_Mid(); 
						sound_play(sndHit00);Quake(5,5);Flash(c_red,0.2,0.01);
						with(BindTarget){ FluidWithScale(x,y,COLOR_BLOOD,35,5,10,180,90,0.5,0.5,HEAD.depth-1000); }
						PartsDamage(objExtParts,		-10,0);
						PartsBloodDamage(objExtParts,	-10,0);
					}
					break;
				case 13 :
				case 21 :
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : playEnemySound(SOUND_SLASH0); break;
							case 1 : playEnemySound(SOUND_SLASH1); break;
							case 2 : playEnemySound(SOUND_SLASH2); break;
							case 3 : playEnemySound(SOUND_SLASH3); break;
						}
						addDamage(5,-1,-1,-1,-1,-1,-1);
						if(random(100) < 50+global.PlayerHP) DamageVoiceRandom_Mid(); 
						sound_play(sndHit00);Quake(5,5);Flash(c_red,0.2,0.01);
						with(BindTarget){ FluidWithScale(x,y,COLOR_BLOOD,35,5,10,90,90,0.5,0.5,HEAD.depth-1000); }
						PartsDamage(objExtParts,		-10,0);
						PartsBloodDamage(objExtParts,	-10,0);
					}
					break;
				case 10: 
				case 14 : 
				case 18 : 
				case 22 : 
					if(global.PlayerHP<-100){
						MotionChange(MotionNum,24,5);
						with(BindTarget){ MotionChange(MotionNum,24,5); }
					}
					else{
						var tmp;
						tmp = random(6);
						switch(irandom(3)){
							case 0 : 
								if(KeyFrmNum == 10){MotionChange(MotionNum,12,5+tmp); with(BindTarget){ MotionChange(MotionNum,12,5+tmp); } break;}
								MotionChange(MotionNum, 7,5+tmp); with(BindTarget){ MotionChange(MotionNum, 7,5+tmp); } break;
							case 1 : 
								if(KeyFrmNum == 14){MotionChange(MotionNum,16,5+tmp); with(BindTarget){ MotionChange(MotionNum,16,5+tmp); } break;}
								MotionChange(MotionNum,12,5+tmp); with(BindTarget){ MotionChange(MotionNum,12,5+tmp); } break;
							case 2 : 
								if(KeyFrmNum == 18){MotionChange(MotionNum,20,5+tmp); with(BindTarget){ MotionChange(MotionNum,20,5+tmp); } break;}
								MotionChange(MotionNum,16,5+tmp); with(BindTarget){ MotionChange(MotionNum,16,5+tmp); } break;
							case 3 : 
								if(KeyFrmNum == 22){MotionChange(MotionNum, 7,5+tmp); with(BindTarget){ MotionChange(MotionNum, 7,5+tmp); } break;}
								MotionChange(MotionNum,20,5+tmp); with(BindTarget){ MotionChange(MotionNum,20,5+tmp); } break;
						}
					}
					break;

				case 25 :
					if(KEYFRAME){
						DeadKind = CATCH_HAND;
						global.PlayerHP = 0;
						addDamage(1,-1,-1,-1,-1,-1,-1);
					}
					break;
			}
			if(MOTIONEND) MotionChange(WAIT_STAND,0,5);
			break;

		case DEAD_MOTION :
			if(KeyFrmNum==1 && KEYFRAME){
				sound_play(sndHit02);
				Fluid(x,y,make_color_rgb(163,73,164),35,5,20,0,180,0.5);
				Quake(5,5);
			}
			if(MOTIONEND){
				effect_create_above(ef_explosion,x,y,1,make_color_rgb(163,73,164));
				Flash(c_white,0.1,0.05);
				Fluid(x,y-64,make_color_rgb(163,73,164),35,5,20,0,180,0.5);
				Quake(5,5);
				sound_play(sndFluid);
				instance_destroy();
			}
			break;
	}
}