Exemplo n.º 1
0
    public List <LFGridNode> GetNeighbours(LFGridNode node)
    {
        List <LFGridNode> neighbours = new List <LFGridNode> ();

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if ((x == 0 && y == 0) || (x == 1 && y == 1) || (x == -1 && y == -1) || (x == -1 && y == 1) || (x == 1 && y == -1))
                {
                    continue;
                }

                int checkX = node.GridX + x;
                int checkY = node.GridY + y;

                if (checkX >= 0 && checkX < _gridSizeX && checkY >= 0 && checkY < _gridSizeY)
                {
                    neighbours.Add(_grid [checkX, checkY]);
                }
            }
        }

        return(neighbours);
    }
Exemplo n.º 2
0
    private List <LFGridNode> FindNodePath(LFGridNode startNode, LFGridNode targetNode)
    {
        List <LFGridNode>    openSet  = new List <LFGridNode> ();
        HashSet <LFGridNode> closeSet = new HashSet <LFGridNode> ();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            LFGridNode currentNode = openSet [0];

            for (int i = 0; i < openSet.Count; i++)
            {
                if (openSet [i].FCost <= currentNode.FCost && openSet [i].HCost < currentNode.HCost)
                {
                    currentNode = openSet [i];
                }
            }

            openSet.Remove(currentNode);
            closeSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                List <LFGridNode> path = RetracePath(startNode, targetNode);

                return(path);
            }

            List <LFGridNode> neighbours = _grid.GetNeighbours(currentNode);

            foreach (LFGridNode neighbour in neighbours)
            {
                if (!neighbour.IsCanWalk || closeSet.Contains(neighbour))
                {
                    continue;
                }

                int newMoveCostToNeighbour = currentNode.GCost + GetNodeDistance(currentNode, neighbour);

                if (newMoveCostToNeighbour < neighbour.GCost || !openSet.Contains(neighbour))
                {
                    neighbour.GCost      = newMoveCostToNeighbour;
                    neighbour.HCost      = GetNodeDistance(neighbour, targetNode);
                    neighbour.ParentNode = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }

        List <LFGridNode> emptyPath = new List <LFGridNode>();

        emptyPath.Add(startNode);

        return(emptyPath);
    }
Exemplo n.º 3
0
    public List <LFGridNode> FindPath(Vector3 startPos, Vector3 targetPos)
    {
        LFGridNode startNode  = _grid.GridNodeFromWorldPosition(startPos);
        LFGridNode targetNode = _grid.GridNodeFromWorldPosition(targetPos);

        return(FindNodePath(startNode, targetNode));
    }
Exemplo n.º 4
0
    public List <LFGridNode> RandomNodePath(Vector3 startPos)
    {
        LFGridNode startNode  = _grid.GridNodeFromWorldPosition(startPos);
        LFGridNode targetNode = _grid.GridNodeFromWorldPosition(_labyrinth.RandomFreeNodeWithOutPosition(startPos).WorldPosition);

        return(FindNodePath(startNode, targetNode));
    }
Exemplo n.º 5
0
    // Update is called once per frame
    void Update()
    {
        if (_targetPosition != null && !EnemyOnTargetNode())
        {
            transform.position = Vector3.MoveTowards(transform.position, _targetPosition, speed * Time.deltaTime);
        }
        else
        {
            switch (_targetType)
            {
            case LFTargetType.player:
                UpdatePath();

                break;

            default:

                if ((_path.Count - 1) > _pathStepIndex)
                {
                    _pathStepIndex += 1;
                    _targetNode     = _path[_pathStepIndex];
                    _targetPosition = new Vector3(_targetNode.WorldPosition.x, _targetNode.WorldPosition.y, transform.position.z);
                }
                else
                {
                    UpdatePath();
                }

                break;
            }
        }

        UpdateSprite();
    }
Exemplo n.º 6
0
    private void UpdatePath()
    {
        _path          = pathFinder.RandomNodePath(transform.position);
        _pathStepIndex = 0;

        if (_path != null && _path.Count > 0)
        {
            _targetNode     = _path[_pathStepIndex];
            _targetPosition = new Vector3(_targetNode.WorldPosition.x, _targetNode.WorldPosition.y, transform.position.z);
        }
    }
Exemplo n.º 7
0
    private int GetNodeDistance(LFGridNode node0, LFGridNode node1)
    {
        int distX = Mathf.Abs(node0.GridX - node1.GridX);
        int distY = Mathf.Abs(node0.GridY - node1.GridY);

        if (distX > distY)
        {
            return(14 * distY + 10 * (distX - distY));
        }

        return(14 * distX + 10 * (distY - distX));
    }
Exemplo n.º 8
0
    private List <LFGridNode> RetracePath(LFGridNode startNode, LFGridNode endNode)
    {
        List <LFGridNode> path        = new List <LFGridNode> ();
        LFGridNode        currentNode = endNode;

        while (currentNode != startNode)
        {
            path.Add(currentNode);
            currentNode = currentNode.ParentNode;
        }

        path.Reverse();
        return(path);
    }
Exemplo n.º 9
0
    private void InitGrid()
    {
        _grid = new LFGridNode[_gridSizeX, _gridSizeY];
        Vector3 leftBottomCorner = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2;

        for (int x = 0; x < _gridSizeX; x++)
        {
            for (int y = 0; y < _gridSizeY; y++)
            {
                Vector3 worldPoint = leftBottomCorner + Vector3.right * (x * _nodeDiameter + nodeRadius) + Vector3.up * (y * _nodeDiameter + nodeRadius);
                bool    isCanWalk  = !(Physics.CheckSphere(worldPoint, nodeRadius, wallMask));
                _grid[x, y] = new LFGridNode(isCanWalk, worldPoint, x, y, _nodeDiameter);
            }
        }
    }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
        if (_targetPosition != null && !EnemyOnTargetNode())
        {
            transform.position = Vector3.MoveTowards(transform.position, _targetPosition, speedScaleFactor * speed * Time.deltaTime);
        }
        else
        {
            switch (_state)
            {
            case LFAnimalState.run:
                _moveSpeed = speed * speedScaleFactor;

                break;

            default:
                _moveSpeed = speed;
                break;
            }

            if ((_path.Count - 1) > _pathStepIndex)
            {
                _pathStepIndex += 1;
                _targetNode     = _path[_pathStepIndex];
                _targetPosition = new Vector3(_targetNode.WorldPosition.x, _targetNode.WorldPosition.y, transform.position.z);
            }
            else
            {
                UpdatePath();
            }
        }

        UpdateSprite();

        if (_state == LFAnimalState.run && _timeRun <= 0)
        {
            _state   = LFAnimalState.walk;
            _timeRun = runTime;
        }
        else if (_state == LFAnimalState.run && _timeRun > 0)
        {
            _timeRun -= Time.deltaTime;
        }
    }
Exemplo n.º 11
0
    private void UpdatePath()
    {
        switch (_targetType)
        {
        case LFTargetType.player:
            _path          = pathFinder.FindPath(transform.position, _player.position);
            _pathStepIndex = 0;

            break;

        default:

            _path          = pathFinder.RandomNodePath(transform.position);
            _pathStepIndex = 0;

            break;
        }

        if (_path != null && _path.Count > 0)
        {
            _targetNode     = _path[_pathStepIndex];
            _targetPosition = new Vector3(_targetNode.WorldPosition.x, _targetNode.WorldPosition.y, transform.position.z);
        }
    }