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.

353 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
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 @TODO @TODO @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. -- top right display types
  63. -- different scenes overlay different content in the top right of the screen
  64. -- f1 toggles what is displayed in the top right of the screen in some scenes
  65. TRDTS = {
  66. NOTHING = 0,
  67. CENTERED_EVENQ = 1,
  68. EVENQ = 2,
  69. HEX = 3,
  70. PLATFORM = 4,
  71. PERF = 5,
  72. SEED = 6,
  73. TILE = 7,
  74. }
  75. function make_top_right_display_node()
  76. return am.translate(win.right - 10, win.top - 15)
  77. ^ am.text("", "right", "top"):tag"top_right_display"
  78. end
  79. -- asset interfaces and/or trivial code
  80. require "conf"
  81. require "color"
  82. require "sound"
  83. require "texture"
  84. --
  85. require "src/entity"
  86. require "src/extra"
  87. require "src/geometry"
  88. require "src/hexyz"
  89. require "src/game"
  90. require "src/gui"
  91. require "src/grid"
  92. require "src/mob"
  93. require "src/projectile"
  94. require "src/tower"
  95. require "src/map-editor"
  96. local sound_toggle_node_tag = "sound_on_off_icon"
  97. local function make_sound_toggle_node(on)
  98. local sprite
  99. if on then
  100. sprite = pack_texture_into_sprite(TEXTURES.SOUND_ON1, 40, 30)
  101. else
  102. sprite = pack_texture_into_sprite(TEXTURES.SOUND_OFF, 40, 30)
  103. end
  104. return (am.translate(win.right - 30, win.top - 60) ^ sprite)
  105. :tag(sound_toggle_node_tag)
  106. :action(function()
  107. -- @TODO click me!
  108. end)
  109. end
  110. local cached_music_volume = 0.2
  111. local cached_sfx_volume = 0.1
  112. local function toggle_mute()
  113. settings.sound_on = not settings.sound_on
  114. if settings.sound_on then
  115. settings.music_volume = cached_music_volume
  116. settings.sfx_volume = cached_sfx_volume
  117. else
  118. cached_music_volume = settings.music_volume
  119. cached_sfx_volume = settings.sfx_volume
  120. settings.music_volume = 0
  121. settings.sfx_volume = 0
  122. end
  123. update_music_volume(settings.music_volume)
  124. win.scene:replace(sound_toggle_node_tag, make_sound_toggle_node(settings.sound_on))
  125. end
  126. function main_action(self)
  127. if win:key_pressed("escape") then
  128. if game then
  129. unpause(self)
  130. else
  131. --win:close()
  132. end
  133. elseif win:key_pressed("f4") then
  134. win:close()
  135. elseif win:key_pressed("m") then
  136. toggle_mute()
  137. end
  138. if self"hex_backdrop" then
  139. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  140. end
  141. end
  142. function make_scene_menu(scene_options, tag)
  143. -- calculate the dimensions of the whole grid
  144. local spacing = 150
  145. local grid_width = 6
  146. local grid_height = 2
  147. local hhs = hex_horizontal_spacing(spacing)
  148. local hvs = hex_vertical_spacing(spacing)
  149. local grid_pixel_width = grid_width * hhs
  150. local grid_pixel_height = grid_height * hvs
  151. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  152. -- generate a map of hexagons (the menu is made up of two rows of hexes) and populate their locations with buttons from the provided options
  153. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  154. local group = am.group():tag(tag or "menu")
  155. local option_index = 1
  156. for i,_ in pairs(map) do
  157. for j,_ in pairs(map[i]) do
  158. local hex = vec2(i, j)
  159. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  160. local option = scene_options[option_index]
  161. local texture = option and option.texture or TEXTURES.SHADED_HEX
  162. local color = option and COLORS.TRANSPARENT or vec4(0.3)
  163. local node = am.translate(position)
  164. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  165. hex_map_set(map, i, j, {
  166. node = node,
  167. option = option
  168. })
  169. local tile = hex_map_get(map, i, j)
  170. local selected = false
  171. node:action(function(self)
  172. local mouse = win:mouse_position()
  173. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  174. if tile.option then
  175. if hex == hex_ then
  176. if not selected then
  177. play_sfx(SOUNDS.SELECT1)
  178. end
  179. selected = true
  180. tile.node"sprite".color = vec4(1)
  181. if win:mouse_pressed("left") then
  182. tile.option.action()
  183. end
  184. else
  185. selected = false
  186. tile.node"sprite".color = COLORS.TRANSPARENT
  187. end
  188. end
  189. end)
  190. group:append(node)
  191. option_index = option_index + 1
  192. end
  193. end
  194. return am.translate(pixel_offset) ^ group
  195. end
  196. function main_scene(do_backdrop, do_logo)
  197. local group = am.group():tag"main_scene"
  198. if do_backdrop then
  199. local map = hex_hexagonal_map(30)
  200. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  201. for i,_ in pairs(map) do
  202. for j,n in pairs(map[i]) do
  203. local color = map_elevation_color(n)
  204. color = color{a=color.a - 0.1}
  205. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  206. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  207. node"circle":action(am.tween(0.6, { color = color }))
  208. hex_backdrop:append(node)
  209. end
  210. end
  211. group:append(hex_backdrop)
  212. else
  213. group:append(
  214. pack_texture_into_sprite(TEXTURES.CURTAIN, win.width, win.height)
  215. )
  216. end
  217. -- version/author info
  218. group:append(
  219. am.translate(win.right - 10, win.bottom + 10)
  220. ^ am.text(string.format("v%s, by %s", version, author), COLORS.WHITE, "right", "bottom")
  221. )
  222. group:append(
  223. make_sound_toggle_node(settings.sound_on)
  224. )
  225. if do_logo then
  226. local position = vec2(0, win.top - 20 - TEXTURES.LOGO.height/2)
  227. local logo =
  228. am.translate(position)
  229. ^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
  230. local selected = false
  231. logo:action(function(self)
  232. local mouse = win:mouse_position()
  233. if math.distance(mouse, position) < TEXTURES.LOGO.height/2 then
  234. selected = true
  235. self"sprite".color = vec4(1)
  236. if win:mouse_pressed("left") then
  237. vplay_sfx(math.random(1000000000))
  238. end
  239. else
  240. selected = false
  241. self"sprite".color = vec4(0.95)
  242. end
  243. end)
  244. group:append(logo)
  245. end
  246. local main_scene_options = {
  247. false,
  248. {
  249. texture = TEXTURES.NEW_GAME_HEX,
  250. action = function()
  251. win.scene:remove"main_scene"
  252. game_init()
  253. end
  254. },
  255. false,
  256. false,
  257. false,
  258. {
  259. texture = TEXTURES.LOAD_GAME_HEX,
  260. action = function()
  261. local save = am.load_state("save", "json")
  262. if save then
  263. win.scene:remove("main_scene")
  264. game_init(save)
  265. else
  266. gui_alert("no saved games")
  267. end
  268. end
  269. },
  270. {
  271. texture = TEXTURES.MAP_EDITOR_HEX,
  272. action = function()
  273. win.scene:remove("main_scene")
  274. map_editor_init()
  275. end
  276. },
  277. false,
  278. {
  279. texture = TEXTURES.SETTINGS_HEX,
  280. action = function()
  281. gui_alert("not yet :)")
  282. end
  283. },
  284. {
  285. texture = TEXTURES.QUIT_HEX,
  286. action = function()
  287. win:close()
  288. end
  289. },
  290. false
  291. }
  292. group:append(make_scene_menu(main_scene_options))
  293. group:action(main_action)
  294. return group
  295. end
  296. win.scene = am.group(
  297. main_scene(true, true)
  298. )
  299. play_track(SOUNDS.MAIN_THEME)
  300. noglobals()