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.

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