Пример #1
0
    // Use this for initialization

    void Start()
    {
        unit = GetComponent <Unit>();
        string    seeker    = transform.position.x + ", " + transform.position.y + ", " + transform.position.z;
        TextAsset textAsset = Resources.Load("shortestPath") as TextAsset;

        if (textAsset != null)
        {
            shortestPathModel = JsonUtility.FromJson <ShortestPathModel>(textAsset.text);
            foreach (ShortestPathBaseModel shortestPathBaseModel in shortestPathModel.__Mappings__)
            {
                if (seeker == shortestPathBaseModel.seeker.ToString())
                {
                    unit.target.position = new Vector3(
                        shortestPathModel.__Mappings__[0].target.x,
                        shortestPathModel.__Mappings__[0].target.y,
                        shortestPathModel.__Mappings__[0].target.z
                        );
                    unit.PathFinder();
                    break;
                }
            }
        }
        else
        {
            Debug.Log("Error: TargetButtonBehavior Json Read");
        }
    }
        public void Setup()
        {
            m_NodeToDisplayLineConverter = Substitute.For <ILineToLineNodeConverterToDisplayLineConverter>();

            m_Logger = Substitute.For <ILogger>();
            m_Bus = Substitute.For <IBus>();
            m_Converter = Substitute.For <IPathToLineToLineNodeConverter>();
            m_Factory = Substitute.For <ILineToLineNodeConverterToDisplayLineConverterFactory>();
            m_Factory.Create().Returns(m_NodeToDisplayLineConverter);

            m_Model = new ShortestPathModel(m_Logger,
                                            m_Bus,
                                            m_Converter,
                                            m_Factory);
        }
Пример #3
0
    private void FindShortestPath()
    {
        if (_lastPlayerTransform == (Vector2)_player.position)
        {
            return;
        }
        //Reset lists
        _closedList = new List <ShortestPathModel>();
        _openList   = new List <ShortestPathModel>();
        //Add initial position to closed list
        _closedList.Add(new ShortestPathModel {
            gCost    = _step,
            hCost    = Vector2.Distance(transform.position, _player.position),
            fCost    = _step + Vector2.Distance(transform.position, _player.position),
            position = transform.position
        });

        //		Debug.Log("[G Cost]: " + _closedList[0].gCost + " - [H Cost]:" + _closedList[0].hCost + " - [F Cost]:" + _closedList[0].fCost + " - [Position]:" + _closedList[0].position);

        //Add 4 adjacents (UP DOWN LEFT RIGHT)
        //Up
        var newPosition          = new Vector2(transform.position.x, transform.position.y + _step);
        var newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
        var lowestFValueIndex    = _openList.Count - 1;

        if (!_closedList.Contains(newShortestPathModel))
        {
            _openList.Add(newShortestPathModel);
            lowestFValueIndex = _openList.Count - 1;
        }

        //Down
        newPosition          = new Vector2(transform.position.x, transform.position.y - _step);
        newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
        if (!_closedList.Contains(newShortestPathModel))
        {
            _openList.Add(newShortestPathModel);
            lowestFValueIndex = _openList[_openList.Count - 1].fCost < _openList[lowestFValueIndex].fCost
                                ? _openList.Count - 1
                                : lowestFValueIndex;
        }

        //Left
        newPosition          = new Vector2(transform.position.x - _step, transform.position.y);
        newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
        if (!_closedList.Contains(newShortestPathModel))
        {
            _openList.Add(newShortestPathModel);
            lowestFValueIndex = _openList[_openList.Count - 1].fCost < _openList[lowestFValueIndex].fCost
                                ? _openList.Count - 1
                                : lowestFValueIndex;
        }

        //Right
        newPosition          = new Vector2(transform.position.x + _step, transform.position.y);
        newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
        if (!_closedList.Contains(newShortestPathModel))
        {
            _openList.Add(newShortestPathModel);
            lowestFValueIndex = _openList[_openList.Count - 1].fCost < _openList[lowestFValueIndex].fCost
                                ? _openList.Count - 1
                                : lowestFValueIndex;
        }

        _closedList.Add(_openList[lowestFValueIndex]);
        _openList.Remove(_openList[lowestFValueIndex]);
        var c = 0;


        while (Vector2.Distance(_closedList[_closedList.Count - 1].position, _player.position) > _minDistanceFromPlayer)
        {
            c++;
            if (c > 100)
            {
                break;
            }
            //UP
            newPosition = new Vector2(_closedList[_closedList.Count - 1].position.x,
                                      _closedList[_closedList.Count - 1].position.y + _step);
            newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
            lowestFValueIndex    = _closedList.Count - 1;
            if (!_closedList.Contains(newShortestPathModel))
            {
                _openList.Add(newShortestPathModel);
                lowestFValueIndex = _openList.Count - 1;
            }

            //Down
            newPosition = new Vector2(_closedList[_closedList.Count - 1].position.x,
                                      _closedList[_closedList.Count - 1].position.y - _step);
            newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
            if (!_closedList.Contains(newShortestPathModel))
            {
                _openList.Add(newShortestPathModel);
                lowestFValueIndex = _openList[_openList.Count - 1].hCost < _openList[lowestFValueIndex].hCost
                                        ? _openList.Count - 1
                                        : lowestFValueIndex;
            }

            //Left
            newPosition = new Vector2(_closedList[_closedList.Count - 1].position.x - _step,
                                      _closedList[_closedList.Count - 1].position.y);
            newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
            if (!_closedList.Contains(newShortestPathModel))
            {
                _openList.Add(newShortestPathModel);
                lowestFValueIndex = _openList[_openList.Count - 1].hCost < _openList[lowestFValueIndex].hCost
                                        ? _openList.Count - 1
                                        : lowestFValueIndex;
            }

            //Right
            newPosition = new Vector2(_closedList[_closedList.Count - 1].position.x + _step,
                                      _closedList[_closedList.Count - 1].position.y);
            newShortestPathModel = new ShortestPathModel(newPosition, _closedList[0].position, _player.position);
            if (!_closedList.Contains(newShortestPathModel))
            {
                _openList.Add(newShortestPathModel);
                lowestFValueIndex = _openList[_openList.Count - 1].hCost < _openList[lowestFValueIndex].hCost
                                        ? _openList.Count - 1
                                        : lowestFValueIndex;
            }

            _closedList.Add(_openList[lowestFValueIndex]);
            _openList.Remove(_openList[lowestFValueIndex]);
        }

        //		Debug.Log("Found: " + _closedList.Count + " Position: " + _closedList[_closedList.Count - 1].position);
        _lastPlayerTransform = _player.position;
        _canMove             = true;
    }