/*-----重力設定--------------------------*/
gravity = 0;
gravity_direction = 0;

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

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

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

switch(MotionNum){
	case INJECTION:
	case CATCH :
	case CATCH_TAMAGO :
		DepthSet(objPlayerDoll.depth + 1000);
		Process[PARTS_RARMA11].depth = objPlayerDoll.depth - 1008;
		Process[PARTS_RARMA21].depth = objPlayerDoll.depth - 1009;
		Process[PARTS_RARMA31].depth = objPlayerDoll.depth - 1010;
		Process[PARTS_RARMA12].depth = objPlayerDoll.depth - 1005;
		Process[PARTS_RARMA22].depth = objPlayerDoll.depth - 1006;
		Process[PARTS_RARMA32].depth = objPlayerDoll.depth - 1007;
		Process[PARTS_RARMB1 ].depth = objPlayerDoll.depth - 1002;
		Process[PARTS_RARMB2 ].depth = objPlayerDoll.depth - 1003;
		Process[PARTS_RARMB3 ].depth = objPlayerDoll.depth - 1004;
		break;
}

if(!ChangeMotion){
	switch(MotionNum){
		case STAND :
			HabatakiAngle = 45-10*(speed/10);
			if(objPlayerDoll.y-120 < y) motion_add(90,0.1);
			if(abs(objPlayerDoll.x-x)<200){
				if(objPlayerDoll.x-x) motion_add(180,1);
				else				  motion_add(  0,1);
			}
			speed *= 0.9;

			/* 相手の方を向く */
			if(objPlayerDoll.x > x) Direction = Right;
			else Direction = Left;

			/* 近づいたら動く */
			if(playerDistance<400){ MotionChange(MOVE,0,5); break; }

			/* 近づいたら動く */
			if(objPlayerDoll.y>y+120 && abs(objPlayerDoll.x-x)>150 && random(100)<5 && playerDistance<450){ MotionChange(NEEDLE,0,5); break; }

			break;
		case MOVE :
			HabatakiAngle = 45-10*(speed/10);
			if(playerDistance<200) motion_add(playerDirection-20+40*random(1),-1);
			else				   motion_add(playerDirection-20+40*random(1), 1);
			if(objPlayerDoll.y-120 < y) motion_add(90,1);
			if(abs(objPlayerDoll.x-x)<200){
				if(objPlayerDoll.x-x) motion_add(180,1);
				else				  motion_add(  0,1);
			}
			if(speed>10) speed = 10;

			/* 相手の方を向く */
			if(objPlayerDoll.x > x) Direction = Right;
			else Direction = Left;

			/* 近づきすぎたり離れたりしたら止まる */
			if( 100>playerDistance || playerDistance>400){ MotionChange(STAND,0,5); break; }

			if(objPlayerDoll.y>y+100 && abs(objPlayerDoll.x-x)>100 && random(100)<5 && playerDistance<400){ MotionChange(NEEDLE,0,1); break; }

			break;
		case NEEDLE:
			switch(KeyFrmNum){
				case  1 : 
					HitFlag = 0;
					/* 相手の方を向く */
					if(objPlayerDoll.x > x) Direction = Right;
					else Direction = Left;
					break;
				case  3 : 
					if(KEYFRAME){
						motion_set(playerDirection,30);
						if(speed>40) speed = 30;
					}
					break;
				case  4 : 
				case  5 : 
					if(!HitFlag){
						with( collision_circle( Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,32,objPlayerDoll,0,1) ){
							if(!INV && !BINDED){
								with(other){
									if(objPlayerDoll.Direction == Direction) DirectionTemp =  1;
									else									 DirectionTemp = -1;
									HitFlag = 1;
								}
								if(global.PlayerST<global.PlayerSTMAX-1){
									other.HitFlag = 1;
									if(!INV && !BINDED){
										sound_play(sndGishi00);
										Quake(15,15);
										VoreVoiceRandom_Mid();
										with(other){
											startBind( objPlayerDoll,0,BIND_CATCH,other.BINDED_HATTY_CATCH,0,1 );
											MotionChange(CATCH,0,1);
										}
									}
								}else{
									if(random(100)<30){
										sound_play(sndGishi00);
										Quake(15,15);
										addDamage(0,DAMAGE_0, 0, 2, DAMAGE_0, 0, 2);
										with(other){ MotionChange(INJECTION,0,2); }
										DamageVoiceRandom_Low();
									}else{
										if(Direction!=other.Direction)	addSTDamage(5,40,DAMAGE_0, 0, 2, DOWN_1, 0, 2);
										else							addSTDamage(5,40,DAMAGE_0, 0, 2, DOWN_1, 0, 2);
										hspeed = -10*other.Direction*-1;
										sound_play(sndHit02);
										Quake(15,15);
										DamageVoiceRandom_Mid();
									}
								}
							}
						}
					}
					break;
			}
			if(MOTIONEND) MotionChange(STAND,0,5);
			break;
		case INJECTION :
			switch(KeyFrmNum){
				case  1 : case  2 : case  3 :
				case  4 : case  5 : case  6 :
				case  7 : case  8 : case  9 :
					speed = 0;
					x = objPlayerDoll.x;
					y = objPlayerDoll.y;
					Direction = objPlayerDoll.Direction*DirectionTemp;
					break;
			}

			switch(KeyFrmNum){
				case  6 : 
					if(KEYFRAME){
						sound_play(sndHit02);
						playEnemySound(SOUND_INJECTION);
					}
					break;
				case  7 : 
					if(KEYFRAME){
						with( collision_circle( Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,32,objPlayerDoll,0,1) ){
							if(!INV && !BINDED){
								Flash(make_color_rgb(163,73,164),1.5,0.05);
								with(other){ HitFlag = 1; Fluid(Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,make_color_rgb(163,73,164),35,0,10,90,180*-Direction,0.5);}
								if(Direction!=other.Direction) addSTDamage(5,100,DOWN_1, 0, 2, DOWN_1, 0, 2);
								else						   addSTDamage(5,100,DOWN_1, 0, 2, DOWN_1, 0, 2);
								hspeed = -10*other.Direction*-1;
								Quake(15,15);

								if(global.PlayerHP>0) DamageVoiceRandom_Mid();
							}
						}
					}
					break;
				case 10 :
					hspeed = Direction*30;
					break;
			}
			if(MOTIONEND) MotionChange(STAND,0,5);
			break;
		case CATCH :
			speed *= 0.95;
			switch(KeyFrmNum){
				case  1 : vspeed = -2; break;
				case  4 : if(KEYFRAME) sound_play(sndGishi00); break;
			}
			if(MOTIONEND){
				addSTDamage(0.5,1,-1,-1,-1,-1,-1,-1);
				if(global.PlayerST<70){
					startBind( objPlayerDoll,0,BIND_CATCH,BindTarget.BINDED_HATTY_TAMAGO,0,1 );
					MotionChange(CATCH_TAMAGO,0,1);
					break;
				}
				with(BindTarget){ MotionChange(BINDED_HATTY_CATCH,8,1); }
				MotionChange(CATCH,6,1);
			}
			break;
		case CATCH_TAMAGO :
			global.PlayerST -= 0.02;
			if(BindTarget){
				if(!BindTarget.BINDED){
					MotionChange(STAND,0,5);
					with(BindTarget){ MotionChange(FLYING_1,0,2); }
					endBind();
					break;
				}
			}

			speed *= 0.95;
			switch(KeyFrmNum){
				case  3 : 
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 0,2); } break;
							case 1 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 5,2); } break;
							case 2 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,10,2); } break;
							case 3 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,15,2); } break;
						}
						DamageVoiceRandom_Mid();
						Flash(make_color_rgb(163,73,164),0.5,0.05);
						sound_play(sndHit02);
						addSTDamage(3,10,-1,-1,-1,-1,-1,-1);
						Quake(15,3);
					}
					break;
				case  5 : 
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 0,2); } break;
							case 1 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 5,2); } break;
							case 2 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,10,2); } break;
							case 3 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,15,2); } break;
						}
						DamageVoiceRandom_Low();
						Fluid(Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,make_color_rgb(163,73,164),15,0,10,90,180*-Direction,0.5);
					}
					break;
				case  7 : 
					if(global.PlayerST>0){ MotionChange(CATCH_TAMAGO,2,1+random(20)); }
					else{
						MotionChange(CATCH_TAMAGO,8,30);
						global.GameOver = true;
					}
					break;
				case  9 : 
					if(KEYFRAME){
						Quake(15,3);
						DamageVoiceRandom_Low();
						sound_play(sndHit02);
					}
					break;
				case  12 : 
				case  17 : 
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 0,2); } break;
							case 1 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 5,2); } break;
							case 2 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,10,2); } break;
							case 3 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,15,2); } break;
						}
						DamageVoiceRandom_High();
						Quake(15,15);
						playEnemySound(SOUND_INJECTION);
						if(KEYFRAME){
							addSTDamage(0.05,-5,-1,-1,-1,-1,-1,-1);
							Flash(make_color_rgb(155,155,100),0.5,0.05);
							Fluid(Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,make_color_rgb(155,155,100),15,0,10,90,180*-Direction,0.5);
						}
					}
					break;
				case  23 : 
					if(KEYFRAME){
						switch(irandom(3)){
							case 0 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 0,2); } break;
							case 1 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO, 5,2); } break;
							case 2 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,10,2); } break;
							case 3 : with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,15,2); } break;
						}
						sound_play(sndHit02);
					}
					break;
				case  25 : 
					if(KEYFRAME){
						with(BindTarget){ MotionChange(BINDED_HATTY_TAMAGO,20,2);}
						Tamago= 1;
						playEnemySound(SOUND_INJECTION);
						CryingVoiceRandom_High();
						Quake(15,15);
						if(KEYFRAME){
							addSTDamage(0.05,-15,-1,-1,-1,-1,-1,-1);
							Flash(make_color_rgb(155,155,100),1.5,0.05);
							Fluid(Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,make_color_rgb(155,155,100),15,0,10,90,180*-Direction,0.5);
						}
					}
					break;
				case  34 : 
					if(KEYFRAME){
						Quake(15,5);
						if(KEYFRAME){
							sound_play(sndHit02);
							DamageVoiceRandom_Low();
							Fluid(Process[PARTS_NEEDLE3].x,Process[PARTS_NEEDLE3].y,make_color_rgb(155,155,100),15,0,10,90,180*-Direction,0.5);
						}
					}
					break;
			}
			
			if(MOTIONEND){
				switch(irandom(0)){
					case 0: with(BindTarget){ MotionChange(FLYING_1,0,1); }
					case 1: with(BindTarget){ MotionChange(FLYING_0,0,1); }
				}
				MotionChange(STAND_WAIT,0,1);
				endBind();
			}
			break;
		case STAND_WAIT:
			if(Tamago){
				global.PlayerST -= 0.05;
				with(objPlayerDoll){
					BINDED = 1;
					if(MotionNum==FINISHED_HATTY_TAMAGOMOGAKI){
						switch(KeyFrmNum){
							case  2 :if(KEYFRAME){ FearVoiceRandom_Mid();Flash(COLOR_BLOOD,1.0,0.05);Quake(5,15);} break; 
							case  7 :if(KEYFRAME) DamageVoiceRandom_High(); break;
							case 15 :if(KEYFRAME) SpecialVoice(V_MUSE,irandom(2)); break;
							case 30 :if(KEYFRAME){ StopAllVoice();SpecialVoice(V_GYA,3); } break;
							case 40 :
								if(KEYFRAME){
									global.PlayerHP -= 200;
									Quake(35,5);
									Flash(COLOR_BLOOD,2.0,0.05);
									with( objPlayerDoll ){
										PartsDamage(objCommon_Body,-100,0);
										PartsBloodDamage(objCommon_Body,-100,0);
										PartsBloodDamage(objCommon_Head,-100,0);
									}
									sound_play(sndGusha);
									Fluid(x,y,COLOR_BLOOD,50,5,20,70,40,1.0);
								}
								break;
						}
						if(MOTIONEND) MotionChange(FINISHED_HATTY_TAMAGOMOGAKI,48,1);
					}else if(global.PlayerST<0) MotionChange(FINISHED_HATTY_TAMAGOMOGAKI,0,1);
				}
			}
			break;
		case DEAD_MOTION :
			if(KeyFrmNum==1 && KEYFRAME){
				sound_play(sndHit02);
				Fluid(x,y,c_olive,35,5,20,0,180,0.5);
				Quake(5,5);
			}
			if(MOTIONEND){
				effect_create_above(ef_explosion,x,y,1,c_olive);
				Flash(c_white,0.1,0.05);
				Fluid(x,y,c_olive,5,5,20,0,180,0.5);
				Quake(5,5);
				sound_play(sndFluid);
				instance_destroy();
			}
			break;
	}
}