Class Hexagon

  • All Implemented Interfaces:
    javafx.css.Styleable, javafx.event.EventTarget

    public class Hexagon
    extends javafx.scene.shape.Polygon
    A Hexagon is the building block of the grid.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      GridPosition position  
      • Fields inherited from class javafx.scene.Node

        BASELINE_OFFSET_SAME_AS_HEIGHT
    • Constructor Summary

      Constructors 
      Constructor Description
      Hexagon​(int q, int r)
      The position of the Hexagon is specified with axial coordinates
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void draw​(javafx.scene.canvas.GraphicsContext gc)  
      void drawContour​(javafx.scene.canvas.GraphicsContext gc)  
      void drawHexagon​(javafx.scene.canvas.GraphicsContext gc)  
      void drawHexagon​(javafx.scene.canvas.GraphicsContext gc, HexagonMap.Direction... directions)  
      boolean equals​(java.lang.Object obj)
      Two Hexagons are equal if they have the same q and r
      HexagonMap.Direction getDirectionTo​(Hexagon target)
      Finds the direction (NORTHWEST, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST or WEST) If target is a neighbour, then it is quite simple.
      int getDistance​(Hexagon target)
      Calculates the distance (number of hexagons) to the target hexagon
      int getGraphicsXoffset()  
      int getGraphicsYoffset()  
      java.util.List<Hexagon> getHexagonsInRingArea​(int radius)
      Returns all Hexagons that are located within a certain distance from here
      java.util.List<Hexagon> getHexagonsOnRingEdge​(int radius)
      Returns all Hexagons that are located a certain distance from here
      java.util.List<Hexagon> getLine​(Hexagon origin, Hexagon destination)
      Finds all Hexagons that are on a line between this and destination
      Hexagon getNeighbour​(HexagonMap.Direction direction)
      Finds the neighbour of this Hexagon
      java.util.List<Hexagon> getNeighbours()
      Finds all neighbours of this Hexagon
      java.util.List<Hexagon> getPathTo​(Hexagon destination)
      Finds the cheapest path from here to the destination.
      java.util.List<Hexagon> getPathTo​(Hexagon destination, IPathInfoSupplier pathInfoSupplier)
      Finds the cheapest path from start to the goal.
      int getQ()  
      int getR()  
      java.util.List<Hexagon> getVisibleHexes​(int visibleRange)
      Calculates all Hexagons that are visible from this Hexagon.
      void init()  
      boolean isBlockingPath()
      This affects the pathfinding calculations
      boolean isVisualObstacle()
      This affects the field of view calculations.
      void renderCoordinates​(javafx.scene.canvas.GraphicsContext gc)  
      void setBackgroundColor​(javafx.scene.paint.Color c)
      This method is the safe way to change the background color since it makes sure that the change is made on the JavaFX Application thread.
      void setIsBlockingPath​(boolean b)
      This affects the pathfinding calculations.
      void setIsVisualObstacle​(boolean b)
      This affects the field of view calculations.
      void setMap​(HexagonMap map)
      This gives the Hexagon access a HexagonMap without actually adding it to the HexagonMap.
      java.lang.String toString()  
      • Methods inherited from class javafx.scene.shape.Polygon

        getPoints
      • Methods inherited from class javafx.scene.shape.Shape

        fillProperty, getClassCssMetaData, getCssMetaData, getFill, getStroke, getStrokeDashArray, getStrokeDashOffset, getStrokeLineCap, getStrokeLineJoin, getStrokeMiterLimit, getStrokeType, getStrokeWidth, intersect, isSmooth, setFill, setSmooth, setStroke, setStrokeDashOffset, setStrokeLineCap, setStrokeLineJoin, setStrokeMiterLimit, setStrokeType, setStrokeWidth, smoothProperty, strokeDashOffsetProperty, strokeLineCapProperty, strokeLineJoinProperty, strokeMiterLimitProperty, strokeProperty, strokeTypeProperty, strokeWidthProperty, subtract, union
      • Methods inherited from class javafx.scene.Node

        accessibleHelpProperty, accessibleRoleDescriptionProperty, accessibleRoleProperty, accessibleTextProperty, addEventFilter, addEventHandler, applyCss, autosize, blendModeProperty, boundsInLocalProperty, boundsInParentProperty, buildEventDispatchChain, cacheHintProperty, cacheProperty, clipProperty, computeAreaInScreen, contains, contains, cursorProperty, depthTestProperty, disabledProperty, disableProperty, effectiveNodeOrientationProperty, effectProperty, eventDispatcherProperty, executeAccessibleAction, fireEvent, focusedProperty, focusTraversableProperty, getAccessibleHelp, getAccessibleRole, getAccessibleRoleDescription, getAccessibleText, getBaselineOffset, getBlendMode, getBoundsInLocal, getBoundsInParent, getCacheHint, getClip, getContentBias, getCursor, getDepthTest, getEffect, getEffectiveNodeOrientation, getEventDispatcher, getId, getInitialCursor, getInitialFocusTraversable, getInputMethodRequests, getLayoutBounds, getLayoutX, getLayoutY, getLocalToParentTransform, getLocalToSceneTransform, getNodeOrientation, getOnContextMenuRequested, getOnDragDetected, getOnDragDone, getOnDragDropped, getOnDragEntered, getOnDragExited, getOnDragOver, getOnInputMethodTextChanged, getOnKeyPressed, getOnKeyReleased, getOnKeyTyped, getOnMouseClicked, getOnMouseDragEntered, getOnMouseDragExited, getOnMouseDragged, getOnMouseDragOver, getOnMouseDragReleased, getOnMouseEntered, getOnMouseExited, getOnMouseMoved, getOnMousePressed, getOnMouseReleased, getOnRotate, getOnRotationFinished, getOnRotationStarted, getOnScroll, getOnScrollFinished, getOnScrollStarted, getOnSwipeDown, getOnSwipeLeft, getOnSwipeRight, getOnSwipeUp, getOnTouchMoved, getOnTouchPressed, getOnTouchReleased, getOnTouchStationary, getOnZoom, getOnZoomFinished, getOnZoomStarted, getOpacity, getParent, getProperties, getPseudoClassStates, getRotate, getRotationAxis, getScaleX, getScaleY, getScaleZ, getScene, getStyle, getStyleableParent, getStyleClass, getTransforms, getTranslateX, getTranslateY, getTranslateZ, getTypeSelector, getUserData, getViewOrder, hasProperties, hoverProperty, idProperty, inputMethodRequestsProperty, intersects, intersects, isCache, isDisable, isDisabled, isFocused, isFocusTraversable, isHover, isManaged, isMouseTransparent, isPickOnBounds, isPressed, isResizable, isVisible, layoutBoundsProperty, layoutXProperty, layoutYProperty, localToParent, localToParent, localToParent, localToParent, localToParent, localToParentTransformProperty, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToSceneTransformProperty, localToScreen, localToScreen, localToScreen, localToScreen, localToScreen, lookup, lookupAll, managedProperty, maxHeight, maxWidth, minHeight, minWidth, mouseTransparentProperty, nodeOrientationProperty, notifyAccessibleAttributeChanged, onContextMenuRequestedProperty, onDragDetectedProperty, onDragDoneProperty, onDragDroppedProperty, onDragEnteredProperty, onDragExitedProperty, onDragOverProperty, onInputMethodTextChangedProperty, onKeyPressedProperty, onKeyReleasedProperty, onKeyTypedProperty, onMouseClickedProperty, onMouseDragEnteredProperty, onMouseDragExitedProperty, onMouseDraggedProperty, onMouseDragOverProperty, onMouseDragReleasedProperty, onMouseEnteredProperty, onMouseExitedProperty, onMouseMovedProperty, onMousePressedProperty, onMouseReleasedProperty, onRotateProperty, onRotationFinishedProperty, onRotationStartedProperty, onScrollFinishedProperty, onScrollProperty, onScrollStartedProperty, onSwipeDownProperty, onSwipeLeftProperty, onSwipeRightProperty, onSwipeUpProperty, onTouchMovedProperty, onTouchPressedProperty, onTouchReleasedProperty, onTouchStationaryProperty, onZoomFinishedProperty, onZoomProperty, onZoomStartedProperty, opacityProperty, parentProperty, parentToLocal, parentToLocal, parentToLocal, parentToLocal, parentToLocal, pickOnBoundsProperty, prefHeight, prefWidth, pressedProperty, pseudoClassStateChanged, queryAccessibleAttribute, relocate, removeEventFilter, removeEventHandler, requestFocus, resize, resizeRelocate, rotateProperty, rotationAxisProperty, scaleXProperty, scaleYProperty, scaleZProperty, sceneProperty, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, screenToLocal, screenToLocal, screenToLocal, setAccessibleHelp, setAccessibleRole, setAccessibleRoleDescription, setAccessibleText, setBlendMode, setCache, setCacheHint, setClip, setCursor, setDepthTest, setDisable, setDisabled, setEffect, setEventDispatcher, setEventHandler, setFocused, setFocusTraversable, setHover, setId, setInputMethodRequests, setLayoutX, setLayoutY, setManaged, setMouseTransparent, setNodeOrientation, setOnContextMenuRequested, setOnDragDetected, setOnDragDone, setOnDragDropped, setOnDragEntered, setOnDragExited, setOnDragOver, setOnInputMethodTextChanged, setOnKeyPressed, setOnKeyReleased, setOnKeyTyped, setOnMouseClicked, setOnMouseDragEntered, setOnMouseDragExited, setOnMouseDragged, setOnMouseDragOver, setOnMouseDragReleased, setOnMouseEntered, setOnMouseExited, setOnMouseMoved, setOnMousePressed, setOnMouseReleased, setOnRotate, setOnRotationFinished, setOnRotationStarted, setOnScroll, setOnScrollFinished, setOnScrollStarted, setOnSwipeDown, setOnSwipeLeft, setOnSwipeRight, setOnSwipeUp, setOnTouchMoved, setOnTouchPressed, setOnTouchReleased, setOnTouchStationary, setOnZoom, setOnZoomFinished, setOnZoomStarted, setOpacity, setPickOnBounds, setPressed, setRotate, setRotationAxis, setScaleX, setScaleY, setScaleZ, setStyle, setTranslateX, setTranslateY, setTranslateZ, setUserData, setViewOrder, setVisible, snapshot, snapshot, startDragAndDrop, startFullDrag, styleProperty, toBack, toFront, translateXProperty, translateYProperty, translateZProperty, usesMirroring, viewOrderProperty, visibleProperty
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface javafx.css.Styleable

        getStyleableNode
    • Constructor Detail

      • Hexagon

        public Hexagon​(int q,
                       int r)
        The position of the Hexagon is specified with axial coordinates
        Parameters:
        q - the Q coordinate
        r - the R coordinate
    • Method Detail

      • init

        public void init()
      • getQ

        public int getQ()
        Returns:
        axial Q-value
      • getR

        public int getR()
        Returns:
        axial R-value
      • setIsVisualObstacle

        public void setIsVisualObstacle​(boolean b)
        This affects the field of view calculations. If true, the hexagons behind this hexagon cannot be seen (but this hexagon can still be seen).
        Parameters:
        b - true: opaque hexagon
      • isVisualObstacle

        public boolean isVisualObstacle()
        This affects the field of view calculations.
        Returns:
        If true, the hexagons behind this hexagon cannot be seen (but this hexagon can still be seen).
      • setIsBlockingPath

        public void setIsBlockingPath​(boolean b)
        This affects the pathfinding calculations. If true, the algorithm will try to find a path around this Hexagon. If you want to have more control over this, you can supply your own class implementing IPathInfoSupplier to the pathfinding method.
        Parameters:
        b - true: blocking hexagon
      • isBlockingPath

        public boolean isBlockingPath()
        This affects the pathfinding calculations
        Returns:
        true if this is an obstacle that blocks the path
      • getGraphicsXoffset

        public int getGraphicsXoffset()
        Returns:
        where this Hexagon is when rendererd into a JavaFX Group
      • getGraphicsYoffset

        public int getGraphicsYoffset()
        Returns:
        where this Hexagon is when rendererd into a JavaFX Group
      • setBackgroundColor

        public void setBackgroundColor​(javafx.scene.paint.Color c)
        This method is the safe way to change the background color since it makes sure that the change is made on the JavaFX Application thread.
        Parameters:
        c - the color
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class javafx.scene.shape.Polygon
      • getDirectionTo

        public HexagonMap.Direction getDirectionTo​(Hexagon target)
        Finds the direction (NORTHWEST, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST or WEST) If target is a neighbour, then it is quite simple. If target is not a neighbour, this returns the direction to the first step on a line to the target.
        Parameters:
        target - target hexagon
        Returns:
        direction towards target
      • getHexagonsOnRingEdge

        public java.util.List<Hexagon> getHexagonsOnRingEdge​(int radius)
        Returns all Hexagons that are located a certain distance from here
        Parameters:
        radius - in hex grid coordinates
        Returns:
        list of all hexagon within the circle
      • getHexagonsInRingArea

        public java.util.List<Hexagon> getHexagonsInRingArea​(int radius)
        Returns all Hexagons that are located within a certain distance from here
        Parameters:
        radius - in hex grid coordinates
        Returns:
        list of all hexagon on the circle radius
      • getNeighbour

        public Hexagon getNeighbour​(HexagonMap.Direction direction)
        Finds the neighbour of this Hexagon
        Parameters:
        direction - direction from source
        Returns:
        neighbour
      • getNeighbours

        public java.util.List<Hexagon> getNeighbours()
        Finds all neighbours of this Hexagon
        Returns:
        list of all direct neighbours
      • getPathTo

        public java.util.List<Hexagon> getPathTo​(Hexagon destination,
                                                 IPathInfoSupplier pathInfoSupplier)
                                          throws NoPathFoundException
        Finds the cheapest path from start to the goal. The A* algorithm is used.
        Parameters:
        destination - the target Hexagon
        pathInfoSupplier - a class implementing the IPathInfoSupplier interface. This can be used to add inpassable hexagons and customize the movement costs.
        Returns:
        an array of Hexagons, sorted so that the first step comes first.
        Throws:
        NoPathFoundException - if there exists no path between start and the goal
      • getPathTo

        public java.util.List<Hexagon> getPathTo​(Hexagon destination)
                                          throws NoPathFoundException
        Finds the cheapest path from here to the destination. The A* algorithm is used. This method uses the method isBlockingPath() in Hexagon and the movement cost between neighboring hexagons is always 1.
        Parameters:
        destination - the target Hexagon
        Returns:
        an array of Hexagons, sorted so that the first step comes first.
        Throws:
        NoPathFoundException - if there exists no path between start and the goal
      • getLine

        public java.util.List<Hexagon> getLine​(Hexagon origin,
                                               Hexagon destination)
        Finds all Hexagons that are on a line between this and destination
        Parameters:
        origin - source hex tile
        destination - target hex tile
        Returns:
        list of hexagon on the path
      • getVisibleHexes

        public java.util.List<Hexagon> getVisibleHexes​(int visibleRange)
        Calculates all Hexagons that are visible from this Hexagon. The line of sight can be blocked by Hexagons that has isVisualObstacle == true. NOTE: Accuracy is not guaranteed!
        Parameters:
        visibleRange - a limit of how long distance can be seen assuming there are no obstacles
        Returns:
        an array of Hexagons that are visible
      • getDistance

        public int getDistance​(Hexagon target)
        Calculates the distance (number of hexagons) to the target hexagon
        Parameters:
        target - destination hex tile
        Returns:
        distance in hex grid coordinates
      • equals

        public boolean equals​(java.lang.Object obj)
        Two Hexagons are equal if they have the same q and r
        Overrides:
        equals in class java.lang.Object
      • setMap

        public void setMap​(HexagonMap map)
        This gives the Hexagon access a HexagonMap without actually adding it to the HexagonMap. It can be useful e.g. if you want to make some calculations before creating another Hexagon.
        Parameters:
        map - global map reference
      • drawHexagon

        public void drawHexagon​(javafx.scene.canvas.GraphicsContext gc)
      • drawHexagon

        public void drawHexagon​(javafx.scene.canvas.GraphicsContext gc,
                                HexagonMap.Direction... directions)
      • draw

        public void draw​(javafx.scene.canvas.GraphicsContext gc)
      • drawContour

        public void drawContour​(javafx.scene.canvas.GraphicsContext gc)
      • renderCoordinates

        public void renderCoordinates​(javafx.scene.canvas.GraphicsContext gc)