Skip to content
Permalink
18ea6546a4
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
185 lines (155 sloc) 6.18 KB
/**
Our Implementation of an AbstractNode
*/
import java.util.ArrayList;
/**
* A simple Example implementation of a Node only overriding the sethCosts
* method; uses manhatten method.
*/
public class Node extends AbstractNode {
public static final int LEFT = 0;
public static final int STRAIGHT = 1;
public static final int RIGHT = 2;
private ArrayList<Node> neighbors; // List of nodes this node is connected to
public Node(float xPosition, float yPosition) {
super(xPosition, yPosition);
// do other init stuff
neighbors = new ArrayList<Node>(3);
}
////////////////////////////////////////////////////////////////////////////////////////////
// Neighbor Getting/Setting
////////////////////////////////////////////////////////////////////////////////////////////
// ---------------------------------------------------------
// Setting
// --------------------------------------------------------
public void setLeft(Node aNode) {
neighbors.set(LEFT, aNode);
}
public void setStraight(Node aNode) {
neighbors.set(STRAIGHT, aNode);
}
public void setRight(Node aNode) {
neighbors.set(RIGHT, aNode);
}
public void setNext(Node aNode, int direction) {
neighbors.set(direction, aNode);
}
public void setParent(Node aNode, int fromDirection) {
aNode.setNext(aNode, fromDirection);
}
// ---------------------------------------------------------
// Getting
// --------------------------------------------------------
public Node getLeft() {
return neighbors.get(LEFT);
}
public Node getStraight() {
return neighbors.get(STRAIGHT);
}
public Node getRight() {
return neighbors.get(RIGHT);
}
public ArrayList<Node> getNeighbors() {
return neighbors;
}
////////////////////////////////////////////////////////////////////////////////////////////
// G cost
////////////////////////////////////////////////////////////////////////////////////////////
/**
* sets gCosts to <code>gCosts</code> plus <code>movementPanelty</code>
* for this AbstractNode.
*
* @param gCosts the gCosts to set
*/
private void setgCosts(float gCosts) {
this.gCosts = gCosts + movementPanelty;
}
/**
* sets gCosts to <code>gCosts</code> plus <code>movementPanelty</code>
* for this AbstractNode given the previous AbstractNode as well as the basic cost
* from it to this AbstractNode.
*
* @param previousAbstractNode
* @param basicCost
*/
public void setgCosts(AbstractNode previousAbstractNode, float basicCost) {
setgCosts(previousAbstractNode.getgCosts() + basicCost);
}
/**
* sets gCosts to <code>gCosts</code> plus <code>movementPanelty</code>
* for this AbstractNode given the previous AbstractNode.
* <p>
* It will assume <code>BASICMOVEMENTCOST</code> as the cost from
* <code>previousAbstractNode</code> to itself if the movement is not diagonally,
* otherwise it will assume <code>DIAGONALMOVEMENTCOST</code>.
* Weather or not it is diagonally is set in the Map class method which
* finds the adjacent AbstractNodes.
*
* @param previousAbstractNode
*/
public void setgCosts(AbstractNode previousAbstractNode) {
if (diagonally) {
setgCosts(previousAbstractNode, DIAGONALMOVEMENTCOST);
} else {
setgCosts(previousAbstractNode, BASICMOVEMENTCOST);
}
}
/**
* calculates - but does not set - g costs.
* <p>
* It will assume <code>BASICMOVEMENTCOST</code> as the cost from
* <code>previousAbstractNode</code> to itself if the movement is not diagonally,
* otherwise it will assume <code>DIAGONALMOVEMENTCOST</code>.
* Weather or not it is diagonally is set in the Map class method which
* finds the adjacent AbstractNodes.
*
* @param previousAbstractNode
* @return gCosts
*/
public float calculategCosts(AbstractNode previousAbstractNode) {
if (diagonally) {
return (previousAbstractNode.getgCosts()
+ DIAGONALMOVEMENTCOST + movementPanelty);
} else {
return (previousAbstractNode.getgCosts()
+ BASICMOVEMENTCOST + movementPanelty);
}
}
/**
* calculates - but does not set - g costs, adding a movementPanelty.
*
* @param previousAbstractNode
* @param movementCost costs from previous AbstractNode to this AbstractNode.
* @return gCosts
*/
public float calculategCosts(AbstractNode previousAbstractNode, float movementCost) {
return (previousAbstractNode.getgCosts() + movementCost + movementPanelty);
}
////////////////////////////////////////////////////////////////////////////////////////////
// H cost
////////////////////////////////////////////////////////////////////////////////////////////
/**
* returns estimated costs to get from this AbstractNode to end AbstractNode.
*
* @return the hCosts
*/
public float gethCosts() {
return hCosts;
}
/**
* sets hCosts.
*
* @param hCosts the hCosts to set
*/
protected void sethCosts(float hCosts) {
this.hCosts = hCosts;
}
public void sethCosts(AbstractNode endNode) {
this.sethCosts((absolute(this.getxPosition() - endNode.getxPosition())
+ absolute(this.getyPosition() - endNode.getyPosition()))
* BASICMOVEMENTCOST);
}
private float absolute(float a) {
return a > 0 ? a : -a;
}
}