Kotlin implementation of hexagonal grids using LibGDX.
Allows for grids of any size and shape - square, rectangular, triangular, or even irregular. Supports both pointy-top and flat-top hexes.
Based on Red Blob Games' fantastic guide on hexagonal grids.
Locations in the map are represented as CubeCoordinate
s. A CubeCoordinate
has many handy operations built-in, but knows nothing of the external map structure. These objects are immutable by design, but can readily be converted to several other types, such as OffsetCoordinate
s or vectors.
A HexagonalMap
consists of a series of CubeCoordinate
s mapped to HexagonalTile
s at those locations. The corresponding HexagonalLayout
and HexagonalOrientation
can be used to control the position and appearance of the tiles. A HexagonalTile
is composed of a location and a Terrain
, which defines rules for tiles such as movement cost and visibility.
A map can be drawn using a LayeredRenderer
. The renderer will draw any Layer
s provided. It will also handle culling the tiles that are drawn to the camera's viewable area to reduce unnecessary drawing. Some simple Layer
implementations are also included.
Create map
val tiles = (0 until rows).flatMap { row ->
(0 until columns).map { column ->
val coordinate = OffsetCoordinate(column, row, OffsetCoordinateType(HexagonalOrientation.POINTY_TOP)).toCubeCoordinate()
coordinate to HexagonalTile(coordinate, getTerrain(column, row))
}
}.toMap()
val map = HexagonalMap(layout, tiles)
Find all valid locations within 10 tiles of a location (CubeCoordinate
)
location.withinRange(10).filter { map.isValidLocation(it) }
Find all locations that are 3 tiles away from a location
location.ring(3)
Find locations that can be moved to at a movement cost of 5 or less
map.getReachableLocations(location, 5)
Find visible locations (i.e. tiles with unobstructed view) within 7 tiles of location
map.getVisibleLocations(location, 7)
Draw map to screen
val hexRenderer = HexagonalRenderer(map, Vector2(32, 32))
val renderer = LayeredRenderer(map, listOf(
TerrainLayer(map, hexRenderer, terrainViews),
TileOutlineLayer(hexRenderer, shapeRenderer)
), camera)
renderer.render(batch)
More documentation to come!