Esempio n. 1
0
    // Get outgoing connections from given node
    public IEnumerable <IConnection> GetConnections(int fromNode)
    {
        // Convert node to tile-world position
        Vector2Int node = Ind2Vec(fromNode, world.GetLength(0));

        // If the position is not blocked, it's a node with possible outgoing
        // connections
        if (world[node.x, node.y].TileType != TileTypeEnum.Blocked)
        {
            // Possible destination nodes
            Vector2Int up, down, left, right;

            // Horizontal world position
            int xdim = world.GetLength(0);

            // Determine node above
            up = node + Vector2Int.up;
            // Is node within world limits and unblocked?
            if (up.y < world.GetLength(1) &&
                world[up.x, up.y].TileType != TileTypeEnum.Blocked)
            {
                // If so, yield return connection to this node
                yield return(new Connection(1f, fromNode, Vec2Ind(up, xdim)));
            }

            // Determine node below
            down = node + Vector2Int.down;
            // Is node within world limits and unblocked?
            if (down.y >= 0 &&
                world[down.x, down.y].TileType != TileTypeEnum.Blocked)
            {
                // If so, yield return connection to this node
                yield return(new Connection(1f, fromNode, Vec2Ind(down, xdim)));
            }

            // Determine left node
            left = node + Vector2Int.left;
            // Is node within world limits and unblocked?
            if (left.x >= 0 &&
                world[left.x, left.y].TileType != TileTypeEnum.Blocked)
            {
                // If so, yield return connection to this node
                yield return(new Connection(1f, fromNode, Vec2Ind(left, xdim)));
            }

            // Determine right node
            right = node + Vector2Int.right;
            // Is node within world limits and unblocked?
            if (right.x < world.GetLength(0) &&
                world[right.x, right.y].TileType != TileTypeEnum.Blocked)
            {
                // If so, yield return connection to this node
                yield return(new Connection(1f, fromNode, Vec2Ind(right, xdim)));
            }
        }
    }
Esempio n. 2
0
    // Euclidean distance heuristic from given node to destination node
    private float EuclideanDistance(int node)
    {
        Vector2 nodeVec = (Vector2)Ind2Vec(node, world.GetLength(0));
        Vector2 destVec = (Vector2)GoalPos;

        return(Vector2.Distance(nodeVec, destVec));
    }
Esempio n. 3
0
    // This method is used as an heuristic for A*
    // It's the euclidean distance heuristic
    private float HeuristicForAStar(int node)
    {
        Vector2 nodeVec = (Vector2)Ind2Vec(node, world.GetLength(0));
        Vector2 destVec = (Vector2)GoalPos;

        return(Vector2.Distance(nodeVec, destVec));
    }
Esempio n. 4
0
    /// <summary>
    /// Checks if a position is in the bounds of the borad or not
    /// </summary>
    /// <param name="position"></param>
    /// <returns></returns>
    public bool IsInBounds(Vector2Int position)
    {
        if (position.x < 0 || position.x >= board.GetLength(0) || position.y < 0)
        {
            return(false);
        }

        return(true);
    }