Class AStarGrid
- All Implemented Interfaces:
IRenderable
Represents an A* grid used for pathfinding.
This class implements the IRenderable interface and provides methods for
managing and rendering a grid of A* nodes. It supports diagonal movement and allows updating the walkable state of nodes based on collisions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleThe penalty value assigned to nodes that have non-walkable neighboring nodes.static final doubleThe penalty value assigned to nodes that intersect with static, indestructible props. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidassignPenalty(AStarNode node) Assigns a penalty to the specified A* node based on collisions and neighboring nodes.AStarNode[][]getGrid()Gets the grid of A* nodes.getIntersectedNodes(Rectangle2D rectangle) Gets the list of A* nodes that intersect with the specified rectangle.getNeighbors(AStarNode node) Gets the list of neighboring A* nodes for the specified node.getNode(double x, double y) Gets the A* node at the specified coordinates.Gets the A* node at the specified point.intGets the size of each node in the grid.getSize()Gets the dimension of the grid.booleanChecks if diagonal corner movement is allowed in the grid.booleanChecks if diagonal movement is allowed in the grid.voidrender(Graphics2D g) Renders the visual contents of this instance onto the provided graphics context.voidsetAllowCuttingCorners(boolean allowCuttingCorners) Sets whether cutting corners during diagonal movement is allowed in the grid.voidsetAllowDiagonalMovement(boolean allowDiagonalMovement) Sets whether diagonal movement is allowed in the grid.voidupdateWalkable(Rectangle2D rectangle) Updates the walkable attribute of nodes intersected by the specified rectangle.
-
Field Details
-
PENALTY_STATIC_PROP
public static final double PENALTY_STATIC_PROPThe penalty value assigned to nodes that intersect with static, indestructible props.- See Also:
-
PENALTY_NOT_WALKABLE_NEIGHBOR
public static final double PENALTY_NOT_WALKABLE_NEIGHBORThe penalty value assigned to nodes that have non-walkable neighboring nodes.- See Also:
-
-
Constructor Details
-
AStarGrid
public AStarGrid(int width, int height, int nodeSize) Constructs an AStarGrid with the specified width, height, and node size.- Parameters:
width- The width of the grid.height- The height of the grid.nodeSize- The size of each node in the grid.
-
AStarGrid
Constructs an AStarGrid with the specified size and node size.- Parameters:
size- The dimension of the grid.nodeSize- The size of each node in the grid.
-
-
Method Details
-
isDiagonalMovementAllowed
public boolean isDiagonalMovementAllowed()Checks if diagonal movement is allowed in the grid.- Returns:
- True if diagonal movement is allowed; otherwise false.
-
isDiagonalCornerMovementAllowed
public boolean isDiagonalCornerMovementAllowed()Checks if diagonal corner movement is allowed in the grid.- Returns:
- True if diagonal corner movement is allowed; otherwise false.
-
getGrid
Gets the grid of A* nodes.- Returns:
- A 2D array representing the grid of A* nodes.
-
getIntersectedNodes
Gets the list of A* nodes that intersect with the specified rectangle.- Parameters:
rectangle- The rectangle to check for intersecting nodes.- Returns:
- A list of A* nodes that intersect with the specified rectangle.
-
getNeighbors
-
getNode
-
getNode
Gets the A* node at the specified coordinates.- Parameters:
x- The x-coordinate of the point.y- The y-coordinate of the point.- Returns:
- The A* node at the specified coordinates, or null if the coordinates are outside the grid.
-
getNodeSize
public int getNodeSize()Gets the size of each node in the grid.- Returns:
- The size of each node in the grid.
-
getSize
-
render
Description copied from interface:IRenderableRenders the visual contents of this instance onto the provided graphics context.
If an
Entityimplements this interface, this method will be called right after the entity was rendered from the environment. Allowing for a custom rendering mechanism.This interface can be implemented in general by anything that should be rendered to the game's screen.
- Specified by:
renderin interfaceIRenderable- Parameters:
g- The current graphics object onto which this instance will render its visual contents.- See Also:
-
setAllowDiagonalMovement
public void setAllowDiagonalMovement(boolean allowDiagonalMovement) Sets whether diagonal movement is allowed in the grid.- Parameters:
allowDiagonalMovement- True to allow diagonal movement; otherwise false.
-
setAllowCuttingCorners
public void setAllowCuttingCorners(boolean allowCuttingCorners) Sets whether cutting corners during diagonal movement is allowed in the grid.- Parameters:
allowCuttingCorners- True to allow cutting corners; otherwise false.
-
updateWalkable
Updates the walkable attribute of nodes intersected by the specified rectangle.- Parameters:
rectangle- The rectangle within which the nodes should be updated.
-
assignPenalty
Assigns a penalty to the specified A* node based on collisions and neighboring nodes.
If the node's location collides with a dynamic object, a penalty is calculated. The penalty is increased if the node intersects with indestructible props or has non-walkable neighbors.
- Parameters:
node- The A* node to which the penalty will be assigned.
-