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.

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