/// <summary> /// 移動先セルに敵が居るか判定 主に敵の移動で使用 /// </summary> /// <param name="moveAbleInfo"></param> /// <returns></returns> private bool isDestinationEnemyExist(MoveAmountInfo moveAbleInfo) { //移動先に自分以外の敵が存在するか判定 EnemyModel destinationEnemy = enemyContainer.GetComponentsInChildren <EnemyModel>().FirstOrDefault( c => c.x == moveAbleInfo.coordinate.x && c.y == moveAbleInfo.coordinate.y); //移動先に敵がいる場合、自分自身以外ならそのセルには移動しない if (destinationEnemy != null) { if (destinationEnemy != ActiveEnemy) { Debug.Log($"既に敵が存在するので移動しないセル X={moveAbleInfo.coordinate.x}, Y={moveAbleInfo.coordinate.y}"); return(true); } } return(false); }
//210214 最もプレイヤーに近いセルを返す public Main_Cell SearchPlayerNearestCell(int x, int y, int moveAmount, PlayerModel[] players) { //探索モード //探索モードでは向かう対象 = 移動可能ではなく、移動力が最大 = 敵の現在いる座標という事にはならないので、 //まず、プレイヤーに一番近いセルを移動可能距離の中から見つめてそこを行き先にする Debug.Log("プレイヤーへの最短距離を検索"); MoveAmountInfo nearestInfo = null; int nearestDistance = 0; //x,yを元に移動開始するセルを取得 var startCell = cells.First(c => c.X == x && c.Y == y); //移動可能となるセルリストがMoveAmountInfo型で返ってくる var moveAbleInfos = GetRemainingMoveAmountInfos(startCell, moveAmount, CalculationMode.ENEMY_SEARCH); foreach (PlayerModel player in players) { //プレイヤーの座標を取得 Main_Cell playerCell = Cells.FirstOrDefault(c => c.X == player.x && c.Y == player.z); Debug.Log($"探索したプレイヤー{player.unit.name} x= {player.x} , y= {player.z}"); foreach (MoveAmountInfo moveAbleInfo in moveAbleInfos) { //最初の1ループは存在しないので、まず設定 if (nearestInfo == null) { //既に自分以外の敵が居れば除外 if (isDestinationEnemyExist(moveAbleInfo)) { continue; } nearestInfo = moveAbleInfo; nearestDistance = (Mathf.Abs(playerCell.X - moveAbleInfo.coordinate.x) + Mathf.Abs(playerCell.Y - moveAbleInfo.coordinate.y)); } else { //2回目以降のループ //攻撃対象への距離 少ない程優先する int distance = (Mathf.Abs(playerCell.X - moveAbleInfo.coordinate.x) + Mathf.Abs(playerCell.Y - moveAbleInfo.coordinate.y)); Debug.Log($"座標 :X={moveAbleInfo.coordinate.x}, Y={moveAbleInfo.coordinate.y},距離={distance}"); //更にプレイヤーに近いセルが有れば最短セルを更新 if (distance < nearestDistance) { //移動先に敵がいるか判定 if (isDestinationEnemyExist(moveAbleInfo)) { continue; } nearestInfo = moveAbleInfo; Debug.Log($"最もプレイヤーに近いセル更新 :X={nearestInfo.coordinate.x}, Y={nearestInfo.coordinate.y}, 距離={nearestDistance}"); nearestDistance = distance; } } } } Main_Cell nearestCell = cells.First(c => c.X == nearestInfo.coordinate.x && c.Y == nearestInfo.coordinate.y); Debug.Log($"最もプレイヤーに近いセル :X={nearestInfo.coordinate.x}, Y={nearestInfo.coordinate.y}, 距離={nearestDistance}"); return(nearestCell); }