List of usage examples for com.badlogic.gdx.ai.pfa GraphPath getCount
public int getCount();
From source file:com.v5ent.game.pfa.MyPathSmoother.java
License:Apache License
/** Smoothes the given path in place. * @param outPath the path to smooth/*from w w w .j av a 2 s . c om*/ * @return the number of nodes removed from the path. */ public int smoothPath(GraphPath<MyNode> outPath) { int inputPathLength = outPath.getCount(); // If the path is two nodes long or less, then we can't smooth it if (inputPathLength <= 2) return 0; // Make sure the ray is instantiated if (this.ray == null) { this.ray = new MyRay(outPath.get(0), outPath.get(0)); } // Keep track of where we are in the smoothed path. // We start at 1, because we must always include the start node in the smoothed path. int outputIndex = 1; // Keep track of where we are in the input path // We start at 2, because we assume two adjacent // nodes will pass the ray cast int inputIndex = 2; // Loop until we find the last item in the input while (inputIndex < inputPathLength) { // Set the ray ray.start = (outPath.get(outputIndex - 1)); ray.end = (outPath.get(inputIndex)); // Do the ray cast boolean collides = raycastCollisionDetector.collides(ray); if (collides) { // The ray test failed, swap nodes and consider the next output node outPath.swapNodes(outputIndex, inputIndex - 1); outputIndex++; } // Consider the next input node inputIndex++; } // Reached the last input node, always add it to the smoothed path. outPath.swapNodes(outputIndex, inputIndex - 1); outPath.truncatePath(outputIndex + 1); // Return the number of removed nodes return inputIndex - outputIndex - 1; }
From source file:toniarts.openkeeper.world.creature.CreatureControl.java
License:Open Source License
public void navigateToRandomPoint() { Point p = worldState.findRandomAccessibleTile( worldState.getTileCoordinates(getSpatial().getWorldTranslation()), 10, creature); if (p != null) { GraphPath<TileData> outPath = worldState .findPath(worldState.getTileCoordinates(getSpatial().getWorldTranslation()), p, creature); if (outPath != null && outPath.getCount() > 1) { // Debug // worldHandler.drawPath(new LinePath<>(pathToArray(outPath))); PrioritySteering<Vector2> prioritySteering = new PrioritySteering(this, 0.0001f); FollowPath<Vector2, LinePathParam> followPath = new FollowPath(this, new LinePath<>(pathToArray(outPath), true), 2); followPath.setDecelerationRadius(1f); followPath.setArrivalTolerance(0.2f); prioritySteering.add(followPath); prioritySteering.setEnabled(!isAnimationPlaying()); setSteeringBehavior(prioritySteering); }/* w w w. ja va 2s. c o m*/ } }
From source file:toniarts.openkeeper.world.creature.CreatureControl.java
License:Open Source License
private Array<Vector2> pathToArray(GraphPath<TileData> outPath) { Array<Vector2> path = new Array<>(outPath.getCount()); for (TileData tile : outPath) { path.add(new Vector2(tile.getX() - 0.5f, tile.getY() - 0.5f)); }/*from w ww .ja va 2 s . c o m*/ return path; }
From source file:toniarts.openkeeper.world.creature.CreatureControl.java
License:Open Source License
public void navigateToAssignedTask() { Vector2f loc = assignedTask.getTarget(this); if (loc != null) { GraphPath<TileData> outPath = worldState.findPath( worldState.getTileCoordinates(getSpatial().getWorldTranslation()), new Point((int) Math.floor(loc.x), (int) Math.floor(loc.y)), creature); if (outPath != null && outPath.getCount() > 1) { // Debug // worldHandler.drawPath(new LinePath<>(pathToArray(outPath))); PrioritySteering<Vector2> prioritySteering = new PrioritySteering(this, 0.0001f); FollowPath<Vector2, LinePathParam> followPath = new FollowPath(this, new LinePath<>(pathToArray(outPath), true), 2); followPath.setDecelerationRadius(1f); followPath.setArrivalTolerance(0.2f); prioritySteering.add(followPath); prioritySteering.setEnabled(!isAnimationPlaying()); setSteeringBehavior(prioritySteering); }/*ww w . j av a2s . c o m*/ } }