Example usage for com.badlogic.gdx.ai.pfa GraphPath get

List of usage examples for com.badlogic.gdx.ai.pfa GraphPath get

Introduction

In this page you can find the example usage for com.badlogic.gdx.ai.pfa GraphPath get.

Prototype

public N get(int index);

Source Link

Document

Returns the item of this path at the given index.

Usage

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   ww  w.  j  a v  a  2  s  .co m
 * @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;
}