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.

306 lines
9.0 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
3 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
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. -- @TODO @TODO @TODO @TODO @TODO
  2. -- settings menu
  3. -- -- music volume
  4. -- -- sfx volume
  5. -- -- resolution settings & fix grid size being based on resolution
  6. --
  7. -- serialization
  8. -- -- allow saving by name
  9. -- -- allow loading by name
  10. -- -- investigate saving as lua instead, and having as a consequence a less janky map serialization
  11. --
  12. -- map editor
  13. -- -- paint terrain elevation levels
  14. -- -- place tiles of set elevation
  15. -- -- place towers
  16. -- -- move home?
  17. --
  18. -- game
  19. -- -- HEX_GRID_CENTER =/= HOME
  20. -- -- allow selecting of tiles, if tower is selected then allow sell/upgrade
  21. -- -- button/ui to open pause menu - make it obvious that 'esc' is the button
  22. -- -- play the game and tweak numbers
  23. -- -- new game menu allowing set seed
  24. -- -- gattling gun tower (fast fire rate)
  25. -- -- spooder mob
  26. -- -- make art, birds-eye-ify the redeye tower and lighthouse maybe?
  27. -- aspect ratios seem like a huge mess
  28. -- for now, i think we should enforce 4:3
  29. local RESOLUTION_OPTIONS = {
  30. -- 16:9
  31. -- { width = 1776, height = 1000 },
  32. -- { width = 1920, height = 1080 },
  33. -- { width = 1600, height = 900 },
  34. -- 4:3
  35. { width = 1440, height = 1080 },
  36. { width = 1400, height = 1050 }, -- seems like a good one
  37. { width = 1280, height = 960 },
  38. { width = 1152, height = 864 },
  39. { width = 1024, height = 768 },
  40. { width = 960, height = 720 },
  41. { width = 832, height = 624 },
  42. { width = 800, height = 600 },
  43. }
  44. settings = am.load_state("settings", "json") or {
  45. fullscreen = false,
  46. window_width = 1400,
  47. window_height = 1050,
  48. music_volume = 0.2,
  49. sfx_volume = 0.1,
  50. }
  51. math.randomseed(os.time())
  52. math.random()
  53. math.random()
  54. math.random()
  55. math.random()
  56. do
  57. win = am.window{
  58. width = settings.window_width,
  59. height = settings.window_height,
  60. title = "hexyz",
  61. mode = settings.fullscreen and "fullscreen" or "windowed",
  62. resizable = true,
  63. highdpi = true,
  64. --resizable = true, -- user should probably set their resolution instead of resizing the window, but hey.
  65. letterbox = true,
  66. show_cursor = true,
  67. }
  68. end
  69. -- asset interfaces and/or trivial code
  70. require "conf"
  71. require "color"
  72. require "sound"
  73. require "texture"
  74. require "src/entity"
  75. require "src/extra"
  76. require "src/geometry"
  77. require "src/hexyz"
  78. require "src/game"
  79. require "src/gui"
  80. require "src/grid"
  81. require "src/mob"
  82. require "src/projectile"
  83. require "src/tower"
  84. -- text popup in the middle of the screen that dissapates, call from anywhere
  85. function alert(message, color)
  86. win.scene:append(
  87. am.scale(3) ^ am.text(message, color or COLORS.WHITE)
  88. :action(coroutine.create(function(self)
  89. am.wait(am.tween(self, 1, { color = vec4(0) }, am.ease_out))
  90. win.scene:remove(self)
  91. end))
  92. )
  93. end
  94. function unpause(root_node)
  95. win.scene("game").paused = false
  96. win.scene:remove(root_node)
  97. end
  98. function main_action(self)
  99. if win:key_pressed("escape") then
  100. if game then
  101. unpause(self)
  102. else
  103. --win:close()
  104. end
  105. elseif win:key_pressed("f4") then
  106. win:close()
  107. end
  108. if self"hex_backdrop" then
  109. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  110. end
  111. end
  112. function make_main_scene_toolbelt()
  113. local include_save_option = game
  114. local include_unpause_option = game
  115. local options = {
  116. false,
  117. {
  118. texture = TEXTURES.NEW_GAME_HEX,
  119. action = function()
  120. win.scene:remove"menu"
  121. game_init()
  122. end
  123. },
  124. false,
  125. include_save_option and {
  126. texture = TEXTURES.SAVE_GAME_HEX,
  127. action = function()
  128. game_save()
  129. alert("succesfully saved!")
  130. end
  131. } or false,
  132. false,
  133. {
  134. texture = TEXTURES.LOAD_GAME_HEX,
  135. action = function()
  136. local save = am.load_state("save", "json")
  137. if save then
  138. win.scene:remove("menu")
  139. game_init(save)
  140. else
  141. alert("no saved games")
  142. end
  143. end
  144. },
  145. {
  146. texture = TEXTURES.MAP_EDITOR_HEX,
  147. action = function() alert("not yet :)") end
  148. },
  149. include_unpause_option and {
  150. texture = TEXTURES.UNPAUSE_HEX,
  151. action = function() unpause(win.scene("menu")) end
  152. } or false,
  153. {
  154. texture = TEXTURES.SETTINGS_HEX,
  155. action = function() alert("not yet :)") end
  156. },
  157. {
  158. texture = TEXTURES.QUIT_HEX,
  159. action = function() win:close() end
  160. },
  161. false
  162. }
  163. -- calculate the dimensions of the whole grid
  164. local spacing = 150
  165. local grid_width = 6
  166. local grid_height = 2
  167. local hhs = hex_horizontal_spacing(spacing)
  168. local hvs = hex_vertical_spacing(spacing)
  169. local grid_pixel_width = grid_width * hhs
  170. local grid_pixel_height = grid_height * hvs
  171. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  172. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  173. local group = am.group()
  174. local option_index = 1
  175. for i,_ in pairs(map) do
  176. for j,_ in pairs(map[i]) do
  177. local hex = vec2(i, j)
  178. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  179. local option = options[option_index]
  180. local texture = option and option.texture or TEXTURES.SHADED_HEX
  181. local color = option and COLORS.TRANSPARENT or vec4(0.3)
  182. local node = am.translate(position)
  183. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  184. hex_map_set(map, i, j, {
  185. node = node,
  186. option = option
  187. })
  188. local tile = hex_map_get(map, i, j)
  189. local selected = false
  190. node:action(function(self)
  191. local mouse = win:mouse_position()
  192. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  193. if tile.option then
  194. if hex == hex_ then
  195. if not selected then
  196. play_sfx(SOUNDS.SELECT1)
  197. end
  198. selected = true
  199. tile.node"sprite".color = vec4(1)
  200. if win:mouse_pressed("left") then
  201. tile.option.action()
  202. end
  203. else
  204. selected = false
  205. tile.node"sprite".color = COLORS.TRANSPARENT
  206. end
  207. end
  208. end)
  209. group:append(node)
  210. option_index = option_index + 1
  211. end
  212. end
  213. return am.translate(pixel_offset) ^ group
  214. end
  215. function main_scene(do_backdrop, do_logo)
  216. local group = am.group()
  217. if do_backdrop then
  218. local map = hex_hexagonal_map(30)
  219. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  220. for i,_ in pairs(map) do
  221. for j,n in pairs(map[i]) do
  222. local color = map_elevation_color(n)
  223. color = color{a=color.a - 0.1}
  224. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  225. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  226. node"circle":action(am.tween(0.6, { color = color }))
  227. hex_backdrop:append(node)
  228. end
  229. end
  230. group:append(hex_backdrop)
  231. else
  232. group:append(
  233. pack_texture_into_sprite(TEXTURES.CURTAIN, win.width, win.height)
  234. )
  235. end
  236. group:append(
  237. am.translate(win.right - 10, win.bottom + 10)
  238. ^ am.text(string.format("v%s, by %s", version, author), COLORS.WHITE, "right", "bottom")
  239. )
  240. if do_logo then
  241. local position = vec2(0, win.top - 20 - TEXTURES.LOGO.height/2)
  242. local logo =
  243. am.translate(position)
  244. ^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
  245. local selected = false
  246. logo:action(function(self)
  247. local mouse = win:mouse_position()
  248. if math.distance(mouse, position) < TEXTURES.LOGO.height/2 then
  249. selected = true
  250. self"sprite".color = vec4(1)
  251. if win:mouse_pressed("left") then
  252. vplay_sfx(math.random(1000000000))
  253. end
  254. else
  255. selected = false
  256. self"sprite".color = vec4(0.95)
  257. end
  258. end)
  259. group:append(logo)
  260. end
  261. group:append(make_main_scene_toolbelt())
  262. group:action(main_action)
  263. return group:tag"menu"
  264. end
  265. win.scene = am.group(
  266. main_scene(true, true)
  267. )
  268. play_track(SOUNDS.MAIN_THEME)
  269. noglobals()