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.

358 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
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
3 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
3 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. require "src/map-editor"
  79. local sound_toggle_node_tag = "sound-on-off-icon"
  80. local function make_sound_toggle_node(on)
  81. local sprite
  82. if on then
  83. sprite = pack_texture_into_sprite(TEXTURES.SOUND_ON1, 40, 30)
  84. else
  85. sprite = pack_texture_into_sprite(TEXTURES.SOUND_OFF, 40, 30)
  86. end
  87. return (am.translate(win.right - 30, win.top - 60) ^ sprite)
  88. :tag(sound_toggle_node_tag)
  89. :action(function()
  90. end)
  91. end
  92. local cached_music_volume = 0.2
  93. local cached_sfx_volume = 0.1
  94. local function toggle_mute()
  95. settings.sound_on = not settings.sound_on
  96. if settings.sound_on then
  97. settings.music_volume = cached_music_volume
  98. settings.sfx_volume = cached_sfx_volume
  99. else
  100. cached_music_volume = settings.music_volume
  101. cached_sfx_volume = settings.sfx_volume
  102. settings.music_volume = 0
  103. settings.sfx_volume = 0
  104. end
  105. update_music_volume(settings.music_volume)
  106. win.scene:replace(sound_toggle_node_tag, make_sound_toggle_node(settings.sound_on))
  107. end
  108. -- text popup in the middle of the screen that dissapates, call from anywhere
  109. function alert(message, color)
  110. win.scene:append(
  111. am.scale(3) ^ am.text(message, color or COLORS.WHITE)
  112. :action(coroutine.create(function(self)
  113. am.wait(am.tween(self, 1, { color = vec4(0) }, am.ease_out))
  114. win.scene:remove(self)
  115. end))
  116. )
  117. end
  118. function unpause(root_node)
  119. win.scene("game").paused = false
  120. win.scene:remove(root_node)
  121. end
  122. function main_action(self)
  123. if win:key_pressed("escape") then
  124. if game then
  125. unpause(self)
  126. else
  127. --win:close()
  128. end
  129. elseif win:key_pressed("f4") then
  130. win:close()
  131. elseif win:key_pressed("m") then
  132. toggle_mute()
  133. end
  134. if self"hex_backdrop" then
  135. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  136. end
  137. end
  138. function make_main_scene_toolbelt()
  139. local include_save_option = game
  140. local include_unpause_option = game
  141. local options = {
  142. false,
  143. {
  144. texture = TEXTURES.NEW_GAME_HEX,
  145. action = function()
  146. win.scene:remove"menu"
  147. game_init()
  148. end
  149. },
  150. false,
  151. include_save_option and {
  152. texture = TEXTURES.SAVE_GAME_HEX,
  153. action = function()
  154. game_save()
  155. alert("succesfully saved!")
  156. end
  157. } or false,
  158. false,
  159. {
  160. texture = TEXTURES.LOAD_GAME_HEX,
  161. action = function()
  162. local save = am.load_state("save", "json")
  163. if save then
  164. win.scene:remove("menu")
  165. game_init(save)
  166. else
  167. alert("no saved games")
  168. end
  169. end
  170. },
  171. {
  172. texture = TEXTURES.MAP_EDITOR_HEX,
  173. action = function()
  174. win.scene:remove("menu")
  175. map_editor_init()
  176. end
  177. },
  178. include_unpause_option and {
  179. texture = TEXTURES.UNPAUSE_HEX,
  180. action = function() unpause(win.scene("menu")) end
  181. } or false,
  182. false and {
  183. texture = TEXTURES.SETTINGS_HEX,
  184. action = function() alert("not yet :)") end
  185. },
  186. {
  187. texture = TEXTURES.QUIT_HEX,
  188. action = function() win:close() end
  189. },
  190. false
  191. }
  192. -- calculate the dimensions of the whole grid
  193. local spacing = 150
  194. local grid_width = 6
  195. local grid_height = 2
  196. local hhs = hex_horizontal_spacing(spacing)
  197. local hvs = hex_vertical_spacing(spacing)
  198. local grid_pixel_width = grid_width * hhs
  199. local grid_pixel_height = grid_height * hvs
  200. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  201. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  202. local group = am.group()
  203. local option_index = 1
  204. for i,_ in pairs(map) do
  205. for j,_ in pairs(map[i]) do
  206. local hex = vec2(i, j)
  207. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  208. local option = options[option_index]
  209. local texture = option and option.texture or TEXTURES.SHADED_HEX
  210. local color = option and COLORS.TRANSPARENT or vec4(0.3)
  211. local node = am.translate(position)
  212. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  213. hex_map_set(map, i, j, {
  214. node = node,
  215. option = option
  216. })
  217. local tile = hex_map_get(map, i, j)
  218. local selected = false
  219. node:action(function(self)
  220. local mouse = win:mouse_position()
  221. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  222. if tile.option then
  223. if hex == hex_ then
  224. if not selected then
  225. play_sfx(SOUNDS.SELECT1)
  226. end
  227. selected = true
  228. tile.node"sprite".color = vec4(1)
  229. if win:mouse_pressed("left") then
  230. tile.option.action()
  231. end
  232. else
  233. selected = false
  234. tile.node"sprite".color = COLORS.TRANSPARENT
  235. end
  236. end
  237. end)
  238. group:append(node)
  239. option_index = option_index + 1
  240. end
  241. end
  242. return am.translate(pixel_offset) ^ group
  243. end
  244. function main_scene(do_backdrop, do_logo)
  245. local group = am.group()
  246. if do_backdrop then
  247. local map = hex_hexagonal_map(30)
  248. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  249. for i,_ in pairs(map) do
  250. for j,n in pairs(map[i]) do
  251. local color = map_elevation_color(n)
  252. color = color{a=color.a - 0.1}
  253. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  254. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  255. node"circle":action(am.tween(0.6, { color = color }))
  256. hex_backdrop:append(node)
  257. end
  258. end
  259. group:append(hex_backdrop)
  260. else
  261. group:append(
  262. pack_texture_into_sprite(TEXTURES.CURTAIN, win.width, win.height)
  263. )
  264. end
  265. -- version/author info
  266. group:append(
  267. am.translate(win.right - 10, win.bottom + 10)
  268. ^ am.text(string.format("v%s, by %s", version, author), COLORS.WHITE, "right", "bottom")
  269. )
  270. group:append(
  271. make_sound_toggle_node(settings.sound_on)
  272. )
  273. if do_logo then
  274. local position = vec2(0, win.top - 20 - TEXTURES.LOGO.height/2)
  275. local logo =
  276. am.translate(position)
  277. ^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
  278. local selected = false
  279. logo:action(function(self)
  280. local mouse = win:mouse_position()
  281. if math.distance(mouse, position) < TEXTURES.LOGO.height/2 then
  282. selected = true
  283. self"sprite".color = vec4(1)
  284. if win:mouse_pressed("left") then
  285. vplay_sfx(math.random(1000000000))
  286. end
  287. else
  288. selected = false
  289. self"sprite".color = vec4(0.95)
  290. end
  291. end)
  292. group:append(logo)
  293. end
  294. group:append(make_main_scene_toolbelt())
  295. group:action(main_action)
  296. return group:tag"menu"
  297. end
  298. function switch_scene(scene)
  299. win.scene = am.group(scene)
  300. end
  301. win.scene = am.group(
  302. main_scene(true, true)
  303. )
  304. play_track(SOUNDS.MAIN_THEME)
  305. noglobals()