예제 #1
0
    override public void Update()
    {
        TileCell goalTileCell = _character.GetGoalTileCell();

        if (null != goalTileCell)
        {
            _nextState = eStateType.PATHFINDING;
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 100))
            {
                MapObject mapObject = hit.collider.gameObject.GetComponent <MapObject>();
                if (null != mapObject)
                {
                    if (eMapObjectType.TILE_OBJECT == mapObject.GetObjectType())
                    {
                        //hit.collider.gameObject.GetComponent<SpriteRenderer>().color = Color.blue;
                        _character.ShowMoveCursor(hit.collider.gameObject.transform.position);

                        TileCell selectTileCell = GameManager.Instance.GetMap().GetTileCell(mapObject.GetTileX(), mapObject.GetTileY());
                        if (true == selectTileCell.IsPathfindable())
                        {
                            _character.SetGoalTileCell(selectTileCell);
                        }
                    }
                }
            }
        }
    }
예제 #2
0
    protected void SettingMovePossibleTiles()
    {
        TileMap map = GameManager.Instance.GetMap();

        map.ResetVisit(_character);

        TileCell startTileCell = map.GetTileCell(_character.GetTileX(), _character.GetTileY());

        startTileCell.SetPrevTileCell(_character, null);
        sTileHeuristicInfo startCmd;

        startCmd.tileCell  = startTileCell;
        startCmd.heuristic = 0.0f;
        _tileInfoQueue.Add(startCmd);

        while (0 != _tileInfoQueue.Count)
        {
            sTileHeuristicInfo command = _tileInfoQueue[0];
            _tileInfoQueue.RemoveAt(0);
            //가져온 커맨드의 현재 타일셀 방문 표시
            if (false == command.tileCell.IsVisited(_character))
            {
                if (_character.GetMoveRange() == command.tileCell.GetDistanceFromStart(_character))
                {
                    _tileInfoQueue.Clear();
                    return;
                }
                command.tileCell.SetVisit(_character, true);
                _movePossibleTiles.Add(command.tileCell);

                //4방향 next타일들 검사
                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 = _character.GetPositionByDirection(curPosition, direction);

                    TileCell nextTileCell = map.GetTileCell(nextPosition.x, nextPosition.y);
                    // nextTileCell 방문 안했고, 움직일수 있는 타일일때
                    if (null != nextTileCell && true == nextTileCell.IsPathfindable() && false == nextTileCell.IsVisited(_character))
                    {
                        float distanceFromStart = command.tileCell.GetDistanceFromStart(_character) + nextTileCell.GetDistanceFromWeight();
                        float heuristic         = distanceFromStart;

                        if (null == nextTileCell.GetPrevTileCell(_character) || distanceFromStart < nextTileCell.GetDistanceFromStart(_character))
                        {
                            nextTileCell.SetDistanceFromStart(_character, distanceFromStart);
                            nextTileCell.SetPrevTileCell(_character, command.tileCell);

                            sTileHeuristicInfo nextCommand;
                            nextCommand.tileCell  = nextTileCell;
                            nextCommand.heuristic = heuristic;
                            PushSortmoveRangeQueue(nextCommand);
                        }
                    }
                }
            }
        }
    }
예제 #3
0
    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);
                            }
                        }
                    }
                }
            }
        }
    }