Class AStarGrid

All Implemented Interfaces:
IRenderable

public class AStarGrid extends Object implements 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 Details

    • PENALTY_STATIC_PROP

      public static final double PENALTY_STATIC_PROP
      The penalty value assigned to nodes that intersect with static, indestructible props.
      See Also:
    • PENALTY_NOT_WALKABLE_NEIGHBOR

      public static final double PENALTY_NOT_WALKABLE_NEIGHBOR
      The 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

      public AStarGrid(Dimension size, int nodeSize)
      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

      public AStarNode[][] getGrid()
      Gets the grid of A* nodes.
      Returns:
      A 2D array representing the grid of A* nodes.
    • getIntersectedNodes

      public List<AStarNode> getIntersectedNodes(Rectangle2D rectangle)
      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

      public List<AStarNode> getNeighbors(AStarNode node)
      Gets the list of neighboring A* nodes for the specified node.
      Parameters:
      node - The node for which to get the neighbors.
      Returns:
      A list of neighboring A* nodes.
    • getNode

      public AStarNode getNode(Point2D point)
      Gets the A* node at the specified point.
      Parameters:
      point - The point for which to get the corresponding A* node.
      Returns:
      The A* node at the specified point, or null if the point is outside the grid.
    • getNode

      public AStarNode getNode(double x, double y)
      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

      public Dimension getSize()
      Gets the dimension of the grid.
      Returns:
      The dimension of the grid.
    • render

      public void render(Graphics2D g)
      Description copied from interface: IRenderable

      Renders the visual contents of this instance onto the provided graphics context.

      If an Entity implements 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:
      render in interface IRenderable
      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

      public void updateWalkable(Rectangle2D rectangle)
      Updates the walkable attribute of nodes intersected by the specified rectangle.
      Parameters:
      rectangle - The rectangle within which the nodes should be updated.
    • assignPenalty

      protected void assignPenalty(AStarNode node)

      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.