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.

207 lines
5.8 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
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 main_action(self)
  40. if win:key_pressed("escape") then
  41. if win.scene("game") then
  42. win.scene("game").paused = false
  43. win.scene:remove(self)
  44. else
  45. --win:close()
  46. end
  47. end
  48. if self"hex_backdrop" then
  49. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  50. end
  51. end
  52. function make_main_scene_toolbelt()
  53. local options = {
  54. false,
  55. false,
  56. false,
  57. {
  58. texture = TEXTURES.NEW_GAME_HEX,
  59. action = function()
  60. win.scene:remove"menu"
  61. game_init()
  62. end
  63. },
  64. false,
  65. {
  66. texture = TEXTURES.LOAD_GAME_HEX,
  67. action = function()
  68. win.scene:remove"menu"
  69. game_init(am.load_state("save", "json"))
  70. end
  71. },
  72. false,
  73. false,
  74. false,
  75. {
  76. texture = TEXTURES.MAP_EDITOR_HEX,
  77. action = function() alert("not yet :)") end
  78. },
  79. {
  80. texture = TEXTURES.SETTINGS_HEX,
  81. action = function() alert("not yet :)") end
  82. },
  83. {
  84. texture = TEXTURES.ABOUT_HEX,
  85. action = function() alert("not yet :)") end
  86. },
  87. false,
  88. {
  89. texture = TEXTURES.QUIT_HEX,
  90. action = function() win:close() end
  91. }
  92. }
  93. local spacing = 160
  94. -- calculate the dimensions of the whole grid
  95. local grid_width = 8
  96. local grid_height = 2
  97. local hhs = hex_horizontal_spacing(spacing)
  98. local hvs = hex_vertical_spacing(spacing)
  99. local grid_pixel_width = grid_width * hhs
  100. local grid_pixel_height = grid_height * hvs
  101. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  102. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  103. local group = am.group()
  104. local option_index = 1
  105. for i,_ in pairs(map) do
  106. for j,_ in pairs(map[i]) do
  107. local hex = vec2(i, j)
  108. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  109. local option = options[option_index]
  110. local texture = option and option.texture or TEXTURES.SHADED_HEX
  111. local color = option and COLORS.TRANSPARENT or vec4(0.3)
  112. local node = am.translate(position)
  113. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  114. hex_map_set(map, i, j, {
  115. node = node,
  116. option = option
  117. })
  118. local tile = hex_map_get(map, i, j)
  119. local selected = false
  120. node:action(function(self)
  121. local mouse = win:mouse_position()
  122. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  123. if tile.option then
  124. if hex == hex_ then
  125. if not selected then
  126. play_sfx(SOUNDS.SELECT1)
  127. end
  128. selected = true
  129. tile.node"sprite".color = vec4(1)
  130. if win:mouse_pressed("left") then
  131. tile.option.action()
  132. end
  133. else
  134. selected = false
  135. tile.node"sprite".color = COLORS.TRANSPARENT
  136. end
  137. end
  138. end)
  139. group:append(node)
  140. option_index = option_index + 1
  141. end
  142. end
  143. return am.translate(pixel_offset) ^ group
  144. end
  145. function main_scene(do_backdrop)
  146. local group = am.group()
  147. if do_backdrop then
  148. local map = hex_hexagonal_map(30)
  149. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  150. for i,_ in pairs(map) do
  151. for j,n in pairs(map[i]) do
  152. local color = map_elevation_color(n)
  153. color = color{a=color.a - 0.1}
  154. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  155. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  156. node"circle":action(am.tween(0.6, { color = color }))
  157. hex_backdrop:append(node)
  158. end
  159. end
  160. group:append(hex_backdrop)
  161. else
  162. group:append(am.rect(win.left, win.bottom, win.right, win.top, COLORS.TRANSPARENT))
  163. end
  164. group:append(
  165. am.translate(win.right - 10, win.bottom + 20)
  166. ^ am.text(version, COLORS.WHITE, "right")
  167. )
  168. local logo_height = 480
  169. group:append(am.translate(0, win.top - 20 - logo_height/2) ^ am.sprite("res/logo.png"))
  170. group:append(make_main_scene_toolbelt())
  171. group:action(main_action)
  172. return group:tag"menu"
  173. end
  174. win.scene = am.group(
  175. main_scene(true)
  176. )
  177. noglobals()