hexyz is tower defense game, and a lua library for dealing with hexagonal grids
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

  1. --[[ AXIAL/CUBE COORDINATE SYSTEM FOR AMULET/LUA]]
  2. --[[
  3. all hexes in functions are assumed to be amulet vectors.
  4. in amulet, vector arithmetic works already with [ + - * / ]
  5. things like equality and distance are implemented here.
  6. some algorithms use axial coordinates for hexes: vec2(s, t)
  7. others use cube coordinates: vec3(s, t, z) where s + t + z = 0
  8. this is for simplicity - many algorithms don't care about the
  9. third coordinate, and if they do, the missing coordinate can
  10. be calculated from the other two.
  11. -- note on orientation:
  12. because of the way amulet draws hexagons, it's much easier to assume
  13. the user wants to use the flat map. rotation after the fact to
  14. achieve other orienations is probably possible, but might have some
  15. aliasing issues. TODO work on this.
  16. some of the primary resources used to develop this library:
  17. - https://redblobgames.com/grid/hexagons - simply amazing.
  18. - http://amulet.xyz/doc - amulet documentation
  19. - TODO that place that had the inner circle/outer circle ratio??
  20. ]]
  21. -- GENERALLY USEFUL FUNCTIONS --------------------------------------------------
  22. function round(n)
  23. return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
  24. end
  25. -- HEX CONSTANTS ---------------------------------------------------------------
  26. -- all possible vector directions from a given hex by edge
  27. HEX_DIRECTIONS = {vec2( 1 , 0),
  28. vec2( 1 , -1),
  29. vec2( 0 , -1),
  30. vec2(-1 , 0),
  31. vec2(-1 , 1),
  32. vec2( 0 , 1)}
  33. -- HEX UTILITY FUNCTIONS -------------------------------------------------------
  34. function hex_equals(a, b)
  35. return a.s == a.t and b.s == b.t
  36. end
  37. function hex_nequals(a, b)
  38. return not hex_equals(a, b)
  39. end
  40. function hex_length(hex)
  41. return ((math.abs(hex.s) + math.abs(hex.t) + math.abs(-hex.s - hex.t)) / 2)
  42. end
  43. function hex_distance(a, b)
  44. return hex_length(a - b)
  45. end
  46. function hex_direction(direction)
  47. return HEX_DIRECTIONS[direction]
  48. end
  49. function hex_neighbour(hex, direction)
  50. return hex + HEX_DIRECTIONS[direction]
  51. end
  52. function hex_round(hex)
  53. rs = round(hex.s)
  54. rt = round(hex.t)
  55. rz = round(-hex.s + -hex.t)
  56. sdelta = math.abs(rs - hex.s)
  57. tdelta = math.abs(rt - hex.t)
  58. zdelta = math.abs(rz + hex.s + hex.t)
  59. if sdelta > tdelta and sdelta > zdelta then
  60. rs = -rt - rz
  61. elseif tdelta > zdelta then
  62. rt = -rs - rz
  63. else
  64. rz = -rs - rt
  65. end
  66. return vec2(rs, rt)
  67. end
  68. -- COORDINATE CONVERSION FUNCTIONS ---------------------------------------------
  69. -- forward & inverse matrices used for coordinate conversion
  70. local M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 )
  71. local W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0)
  72. -- hex to screen
  73. function hex_to_pixel(hex)
  74. x = (M[1][1] * hex.s + M[1][2] * hex.t) * map.size
  75. y = (M[2][1] * hex.s + M[2][2] * hex.t) * map.size
  76. return vec2(x + map.origin.x, y + map.origin.y)
  77. end
  78. -- screen to hex
  79. function pixel_to_hex(pix)
  80. pix = vec2(pix.x - map.origin.x) / map.size,
  81. (pix.y - map.origin.y) / map.size
  82. s = W[1][1] * pix.x + W[1][2] * pix.y
  83. t = W[2][1] * pix.x + W[2][2] * pix.y
  84. return hex_round(vec2(s, t))
  85. end
  86. -- MAP FUNCTIONS ---------------------------------------------------------------