sPathCommand PopCommand() { sPathCommand command = _pathFindingQueue[0]; _pathFindingQueue.RemoveAt(0); return(command); }
void PushCommand(sPathCommand command) { _pathfindingQueue.Add(command); //sorting _pathfindingQueue.Sort(CompareHeuristic); }
void PushCommand(sPathCommand command) { _pathFindingQueue.Add(command); ////Sort heuristic값이 작은게 우선 //_pathFindingQueue.Sort(delegate (sPathCommand c1, sPathCommand c2) //{ // if (c1.heuristic < c2.heuristic) return 1; // if (c2.heuristic < c1.heuristic) return -1; // return 0; //}); }
void FindPath() { TileSystem tileSystem = TileSystem.Instance; while (0 != _pathfindingQueue.Count) { //제일 앞 타일을 하나 꺼낸다. sPathCommand command = _pathfindingQueue[0]; _pathfindingQueue.RemoveAt(0); //방문한 타일인가? if (false == command.tileCell.IsVisit()) { command.tileCell.Visit(); if (_targetTileCell.GetTilePosition().Equals(command.tileCell.GetTilePosition())) { _reverseTileCell = _targetTileCell; return; } for (int direction = 0; direction < (int)eDirection.MAX; direction++) { sTilePosition nextTilePos = new sTilePosition(command.tileCell.GetTileX(), command.tileCell.GetTileY()); TileHelper.GetNextTilePosByDirection((eDirection)direction, ref nextTilePos); TileCell nextTileCell = tileSystem.GetTileCell(nextTilePos); if (((true == tileSystem.CanMoveTileCell(nextTilePos.tileX, nextTilePos.tileY)) && false == nextTileCell.IsVisit()) || (nextTilePos.tileX == _targetTileCell.GetTileX() && nextTilePos.tileY == _targetTileCell.GetTileY())) { float newDistanceFromStart = command.tileCell.GetDistanceFromStart() + command.tileCell.GetDistanceWeight(); float newHeuristic = CalcAstarHeuristic(newDistanceFromStart, nextTileCell, _targetTileCell); if (null == nextTileCell.GetPrevCell()) { nextTileCell.SetDistanceFromStart(newDistanceFromStart); nextTileCell.SetPrevCell(command.tileCell); //이전 타일 기억 sPathCommand newCommand = new sPathCommand(); newCommand.heuristic = newHeuristic; newCommand.tileCell = nextTileCell; PushCommand(newCommand); } } } } } }
void PushCommand(sPathCommand command) { _pathfindingQueue.Add(command); // Sorting _pathfindingQueue.Sort(delegate(sPathCommand c1, sPathCommand c2) { if (c1.heuristic < c2.heuristic) { return(-1); } if (c2.heuristic < c1.heuristic) { return(1); } return(0); }); }
public void MakePathToTarget(TileCell destination) { TileSystem.Instance.ResetPathfindInfo(); #if UNITY_EDITOR ResetPathColor(); #endif _pathfindingQueue.Clear(); _targetTileCell = destination; sPathCommand command = new sPathCommand(); command.tileCell = _character.GetCurrentTileCell(); command.heuristic = 0.0f; PushCommand(command); FindPath(); BuildPath(); InitMove(); }
public void FindPath(eFindMode mode, eFindMethod method) { bool isViewRange = (eFindMode.VIEW_MOVERANGE == mode || eFindMode.VIEW_ATTACKRANGE == mode); while (0 != _pathfindingQueue.Count) { sPathCommand command = _pathfindingQueue[0]; _pathfindingQueue.RemoveAt(0); if (false == command.tileCell.IsVisit()) { command.tileCell.Visit(); //FIND TARGET if (eFindMode.FIND_PATH == mode) { if ((_targetTileCell.GetTileX() == command.tileCell.GetTileX()) && (_targetTileCell.GetTileY() == command.tileCell.GetTileY())) { _reverseTileCell = _targetTileCell; return; } } for (eMoveDirection direction = 0; direction <= eMoveDirection.DOWN; direction++) { sPosition curPosition; curPosition.x = command.tileCell.GetTileX(); curPosition.y = command.tileCell.GetTileY(); sPosition nextPosition = GlobalUtility.GetPositionByDirection(curPosition, direction); TileMap map = GameManager.Instance.GetMap(); TileCell nextTileCell = map.GetTileCell(nextPosition.x, nextPosition.y); if (CheckPrecondition(mode, nextTileCell, _targetTileCell)) { float distanceFromStart = command.tileCell.GetDistanceFromStart() + command.tileCell.GetDistanceWeight(); float heuristic = CalcHeuristic(method, distanceFromStart, command.tileCell, nextTileCell, _targetTileCell); if (isViewRange && (_range < distanceFromStart)) { return; } if (null == nextTileCell.GetPrevTileCell()) { nextTileCell.SetDistanceFromStart(distanceFromStart); nextTileCell.SetPrevTileCell(command.tileCell); sPathCommand newCommand; newCommand.tileCell = nextTileCell; newCommand.heuristic = heuristic; PushCommand(newCommand); //검색범위를 그려준다. if (isViewRange) { DrawSearchTile(nextTileCell); } } } } } } }
int CompareHeuristic(sPathCommand command1, sPathCommand command2) { return(command1.heuristic.CompareTo(command2.heuristic)); }
public void UpdatePathFinding() { if (_pathFindingQueue.Count != 0) { sPathCommand command = PopCommand(); if (command.tileCell.Marked() == false) { command.tileCell.Mark(); if (command.tileCell.CanMove() == true) { command.tileCell.SetPathFindingTestMark(Color.blue); } else if (command.tileCell.CanMove() == false) { command.tileCell.SetPathFindingTestMark(Color.red); } if (command.tileCell.GetDistanceFromStart() > 5) { _reverseTileCell = command.tileCell; _updateState = eUpdateState.BUILD; return; } // 목표 도달? if (command.tileCell.GetTileX() == _targetTileCell.GetTileX() && command.tileCell.GetTileY() == _targetTileCell.GetTileY()) { _updateState = eUpdateState.BUILD; _reverseTileCell = _targetTileCell; return; } // 주변 타일 검사 for (int direction = (int)eMoveDirection.UP; direction < (int)eMoveDirection.NONE; direction++) { sPosition currentTilePosition; currentTilePosition.x = command.tileCell.GetTileX(); currentTilePosition.y = command.tileCell.GetTileY(); sPosition nextTilePos = GetPositionByDirection(currentTilePosition, (eMoveDirection)direction); TileCell nextTileCell = GameManager.Instance.GetMap().GetTileCell(nextTilePos.x, nextTilePos.y); //nextTileCell.SetPathFindingTestMark(Color.red); // 검사 한타일인지 && 이동 가능한 타일 인지 && 갈수 없는 노드의 타입이 혹시 몬스터? -> 리팩토링하고싶다ㅏㅏ if ((nextTileCell.IsPathFindable() == true && nextTileCell.Marked() == false)) { float distanceFromStart = command.tileCell.GetDistanceFromStart() + command.tileCell.GetDistanceWeight() + 1; //float heuristic = CalcAStarHeuristic(distanceFromStart, nextTileCell, _targetTileCell); float heuristic = 0; switch (_character.GetPathFindingType()) { case ePathFindingType.DISTANCE: heuristic = distanceFromStart; break; case ePathFindingType.SIMPLE: heuristic = CalcSimpleHeuristic(command.tileCell, nextTileCell, _targetTileCell); break; case ePathFindingType.COMPLEX: heuristic = CalcComplexHeuristic(nextTileCell, _targetTileCell); break; case ePathFindingType.ASTAR: heuristic = CalcAStarHeuristic(distanceFromStart, nextTileCell, _targetTileCell); break; } if (nextTileCell.GetPrevTileCell() == null) { nextTileCell.SetDistanceFromStart(distanceFromStart); nextTileCell.SetPrevTileCell(command.tileCell); sPathCommand newCommand; newCommand.heuristic = heuristic; newCommand.tileCell = nextTileCell; PushCommand(newCommand); } } } } } }
protected void UpdatePathfinding() { // 길찾기 알고리즘이 시작 if (0 != _pathfindingQueue.Count) { sPathCommand command = _pathfindingQueue[0]; _pathfindingQueue.RemoveAt(0); if (false == command.tileCell.IsPathfided()) { command.tileCell.Pathfinded(); // 목표에 도달 했나? if (command.tileCell.GetTileX() == _goalTileCell.GetTileX() && command.tileCell.GetTileY() == _goalTileCell.GetTileY()) { _reverseTileCell = command.tileCell; _updateState = eUpdateState.BUILD_PATH; return; } for (int direction = (int)eMoveDirection.LEFT; direction < (int)eMoveDirection.DOWN + 1; direction++) { sPosition curPosition; curPosition.x = command.tileCell.GetTileX(); curPosition.y = command.tileCell.GetTileY(); sPosition nextPosition = GetPositionByDirection(curPosition, (eMoveDirection)direction); TileCell searchTileCell = GameManager.Instance.GetMap().GetTileCell(nextPosition.x, nextPosition.y); if (null != searchTileCell && searchTileCell.IsPathfindable() && false == searchTileCell.IsPathfided()) { float distance = command.tileCell.GetDistanceFromStart() + searchTileCell.GetDistanceWeight(); if (null == searchTileCell.GetPrevPathfindingCell()) { searchTileCell.SetDistanceFromStart(distance); searchTileCell.SetPrevPathfindingCell(command.tileCell); searchTileCell.SetPathfindingTestMark(); sPathCommand newCommand; newCommand.tileCell = searchTileCell; /* * newCommand.heuristic = CalcSimpleHeuristic( * command.tileCell, * searchTileCell, * _goalTileCell); */ newCommand.heuristic = CalcAStarHeuristic(distance, searchTileCell, _goalTileCell); PushCommand(newCommand); } else { if (distance < searchTileCell.GetDistanceFromStart()) { searchTileCell.SetDistanceFromStart(distance); searchTileCell.SetPrevPathfindingCell(command.tileCell); sPathCommand newCommand; newCommand.tileCell = searchTileCell; /* * newCommand.heuristic = CalcSimpleHeuristic( * command.tileCell, * searchTileCell, * _goalTileCell); */ newCommand.heuristic = CalcAStarHeuristic(distance, searchTileCell, _goalTileCell); PushCommand(newCommand); } } } } } } }
void PushCommand(sPathCommand command) { _pathfindingQueue.Add(command); _pathfindingQueue.Sort((sPathCommand lhs, sPathCommand rhs) => lhs.heuristic.CompareTo(rhs.heuristic)); }