Created
November 21, 2016 15:25
-
-
Save zacharycarter/b536af17d78b518318fd7c122c9c1ac8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.ai.path; | |
import com.artemis.Entity; | |
import com.badlogic.gdx.ai.pfa.Connection; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.utils.Array; | |
import com.carterza.generation.ship.grid.GridMap; | |
import java.util.List; | |
import static com.carterza.ai.path.TiledNode.TILE_EMPTY; | |
import static com.carterza.ai.path.TiledNode.TILE_FLOOR; | |
import static com.carterza.ai.path.TiledNode.TILE_WALL; | |
/** A random generated graph representing a flat tiled map. | |
* | |
* @author davebaol */ | |
public class FlatTiledGraph implements TiledGraph<FlatTiledNode> { | |
protected Array<FlatTiledNode> nodes; | |
public boolean diagonal; | |
public FlatTiledNode startNode; | |
public FlatTiledGraph () { | |
this.diagonal = false; | |
this.startNode = null; | |
} | |
private static int width, height; | |
@Override | |
public void init(GridMap gridMap, int startX, int startY, int width, int height, List<Vector2> positions) { | |
this.width = width; | |
this.height = height; | |
this.nodes = new Array<FlatTiledNode>(width * height); | |
for(int row = startX; row < startX+width; row++) { | |
for(int col = startY; col < startY+height; col++) { | |
if(!positions.contains(new Vector2(row, col))) { | |
switch (gridMap.getTile(row, col)) { | |
case FLOOR: | |
nodes.add(new FlatTiledNode(row, col, TILE_FLOOR, 4)); | |
break; | |
case DOOR: | |
nodes.add(new FlatTiledNode(row, col, TILE_FLOOR, 4)); | |
break; | |
case WALL: | |
nodes.add(new FlatTiledNode(row, col, TILE_WALL, 4)); | |
break; | |
case REINFORCED_WALL: | |
nodes.add(new FlatTiledNode(row, col, TILE_WALL, 4)); | |
break; | |
default: | |
nodes.add(new FlatTiledNode(row, col, TILE_EMPTY, 4)); | |
break; | |
} | |
} else { | |
nodes.add(new FlatTiledNode(row, col, TILE_EMPTY, 4)); | |
} | |
} | |
} | |
// Each node has up to 4 neighbors, therefore no diagonal movement is possible | |
for (int x = 0; x < width; x++) { | |
int idx = x * height; | |
for (int y = 0; y < height; y++) { | |
FlatTiledNode n = nodes.get(idx + y); | |
if (x > 0) addConnection(n, -1, 0); | |
if (y > 0) addConnection(n, 0, -1); | |
if (x < width - 1) addConnection(n, 1, 0); | |
if (y < height - 1) addConnection(n, 0, 1); | |
} | |
} | |
} | |
@Override | |
public FlatTiledNode getNode (int x, int y) { | |
FlatTiledNode node = null; | |
for(FlatTiledNode n : nodes) { | |
if(n.x == x && n.y == y) { | |
node = n; | |
break; | |
} | |
} | |
return node; | |
} | |
@Override | |
public FlatTiledNode getNode (int index) { | |
return nodes.get(index); | |
} | |
@Override | |
public int getIndex (FlatTiledNode node) { | |
int index = 0; | |
for(FlatTiledNode n : nodes) { | |
if(n.x == node.x && n.y == node.y) | |
break; | |
index++; | |
} | |
return index; | |
} | |
@Override | |
public int getNodeCount () { | |
return nodes.size; | |
} | |
@Override | |
public Array<Connection<FlatTiledNode>> getConnections (FlatTiledNode fromNode) { | |
return fromNode.getConnections(); | |
} | |
private void addConnection (FlatTiledNode n, int xOffset, int yOffset) { | |
FlatTiledNode target = getNode(n.x + xOffset, n.y + yOffset); | |
if (target.type == TILE_FLOOR) n.getConnections().add(new FlatTiledConnection(this, n, target)); | |
} | |
public static int getWidth() { | |
return width; | |
} | |
public static int getHeight() { | |
return height; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.ai.path; | |
import com.badlogic.gdx.ai.pfa.Connection; | |
import com.badlogic.gdx.utils.Array; | |
/** A node for a {@link FlatTiledGraph}. | |
* | |
* @author davebaol */ | |
public class FlatTiledNode extends TiledNode<FlatTiledNode> { | |
public FlatTiledNode (int x, int y, int type, int connectionCapacity) { | |
super(x, y, type, new Array<Connection<FlatTiledNode>>(connectionCapacity)); | |
} | |
@Override | |
public int getIndex () { | |
return x * FlatTiledGraph.getHeight() + y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment