Skip to content

musniro/Godot_Hex_Gridmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Godot_Hex_Gridmap

This repo contains the code to support Hexagonal gridmaps in godot. It also contains a demo in the form of a godot project with scene 'HexMapd3d_test.tscn'

HexMap interface

HexMap functions:

  • get_distance_between(a,b) : get distance between mappos a and b
  • set_obj_pos_HEX(object,mappos) : move object to the given mappos
  • world_to_map_HEX(pos: Vector3)
  • map_to_world_HEX(mappos: Vector2)
  • has_tile_HEX(mappos: Vector2)
  • get_cell_HEX(x : int, y : int)
  • set_cell_HEX(x: int, y: int, tile_int: int)

These last five are the interfaces to the GridMap functions. The HexMap script corrects the coordinate system for you to the following usable format:

image

HexMap signals

  • on_hover
  • on_left_click

Camera functions:

  • get_mousepos3d(mouse_pos : Vector2)

How to use hextiles in the 3D Gridmap in Godot

We are going to create the following structure

image

First create the camera and add the Camera.gd script. This script supports getting the 3d position from the mouse position

Create a Gridmap with property Cell>Size (1,0.2,1.732) as shown below. This works at least for kaykit's hextiles. Give it a pointer to the camera node and mesh library to use and add the HexMap script to the node.

image

Instance the UI_info scene as a child of the gridmap, this is a way to show information about the tiles. For now it shows the coorinates.

Now connect the on_hover and on_left_click signals from HexMaps. For now connect on_hover to UI_info, and on_left_click to HexMap itself.

image

Known limitations

For now the HexMap can only correctly support tiles on level 0. The camera get_mousepos3d returns projection of the mouse position onto a flat plane at height 0. This correlates okay with a tilemap as long as it is also at height 0. We use a 'hack' in the tilemap size to allow us to place tiles at 'half offsets' (somewhat similar to Godot Tilemaps in 2d). The result of this is that it possibly to start the tiling incorrectly. The simplest way to test this is to run the scene and check if the coordinates make sense. If this is not the case you must move the tiles by one of these half offsets. (This is diffult to describe in words I guess)

About

How to use hextiles in the 3D Gridmap in Godot

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published