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.

172 lines
5.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. local map_editor_state = {
  2. map = {},
  3. world = {},
  4. ui = {},
  5. selected_tile = false
  6. }
  7. local map_editor_scene_menu_options = {
  8. false,
  9. {
  10. texture = TEXTURES.NEW_GAME_HEX,
  11. action = function()
  12. win.scene:remove("menu")
  13. game_init()
  14. end
  15. },
  16. false,
  17. {
  18. texture = TEXTURES.SAVE_GAME_HEX,
  19. action = function()
  20. game_save()
  21. gui_alert("succesfully saved!")
  22. end
  23. },
  24. false,
  25. {
  26. texture = TEXTURES.LOAD_GAME_HEX,
  27. action = function()
  28. local save = am.load_state("save", "json")
  29. if save then
  30. win.scene:remove("menu")
  31. game_init(save)
  32. else
  33. gui_alert("no saved games")
  34. end
  35. end
  36. },
  37. {
  38. texture = TEXTURES.MAP_EDITOR_HEX,
  39. action = function()
  40. map_editor_init(game_state and game_state.map and game_state.map.seed)
  41. end
  42. },
  43. {
  44. texture = TEXTURES.UNPAUSE_HEX,
  45. action = function()
  46. win.scene:remove("menu")
  47. win.scene("context").paused = false
  48. end,
  49. keys = {
  50. "escape"
  51. }
  52. },
  53. {
  54. texture = TEXTURES.SETTINGS_HEX,
  55. action = function()
  56. gui_alert("not yet :)")
  57. end
  58. },
  59. {
  60. texture = TEXTURES.QUIT_HEX,
  61. action = function()
  62. win:close()
  63. end,
  64. keys = {
  65. "f4"
  66. }
  67. },
  68. false
  69. }
  70. local function deselect_tile()
  71. map_editor_state.selected_tile = false
  72. win.scene:remove("tile_select_box")
  73. end
  74. function map_editor_action()
  75. local mouse = win:mouse_position()
  76. local hex = pixel_to_hex(mouse - WORLDSPACE_COORDINATE_OFFSET, vec2(HEX_SIZE))
  77. local rounded_mouse = hex_to_pixel(hex, vec2(HEX_SIZE)) + WORLDSPACE_COORDINATE_OFFSET
  78. local evenq = hex_to_evenq(hex)
  79. local tile = hex_map_get(map_editor_state.map, hex)
  80. local interactable = evenq_is_in_interactable_region(evenq{ y = -evenq.y })
  81. if win:key_pressed"escape" then
  82. win.scene("map_editor").paused = true
  83. win.scene:append(make_scene_menu(map_editor_scene_menu_options, nil, true))
  84. end
  85. if win:mouse_down"left" then
  86. deselect_tile()
  87. map_editor_state.selected_tile = tile
  88. win.scene:append((
  89. am.translate(rounded_mouse)
  90. ^ pack_texture_into_sprite(TEXTURES.SELECT_BOX, HEX_SIZE*2, HEX_SIZE*2, COLORS.SUNRAY)
  91. )
  92. :tag"tile_select_box"
  93. )
  94. end
  95. if map_editor_state.selected_tile then
  96. if win:key_down"a" then
  97. -- make the selected tile 'mountain'
  98. map_editor_state.selected_tile.elevation = 0.75
  99. --map_editor_state.selected_tile.node("circle").color = map_elevation_color(map_editor_state.selected_tile.elevation)
  100. elseif win:key_down"w" then
  101. -- make the selected tile 'water'
  102. map_editor_state.selected_tile.elevation = -0.75
  103. --map_editor_state.selected_tile.node("circle").color = map_elevation_color(map_editor_state.selected_tile.elevation)
  104. elseif win:key_down"d" then
  105. -- make the selected tile 'dirt'
  106. map_editor_state.selected_tile.elevation = 0.25
  107. --map_editor_state.selected_tile.node("circle").color = map_elevation_color(map_editor_state.selected_tile.elevation)
  108. elseif win:key_down"g" then
  109. -- make the selected tile 'grass'
  110. map_editor_state.selected_tile.elevation = -0.25
  111. --map_editor_state.selected_tile.node("circle").color = map_elevation_color(map_editor_state.selected_tile.elevation)
  112. end
  113. -- fine tune tile's elevation with mouse wheel
  114. local mouse_wheel_delta = win:mouse_wheel_delta().y / 100
  115. if map_editor_state.selected_tile and mouse_wheel_delta ~= 0 then
  116. map_editor_state.selected_tile.elevation = math.clamp(map_editor_state.selected_tile.elevation + mouse_wheel_delta, -1, 1 - math.SMALLEST_NUMBER_ABOVE_0)
  117. --map_editor_state.selected_tile.node("circle").color = map_elevation_color(map_editor_state.selected_tile.elevation)
  118. end
  119. end
  120. -- update the cursor
  121. if not interactable then
  122. win.scene("cursor").hidden = true
  123. else
  124. win.scene("cursor").hidden = false
  125. win.scene("cursor_translate").position2d = rounded_mouse
  126. end
  127. if tile then
  128. win.scene("top_right_display").text = string.format(
  129. "raw elevation value: %.2f\ntile type: %s",
  130. tile.elevation, map_elevation_to_tile_type(tile.elevation)
  131. )
  132. end
  133. end
  134. function map_editor_init()
  135. -- remove existing map_editor scene from the graph if it's there
  136. local map_editor_scene = am.group():tag"map_editor"
  137. map_editor_scene:late_action(map_editor_action)
  138. map_editor_state.map = default_map_editor_map(1)
  139. map_editor_state.world = make_hex_grid_scene(map_editor_state.map, false)
  140. map_editor_state.ui = am.group(
  141. am.translate(HEX_GRID_CENTER):tag"cursor_translate"
  142. ^ make_hex_cursor_node(0, COLORS.TRANSPARENT):tag"cursor",
  143. make_top_right_display_node()
  144. )
  145. -- add the top level nodes to the scene
  146. map_editor_scene:append(map_editor_state.world)
  147. map_editor_scene:append(map_editor_state.ui)
  148. -- add the scene to the window
  149. switch_context(map_editor_scene)
  150. end