MODULE: Hexagons

Started by fernewelten, Tue 07/04/2020 04:46:29

Previous topic - Next topic

fernewelten

Hello,

I've used the last lockdown days to learn about grids of hexagonal tiles, and I've made a module out of it -- mainly for my own use, in minigames and such. If you think it useful, you're free to use it as you like. I've put the module under a CC-BY license.

Summary
This AGS module provides a framework for managing grids of hexagonal tiles.

  • Define a grid in arbitrary shape. Shortcuts for defining rectangular and hexagonal grids.
  • The tiles may be squashed.
  • Assign each tile on the grid a unique index (tile number) â€" so that you can keep the tile information in a (one-dimensional) dynamic array
  • Get the co-ordinates that correspond to a tile index.
  • Get the position of the centre of any tile -- so that you can position ,e.g., an object to it.
  • Convert the mouse position to the tile that the mouse is over.
  • Draw tile outlines onto a DrawingSurface., e.g., the room background.
  • Fill tiles with any colour (realized by drawing onto a DrawingSurface).
  • Print a sprite into the centre of a tile (onto a DrawingSurface).
  • Convert pixel co-ordinates (e.g., mouse co-ordinates) to Cube co-ordinates and Cube co-
    ordinates to the pixel co-ordinates of the centre of the tile.
  • Loop through a grid along any grid dimensions.
  • Convert from Rectangular co-ordinates to Cube (Axial) co-ordinates and vice versa.

Main limitation: Hexagonal grids have tiles with either “pointy tops” or “flat tops” -- I've only implemented flat tops so far.

The module is designed for AGS 3.5. You'll probably be able to make it work in AGS 3.x if you provide a declaration for managed struct Point { int x, y; }.

I'm providing documentation as well as a demo AGS program that uses these features. The program features some simple games and a maze generator for hex mazes.



Get the module here: http://www.loquimur-online.de/ags/Hexagons/Hexagons-1.01.zip
(The module proper and the documentation are in the Assets subdirectory, as stated in the README file.)

Crimson Wizard

This will definitely come useful.
Are you planning on making a module for isometric grid? Then it could be bundled with regular rectangle grid and form 3 grid types modules pack :).

Ponch

Awesome. I respect the amount of work that went into this. Many years ago, I tried something similar to build a DRADIS combat game set in the Battlestar Galactica early 2000s remake series. I got it to work, finally, but the code was so kludged together that I will take that digital spaghetti to my grave!  :=

Thanks for sharing!

fernewelten

#3
I've just uploaded version 1.01 of the Hexagons module.

  • Provide explicit function Hx_GetTileUnderMouse()
  • Minor fixes to the documentation

Get the new version here: http://www.loquimur-online.de/ags/Hexagons/Hexagons-1.01.zip

SMF spam blocked by CleanTalk