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.

350 lines
10 KiB

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