@Override public Array<Connection<TileCoordinate>> getConnections(final TileCoordinate fromNode) { Array<Connection<TileCoordinate>> array = new Array<>(); if (!isCollision(null, fromNode.getAbove())) { array.add(new DefaultConnection<>(getCachedNode(fromNode), getCachedNode(fromNode.getAbove()))); } if (!isCollision(null, fromNode.getBelow())) { array.add(new DefaultConnection<>(getCachedNode(fromNode), getCachedNode(fromNode.getBelow()))); } if (!isCollision(null, fromNode.getLeft())) { array.add(new DefaultConnection<>(getCachedNode(fromNode), getCachedNode(fromNode.getLeft()))); } if (!isCollision(null, fromNode.getRight())) { array.add(new DefaultConnection<>(getCachedNode(fromNode), getCachedNode(fromNode.getRight()))); } return array; }
public static MyGraph createGraph (AStarMap map) { final int height = map.getHeight(); final int width = map.getWidth(); MyGraph graph = new MyGraph(map); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Node node = map.getNodeAt(x, y); if (node.isWall) { continue; } // Add a connection for each valid neighbor for (int offset = 0; offset < NEIGHBORHOOD.length; offset++) { int neighborX = node.x + NEIGHBORHOOD[offset][0]; int neighborY = node.y + NEIGHBORHOOD[offset][1]; if (neighborX >= 0 && neighborX < width && neighborY >= 0 && neighborY < height) { Node neighbor = map.getNodeAt(neighborX, neighborY); if (!neighbor.isWall) { // Add connection to walkable neighbor node.getConnections().add(new DefaultConnection<Node>(node, neighbor)); } } } node.getConnections().shuffle(); } } return graph; }
public static MyGraph generateGraph(TiledMapTileLayer mapCollisionLayer,int numCols,int numRows,int tileW,int tileH) { final MyNode[][] nodes = new MyNode[numCols][numRows]; final Array<MyNode> indexedNodes = new Array<MyNode>(numCols * numRows); //初始化数据地图 final String[][] tiles = new String[numCols][numRows]; for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++) { tiles[x][y] = "."; if(mapCollisionLayer!=null&&mapCollisionLayer.getCell(x, y)!=null){ tiles[x][y] = "#"; } } } //print if(Gdx.app.getLogLevel()==Application.LOG_DEBUG){ Gdx.app.debug(TAG,"----------------------------------"); StringBuilder temp = new StringBuilder("\n"); for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++) { temp.append(tiles[x][numRows-1-y]) ; } temp.append("\n"); } Gdx.app.debug(TAG,temp.toString()); Gdx.app.debug(TAG,"----------------------------------"); } int index = 0; for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++, index++) { nodes[x][y] = new MyNode(index, x, y,tiles[x][y], 4); indexedNodes.add(nodes[x][y]); } } for (int y = 0; y < numRows; y++, index++) { for (int x = 0; x < numCols; x++, index++) { if (tiles[x][y].equals("#")) { continue; } if (x - 1 >= 0 && tiles[x - 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x - 1][y])); } if (x + 1 < numCols && tiles[x + 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x + 1][y])); } if (y - 1 >= 0 && tiles[x][y - 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y - 1])); } if (y + 1 < numRows && tiles[x][y + 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y + 1])); } } } return new MyGraph(indexedNodes); }
private static MyGraph createGraphFromTextRepresentation (final String graphTextRepresentation) { final String[][] tiles = createStringTilesFromGraphTextRepresentation(graphTextRepresentation); final int numRows = tiles[0].length; final int numCols = tiles.length; final MyNode[][] nodes = new MyNode[numCols][numRows]; final Array<MyNode> indexedNodes = new Array<MyNode>(numCols * numRows); int index = 0; for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++, index++) { nodes[x][y] = new MyNode(index, x, y,tiles[x][y], 4); indexedNodes.add(nodes[x][y]); } } for (int y = 0; y < numRows; y++, index++) { for (int x = 0; x < numCols; x++, index++) { if (tiles[x][y].equals("#")) { continue; } if (x - 1 >= 0 && tiles[x - 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x - 1][y])); } if (x + 1 < numCols && tiles[x + 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x + 1][y])); } if (y - 1 >= 0 && tiles[x][y - 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y - 1])); } if (y + 1 < numRows && tiles[x][y + 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y + 1])); } } } return new MyGraph(indexedNodes); }
private static MyGraph createGraphFromTextRepresentation (final String graphTextRepresentation) { final String[][] tiles = createStringTilesFromGraphTextRepresentation(graphTextRepresentation); final int numRows = tiles[0].length; final int numCols = tiles.length; final MyNode[][] nodes = new MyNode[numCols][numRows]; final Array<MyNode> indexedNodes = new Array<>(numCols * numRows); int index = 0; for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++, index++) { nodes[x][y] = new MyNode(index, x, y, 4); indexedNodes.add(nodes[x][y]); } } for (int y = 0; y < numRows; y++, index++) { for (int x = 0; x < numCols; x++, index++) { if (tiles[x][y].equals("#")) { continue; } if (x - 1 >= 0 && tiles[x - 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x - 1][y])); } if (x + 1 < numCols && tiles[x + 1][y].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x + 1][y])); } if (y - 1 >= 0 && tiles[x][y - 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y - 1])); } if (y + 1 < numRows && tiles[x][y + 1].equals(".")) { nodes[x][y].getConnections().add(new DefaultConnection<MyNode>(nodes[x][y], nodes[x][y + 1])); } } } return new MyGraph(indexedNodes); }
public void addNeighbour(TestNode aNode) { if (null != aNode) { mConnections.add(new DefaultConnection<TestNode>(this, aNode)); } }
public void addNeighbour(Node aNode) { if (null != aNode) { connections.add(new DefaultConnection<Node>(this, aNode)); } }