/*-----重力設定--------------------------*/
GSet(0,0,0.95,0.95, 0,0,0.95,0.95);

with(objEnemyEdit){ if(Edit_Play) exit; }
/*-----キャラクタとの情報の取得---------------*/
TARGET = objPlayerDoll;
if(room == Stage5_BoatRoom2){
	TARGET = objBoat;
	with(TARGET){
		if( Tenpuku ) other.TARGET = objToView01;
	}
}

playerDistanceX = abs(x - TARGET.x);
playerDistance = point_distance(x,y,TARGET.x,TARGET.y);
playerDirection = point_direction(x,y,TARGET.x,TARGET.y);

/*-----モーション毎の処理--------------------*/
if(RECV_DAMAGE){
	if(INV){
		MotionChange(GUARD,irandom(2)*5,1);
		with(PartsParam(PARTS_STRING[1,strLen-2])){
	        var Num,VarianceX,VarianceY,Gravity,AngleMin,AngleRange,SpeedMin,SpeedRange,TimeMin,TimeRange;
	        Num = 10+random(20);
	        VarianceX   = 8;                VarianceY = 8;
	        Gravity     = 1;
	        AngleMin    = random(360); AngleRange = 40;
	        SpeedMin    = 0;                SpeedRange = 10;
	        TimeMin     = 5;                TimeRange = 5;
	        Spark(Num,VarianceX,VarianceY,Gravity,AngleMin,AngleRange,SpeedMin,SpeedRange,TimeMin,TimeRange);
		}
	}
}

INV = 0;
switch(MotionNum){
	case GUARD :
	case STAND :
		if(Direction != TARGET.Direction){
			if( playerDistance > 200){
				INV= 1;
			}
		}
		break;
	case CATCH_MOJA:
	case CATCH_MOJA_START:
	case CATCH_MAKI:
	case CATCH_MAKI_ELEC:
		with(objController){
			global.BindMax = 50+0.1*(100-global.PlayerHP);
			with(CtrlTarget){
				if( other.BindCounter>50+0.1*(100-global.PlayerHP) ){
					other.BindCounter = 0;
					move_outside_afterBind();
					BINDED = 0;
				}
			}
		}
		break;
}

DepthSet(TARGET.depth + 1000);
if(!ChangeMotion){
	switch(MotionNum){
		case GUARD :
			switch(KeyFrmNum){
				case 4:
				case 9:
				case 14:
					MotionChange(STAND,0,1);
					break;
			}
		case STAND :
			if(-256<SoutaiPosX && SoutaiPosX<256) SoutaiPosX += -1+random(2);
			if(-256<SoutaiPosY && SoutaiPosY<256) SoutaiPosY += -1+random(2);
			if(place_free(TARGET.x - 256*TARGET.Direction,TARGET.y - 256)){
				tmp_dis = point_distance( x,y,TARGET.x - SoutaiPosX*TARGET.Direction,TARGET.y - SoutaiPosY);
				tmp_dir = point_direction(x,y,TARGET.x - SoutaiPosX*TARGET.Direction,TARGET.y - SoutaiPosY);
			}else{
				tmp_dis = point_distance( x,y,TARGET.x + SoutaiPosX*TARGET.Direction,TARGET.y - SoutaiPosY);
				tmp_dir = point_direction(x,y,TARGET.x + SoutaiPosX*TARGET.Direction,TARGET.y - SoutaiPosY);
			}
			if(room == Stage5_BoatRoom2){
				if(TARGET.x > 10000){
					with(objBoat){ 
						if(!Tenpuku){
							with(other){ MotionChange(SHOCKWAVE,2,2);} 
						}
					}
				}
				motion_add(tmp_dir, min( 3, (tmp_dis/  2.0)) );
			}else{
				 motion_add(tmp_dir, min( 3, (tmp_dis/300.0)) );

				if( random(100) < 1 && !instance_exists(objBlackHole)){ MotionChange(SHOT,0,2); break; }
				if( random(100) < 1 && playerDistanceX < 300 ){ MotionChange(SHOCKWAVE,0,2); break; }
				if( random(100) < 6 && searchPlayer(50,220,300,5) ){ MotionChange(CATCH,0,2); break; }
				if( random(100) < 1 && global.BOSSHPMAX*0.6 > HP && SpaceCount<0 ){ MotionChange(SHOCKWAVE,0,2); break; }

				if(random(100) < 2 && SpaceCount>0){
					if(place_free(TARGET.x - 128*TARGET.Direction,TARGET.y - 128)){
						x = TARGET.x - 128*TARGET.Direction;
						y = TARGET.y - 128;
						instance_create(x,y,objShock);
						effect_create_above(ef_ring,x,y,2,c_white);
					}
				}
			}

			/* 相手の方を向く */
			if(TARGET.x > x){
				if(Direction != Right) MotionChange(TURN,0,2);
			}else{
				if(Direction != Left ) MotionChange(TURN,0,2);
			}

			if(MOTIONEND){
				MotionChange(STAND,0,1);
			}
			break;
		case SHOCKWAVE:
			if(room == Stage5_BoatRoom2){
				if(place_free(TARGET.x - 256*TARGET.Direction,TARGET.y - 256)){
					tmp_dis = point_distance( x,y,TARGET.x - 256*TARGET.Direction,TARGET.y - 256);
					tmp_dir = point_direction(x,y,TARGET.x - 256*TARGET.Direction,TARGET.y - 256);
				}else{
					tmp_dis = point_distance( x,y,TARGET.x + 256*TARGET.Direction,TARGET.y - 256);
					tmp_dir = point_direction(x,y,TARGET.x + 256*TARGET.Direction,TARGET.y - 256);
				}
				motion_add(tmp_dir, min( 3, (tmp_dis/  2.0)) );
			}

			switch(KeyFrmNum){
				case 4:
					if(global.BOSSHPMAX*0.6 > HP && SpaceCount<0){
						SpaceCount = 600;
						Flash_Rect_notAdd(c_black, 1, 0.01);
						new = instance_create(x,y,objUnyoBack);
						new.sprite_index = global.EnemySprite[index,SPRITE_BACK];
						tile_layer_hide(10010);
						tile_layer_hide(-9999);
						tile_layer_hide(-10000);
						tile_layer_hide(-10001);
						tile_layer_hide(-11000);
						instance_activate_object( objWaterBase );
						with( objWaterBase ){ visible = false; }
						instance_activate_object( objCamera );
						with( objCamera ){ visible = false; }
						global.G = 0.3;
					}else{
						if(KEYFRAME){
							Flash_Rect_notAdd(c_black,0.5,0.1);
						}
					}
					break;
				case 5:
					if(KEYFRAME){
						playEnemySound(SOUND_SHOCKWAVE);
						Quake(15,15);
						instance_create(x,y,objShock);
						if(room == Stage5_BoatRoom2){
							instance_create(x,y,objAddFire);
							with(objBoat){
								vspeed = -15; Tenpuku = true;
								with(objYukiBoatAccessory){ instance_destroy() };
								with(Ryoko){ hspeed = other.hspeed*2; vspeed = -24; x += 64; MotionChange( FLYING, 0, 2 ); }
								with(Yuki){  hspeed = other.hspeed*2; vspeed = -30; x -= 128; MotionChange( FLYING, 0, 2 ); }
								with(Nana){  hspeed = other.hspeed*2; vspeed = -20; MotionChange( FLYING, 0, 2 ); }
							}
							break;
						}
						with( collision_circle( x,y, 256,TARGET,0,1) ){
							if(!INV && !BINDED){
								DamageVoiceRandom_Mid();

								if(other.x < x){
									if(Direction)	addDamage(30,FLYING_0, 0, 1, FLYING_0, 0, 2);
									else			addDamage(30,FLYING_1, 0, 1, FLYING_1, 0, 2);
								}else{
									if(Direction)	addDamage(30,FLYING_1, 0, 1, FLYING_1, 0, 2);
									else			addDamage(30,FLYING_0, 0, 1, FLYING_0, 0, 2);
								}
								PartsDamage(objExtParts, -70,0);
								PartsBloodDamage(objExtParts, -70,0);
								hspeed = -100 + 200*(other.x < x);
								vspeed = -10;
								sound_play(sndHit00);
								Quake(15,15);
							}
						}
					}					
					break;
			}
			if(MOTIONEND){
				MotionChange(STAND,0,1);
			}
			break;
		case SHOT:
			if(KeyFrmNum==4 && KEYFRAME){
				if(playerDistanceX > 500){
					with(objPlayerDoll){
						effect_create_below(ef_ring,x,y-320,2,c_white);
						new = instance_create(x,y-320,objBlackHole);
						new.hspeed = 0;
						new.vspeed = 5;
					}
				}else{
					with(PartsParam(PARTS_STRING[0,strLen-1])){
						effect_create_below(ef_ring,x,y,0,c_white);
						new = instance_create(x,y,objBlackHole);
						new.hspeed = Direction*-2;
						new.vspeed = -0.01*(y - other.TARGET.y);
					}
				}
			}
			if(MOTIONEND){
				MotionChange(STAND,0,1);
			}
			break;
		case CATCH:
			if(KeyFrmNum>2 && KeyFrmNum < 7){
				col = noone;
				for(i=0;i<3;i+=1){
					/*
					Line = drawLine((PartsParam(PARTS_STRING[0,strLen-(1+i)])).x,(PartsParam(PARTS_STRING[0,strLen-(1+i)])).y,
									 PreStrPosX[i]                              , PreStrPosY[i]);
					Line.Color = c_red; Line.Width = 3; Line.depth = -1000000;
					*/
					if(!col) col = collision_line((PartsParam(PARTS_STRING[0,strLen-(1+i)])).x,
												  (PartsParam(PARTS_STRING[0,strLen-(1+i)])).y,
												   PreStrPosX[i] , PreStrPosY[i], TARGET,0,1);
				}
				with( col ){
					if(!INV && !BINDED){
						sound_play(sndHit01);
						Quake(15,15);

						Direction = -1*other.Direction;
						with(other){
							if(random(100)<50){
								startBind( TARGET, BIND_CATCH,0,other.BINDED_BOSSHITOBRN_CATCH_MOJA ,0,1 );
								MotionChange(CATCH_MOJA,0,1);
							}else{
								startBind( TARGET, BIND_CATCH,0,other.BINDED_BOSSHITOBRN_CATCH_MAKI ,0,1 );
								MotionChange(CATCH_MAKI,0,1);
							}
						}
					}
				}

			}

			if(MOTIONEND){
				MotionChange(STAND,0,1);
			}
			break;
		case CATCH_MOJA:
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						DamageVoiceRandom_Low();
						with(BindTarget){ x = other.x; MotionChange(FLYING_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}

			for(strID=0;strID<strNum;strID+=1)for(i=3;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
			}
			if(MOTIONEND){
				startBind( TARGET, BIND_CATCH,0,TARGET.BINDED_BOSSHITOBRN_CATCH_MOJA_START ,0,1 );
				MotionChange(CATCH_MOJA_START,0,1);
			}
			break;
		case CATCH_MOJA_START:
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						DamageVoiceRandom_Low();
						with(BindTarget){ x = other.x; MotionChange(FLYING_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}

			PartsDamage(objExtParts, -10,0);
			addDamage(0.1,-1,-1,-1,-1,-1,-1);
			for(strID=0;strID<strNum;strID+=1)for(i=3;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
			}
			addDamage(0.5,-1,-1,-1,-1,-1,-1);
			switch(KeyFrmNum){
				case 1:
				case 4:
					if(KEYFRAME){
						Quake(3,3);
						if(random(100)<50){
							sound_play(sndLotion);
						}
						Flash(random(c_red),0.8,0.01);
						switch(irandom(4)){
							case 0:sound_play(sndVore01);break;
							case 1:sound_play(sndVore02);break;
							case 2:sound_play(sndVore03);break;
							case 3:sound_play(sndVore04);break;
						}
					}
					break;
			}
			if(random(100)<3){
				var tmp_parts;
				tmp_parts = (PartsParam(PARTS_STRING[irandom(strNum-1),4]));
				new = instance_create(tmp_parts.x,tmp_parts.y,objSpring);
				new.Points[0,new.ATTACH]    = tmp_parts;
				new.Points[0,new.ATTACHDIS] = 0;
				new.Points[0,new.ATTACHDIR] = 0;
				new.mSpringRate = 0.3;
			}
			if(isGameOver()){
				startBind( TARGET, BIND_CATCH,0,TARGET.BINDED_BOSSHITOBRN_CATCH_MOJA_FINISH ,0,1 );
				MotionChange(CATCH_MOJA_FINISH,0,1);
			}
			break;
		case CATCH_MOJA_FINISH:
			PartsBloodDamage(objExtParts, -10,0);
			addDamage(0.5,-1,-1,-1,-1,-1,-1);
			switch(KeyFrmNum){
				case 1:
				case 4:
					if(KEYFRAME){
						Quake(3,3);
						if(random(100)<50){
							sound_play(sndLotion);
							if(random(100)<50+global.PlayerHP) SpecialVoice(V_OE,irandom(5));
						}
						Flash(random(c_red),0.8,0.01);
						switch(irandom(4)){
							case 0:sound_play(sndVore01);break;
							case 1:sound_play(sndVore02);break;
							case 2:sound_play(sndVore03);break;
							case 3:sound_play(sndVore04);break;
						}
					}
					break;
			}
			if(random(100)<3){
				var tmp_parts;
				tmp_parts = (PartsParam(PARTS_STRING[irandom(strNum-1),4]));
				new = instance_create(tmp_parts.x,tmp_parts.y,objSpring);
				new.Points[0,new.ATTACH]    = tmp_parts;
				new.Points[0,new.ATTACHDIS] = 0;
				new.Points[0,new.ATTACHDIR] = 0;
				new.image_blend = COLOR_BLOOD;
				new.mSpringRate = 0.2;
			}
			for(strID=0;strID<strNum;strID+=1)for(i=3;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
			}
			break;
		case CATCH_MAKI:
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						DamageVoiceRandom_Low();
						with(BindTarget){ x = other.x; MotionChange(FLYING_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}
			for(strID=0;strID<3;strID+=1)for(i=4;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
			}
			if(MOTIONEND){
				SpecialVoice(V_GYA,3);
				loopEnemySound(SOUND_ELEC00);
				playEnemySound(SOUND_ELEC01);
				effect_create_below(ef_smokeup,x,y,2,c_black);
				Flash_Rect(c_white,random(1.1),0.02+random(0.5));
				Flash(c_white,random(1.1),0.02+random(0.5));
				Quake(10,10);

				startBind( TARGET, BIND_CATCH,0,TARGET.BINDED_BOSSHITOBRN_CATCH_MAKI_ELEC ,0,1 );
				MotionChange(CATCH_MAKI_ELEC,0,1);
				ElecCount = 0;
			}
			break;
		case CATCH_MAKI_ELEC:
			if(BindTarget){
				if(!BindTarget.BINDED && !isGameOver()){
						DamageVoiceRandom_Low();
						with(BindTarget){ x = other.x; MotionChange(FLYING_0,0,2); }
						endBind();
						MotionChange(STAND,0,20);
						break;
				}
			}
			if(global.PlayerHP<0)ElecCount += 1;
			addDamage(0.6,-1,-1,-1,-1,-1,-1);
			with(BindTarget){
				if(other.ElecCount < 60){
					if(random(100)<50){
						new = instance_create(BODY_TOP.x,BODY_TOP.y, objElec);
						new.image_blend = c_blue;
						new.image_xscale = 0.2 + random(0.8);
						new.image_yscale = new.image_xscale;
					}
					if(random(100)<50){
						new = instance_create(BODY_TOP.x,BODY_TOP.y, objElec);
						new.image_xscale = 0.2 + random(0.8);
						new.image_yscale = new.image_xscale;
					}
				}else if(other.ElecCount > 1000){
					/* 本来ココで止める */
					with(other){
						startBind( TARGET, BIND_CATCH,0,TARGET.BINDED_BOSSHITOBRN_CATCH_MAKI_ELECFINISH ,0,1 );
						MotionChange(CATCH_MAKI_ELECFINISH,0,1);
						stopEnemySound(SOUND_ELEC00);
						stopEnemySound(SOUND_HIGHELEC);
					}
				}else if(other.ElecCount < 1000){
					with(other){
						if(!HiElecFlag){
							HiElecFlag = true;
							SpecialVoice(V_GYA,4);
							loopEnemySound(SOUND_HIGHELEC);
							playEnemySound(SOUND_ELEC01);
							effect_create_below(ef_smokeup,x,y,2,c_black);
							Flash_Rect(c_white,random(1.1),0.02+random(0.5));
							Flash(c_white,random(1.1),0.02+random(0.5));
							Quake(10,10);
						}
					}

					PartsBloodDamage(objCommon_Head, -1,0);
					PartsDamage(objExtParts, -1,0);
					if(random(100)<80){
						new = instance_create(BODY_TOP.x,BODY_TOP.y, objElec2);
						new.image_xscale = 0.8 + random(0.2);
						new.image_yscale = new.image_xscale;
					}
					if(random(100)<50){
						new = instance_create(BODY_TOP.x,BODY_TOP.y, objElec);
						new.image_blend = c_blue;
						new.image_xscale = 0.8 + random(0.8);
						new.image_yscale = new.image_xscale;
					}
					if(random(100)<50){
						new = instance_create(BODY_TOP.x,BODY_TOP.y, objElec);
						new.image_xscale = 0.8 + random(0.8);
						new.image_yscale = new.image_xscale;
					}
				}

				if(random(100)<10) effect_create_above(ef_smokeup,BODY_TOP.x,BODY_TOP.y,1,c_black);
				if(random(100)<15) Flash_Rect(c_white,random(0.5),0.1+random(0.5));
				if(random(100)<15) Flash(c_white,random(0.5),0.1+random(0.5));
				if(random(100)<10) Quake(5,5);
			}

			for(strID=0;strID<3;strID+=1)for(i=4;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
				(PartsParam(PARTS_STRING[strID,i])).image_blend = make_color_hsv(0,0,50+irandom(205));
			}
			if(random(100)<30){
				var tmp;
				tmp = irandom(4);
				MotionChange(MotionNum, tmp, 1);
				with(BindTarget){ MotionChange(MotionNum, tmp, 1); }
			}
			break;
		case CATCH_MAKI_ELECFINISH:
			for(strID=0;strID<3;strID+=1)for(i=4;i<strLen;i+=1){
				(PartsParam(PARTS_STRING[strID,i])).depth = TARGET.depth - (1000 + i + strID*8);
			}
			with(PartsParam(PARTS_STRING[0,strLen-1])){ if(random(100)<10) effect_create_above(ef_smokeup,x,y,irandom(1),c_black); }
			with(BindTarget){
				if(random(100)<1) MotionChange(MotionNum,1+irandom(2),1);
				if(random(200)<1) objCommon_Body.FluidBreak = true;
			} 
			break;
		case CATCH_MOJA:
			if(MOTIONEND){
				MotionChange(CATCH_MOJA_START,0,1);
			}
			break;
		case CATCH_MOJA_START:
			if(keyboard_check(vk_space)) MotionChange(STAND,0,1);
			break;
		case TURN:
			if(place_free(TARGET.x - 256*TARGET.Direction,TARGET.y - 256)){
				tmp_dis = point_distance( x,y,TARGET.x - 256*TARGET.Direction,TARGET.y - 256);
				tmp_dir = point_direction(x,y,TARGET.x - 256*TARGET.Direction,TARGET.y - 256);
			}else{
				tmp_dis = point_distance( x,y,TARGET.x + 256*TARGET.Direction,TARGET.y - 256);
				tmp_dir = point_direction(x,y,TARGET.x + 256*TARGET.Direction,TARGET.y - 256);
			}
			if(room == Stage5_BoatRoom2) motion_add(tmp_dir, min( 3, (tmp_dis/  1.5)) );
			else						 motion_add(tmp_dir, min( 3, (tmp_dis/300.0)) );

			if(KeyFrmNum == 3 && KEYFRAME){
				Direction *= -1;
				AngleBlurOff = true;
			}
			if(MOTIONEND) MotionChange(STAND,0,2);
			break;
		case DEAD_MOTION:
			break;
	}
}

SpaceCount -= 1;
if(SpaceCount == 0){
	Flash_Rect(c_white, 1, 0.1);
	global.G = 1.5;

	with(objUnyoBack){ instance_destroy(); }
	tile_layer_show(10010);
	tile_layer_show(-9999);
	tile_layer_show(-10000);
	tile_layer_show(-10001);
	tile_layer_show(-11000);
	instance_activate_object( objWaterBase );
	with( objWaterBase ){ visible = true; }
	instance_activate_object( objCamera );
	with( objCamera ){ visible = true; }
}

PreStrPosX[0] = (PartsParam(PARTS_STRING[0,strLen-1])).x;
PreStrPosY[0] = (PartsParam(PARTS_STRING[0,strLen-1])).y;
PreStrPosX[1] = (PartsParam(PARTS_STRING[0,strLen-2])).x;
PreStrPosY[1] = (PartsParam(PARTS_STRING[0,strLen-2])).y;
PreStrPosX[2] = (PartsParam(PARTS_STRING[0,strLen-3])).x;
PreStrPosY[2] = (PartsParam(PARTS_STRING[0,strLen-3])).y;
