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.

231 lines
6.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
  1. settings = am.load_state("settings", "json") or {
  2. fullscreen = false,
  3. window_width = 1920,
  4. window_height = 1080,
  5. music_volume = 0.1,
  6. sfx_volume = 0.1,
  7. }
  8. math.randomseed(os.time())
  9. math.random()
  10. math.random()
  11. math.random()
  12. math.random()
  13. do
  14. win = am.window{
  15. width = settings.window_width,
  16. height = settings.window_height,
  17. title = "hexyz",
  18. mode = settings.fullscreen and "fullscreen" or "windowed",
  19. highdpi = true,
  20. letterbox = true,
  21. resizable = true, -- user should probably set their resolution instead of resizing the window, but hey.
  22. }
  23. end
  24. -- asset interfaces and/or trivial code
  25. require "conf"
  26. require "color"
  27. require "sound"
  28. require "texture"
  29. require "src/entity"
  30. require "src/extra"
  31. require "src/geometry"
  32. require "src/hexyz"
  33. require "src/game"
  34. require "src/gui"
  35. require "src/grid"
  36. require "src/mob"
  37. require "src/projectile"
  38. require "src/tower"
  39. function alert(message)
  40. win.scene:append(
  41. am.scale(3) ^ am.text(message)
  42. :action(coroutine.create(function(self)
  43. am.wait(am.tween(self, 1, { color = vec4(0) }))
  44. win.scene:remove(self)
  45. end))
  46. )
  47. end
  48. function main_action(self)
  49. if win:key_pressed("escape") then
  50. if win.scene("game") then
  51. win.scene("game").paused = false
  52. win.scene:remove(self)
  53. else
  54. --win:close()
  55. end
  56. end
  57. if self"hex_backdrop" then
  58. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  59. end
  60. end
  61. function make_main_scene_toolbelt()
  62. local include_save_option = game
  63. local options = {
  64. false,
  65. false,
  66. false,
  67. {
  68. texture = TEXTURES.NEW_GAME_HEX,
  69. action = function()
  70. win.scene:remove"menu"
  71. game_init()
  72. end
  73. },
  74. false,
  75. include_save_option and {
  76. texture = TEXTURES.SAVE_GAME_HEX,
  77. action = function()
  78. game_save()
  79. alert("succesfully saved!")
  80. end
  81. } or false,
  82. false,
  83. {
  84. texture = TEXTURES.LOAD_GAME_HEX,
  85. action = function()
  86. win.scene:remove"menu"
  87. game_init(am.load_state("save", "json"))
  88. end
  89. },
  90. false,
  91. {
  92. texture = TEXTURES.MAP_EDITOR_HEX,
  93. action = function() alert("not yet :)") end
  94. },
  95. {
  96. texture = TEXTURES.SETTINGS_HEX,
  97. action = function() alert("not yet :)") end
  98. },
  99. {
  100. texture = TEXTURES.ABOUT_HEX,
  101. action = function() alert("not yet :)") end
  102. },
  103. false,
  104. {
  105. texture = TEXTURES.QUIT_HEX,
  106. action = function() win:close() end
  107. }
  108. }
  109. local spacing = 160
  110. -- calculate the dimensions of the whole grid
  111. local grid_width = 8
  112. local grid_height = 2
  113. local hhs = hex_horizontal_spacing(spacing)
  114. local hvs = hex_vertical_spacing(spacing)
  115. local grid_pixel_width = grid_width * hhs
  116. local grid_pixel_height = grid_height * hvs
  117. -- @TODO the vertical offset should be different depending on if this is the main menu or the pause menu
  118. -- perhaps the map that makes the grid of hexes should be different as well
  119. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  120. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  121. local group = am.group()
  122. local option_index = 1
  123. for i,_ in pairs(map) do
  124. for j,_ in pairs(map[i]) do
  125. local hex = vec2(i, j)
  126. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  127. local option = options[option_index]
  128. local texture = option and option.texture or TEXTURES.SHADED_HEX
  129. local color = option and COLORS.TRANSPARENT or vec4(0.3)
  130. local node = am.translate(position)
  131. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  132. hex_map_set(map, i, j, {
  133. node = node,
  134. option = option
  135. })
  136. local tile = hex_map_get(map, i, j)
  137. local selected = false
  138. node:action(function(self)
  139. local mouse = win:mouse_position()
  140. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  141. if tile.option then
  142. if hex == hex_ then
  143. if not selected then
  144. play_sfx(SOUNDS.SELECT1)
  145. end
  146. selected = true
  147. tile.node"sprite".color = vec4(1)
  148. if win:mouse_pressed("left") then
  149. tile.option.action()
  150. end
  151. else
  152. selected = false
  153. tile.node"sprite".color = COLORS.TRANSPARENT
  154. end
  155. end
  156. end)
  157. group:append(node)
  158. option_index = option_index + 1
  159. end
  160. end
  161. return am.translate(pixel_offset) ^ group
  162. end
  163. function main_scene(do_backdrop, do_logo)
  164. local group = am.group()
  165. if do_backdrop then
  166. local map = hex_hexagonal_map(30)
  167. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  168. for i,_ in pairs(map) do
  169. for j,n in pairs(map[i]) do
  170. local color = map_elevation_color(n)
  171. color = color{a=color.a - 0.1}
  172. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  173. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  174. node"circle":action(am.tween(0.6, { color = color }))
  175. hex_backdrop:append(node)
  176. end
  177. end
  178. group:append(hex_backdrop)
  179. else
  180. group:append(am.rect(win.left, win.bottom, win.right, win.top, COLORS.TRANSPARENT))
  181. end
  182. group:append(
  183. am.translate(win.right - 10, win.bottom + 10)
  184. ^ am.text(string.format("v%s, by %s", version, author), COLORS.WHITE, "right", "bottom")
  185. )
  186. if do_logo then
  187. group:append(
  188. am.translate(0, win.top - 20 - TEXTURES.LOGO.height/2)
  189. ^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
  190. )
  191. end
  192. group:append(make_main_scene_toolbelt())
  193. group:action(main_action)
  194. return group:tag"menu"
  195. end
  196. win.scene = am.group(
  197. main_scene(true, true)
  198. )
  199. noglobals()