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.

313 lines
9.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 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
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
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
5 years ago
3 years ago
4 years ago
5 years ago
  1. -- all 4:3 aspect ratio
  2. local RESOLUTION_OPTIONS = {
  3. { width = 1440, height = 1080 },
  4. { width = 1400, height = 1050 }, -- seems like a good default one
  5. { width = 1280, height = 960 },
  6. { width = 1152, height = 864 },
  7. { width = 1024, height = 768 },
  8. { width = 960, height = 720 },
  9. { width = 832, height = 624 },
  10. { width = 800, height = 600 },
  11. }
  12. local DEFAULT_RESOLUTION = RESOLUTION_OPTIONS[2]
  13. SETTINGS = am.load_state("settings", "json") or {
  14. fullscreen = false,
  15. window_width = DEFAULT_RESOLUTION.width,
  16. window_height = DEFAULT_RESOLUTION.height,
  17. music_volume = 0.2,
  18. sfx_volume = 0.1,
  19. sound_on = true
  20. }
  21. win = am.window{
  22. width = SETTINGS.window_width,
  23. height = SETTINGS.window_height,
  24. title = "",
  25. mode = SETTINGS.fullscreen and "fullscreen" or "windowed",
  26. resizable = false,
  27. highdpi = true,
  28. letterbox = true,
  29. show_cursor = true,
  30. clear_color = vec4(0),
  31. }
  32. -- top right display types
  33. -- different scenes overlay different content in the top right of the screen
  34. -- f1 toggles what is displayed in the top right of the screen in some scenes
  35. TRDTS = {
  36. NOTHING = 0,
  37. CENTERED_EVENQ = 1,
  38. EVENQ = 2,
  39. HEX = 3,
  40. PLATFORM = 4,
  41. PERF = 5,
  42. SEED = 6,
  43. TILE = 7,
  44. }
  45. function make_top_right_display_node()
  46. return am.translate(win.right - 10, win.top - 15)
  47. ^ am.text("", "right", "top"):tag"top_right_display"
  48. end
  49. require "conf"
  50. -- library/standard code (ours)
  51. require "lib/random"
  52. require "lib/extra"
  53. require "lib/memory"
  54. require "lib/geometry"
  55. require "lib/gui"
  56. require "lib/color"
  57. require "lib/sound"
  58. require "lib/texture"
  59. -- other internal dependencies
  60. require "src/hexyz"
  61. require "src/grid"
  62. require "src/game"
  63. require "src/tower"
  64. require "src/mob"
  65. require "src/map-editor"
  66. require "src/entity"
  67. require "src/projectile"
  68. function main_action(self)
  69. if win:key_pressed("escape") then
  70. if game then
  71. unpause(self)
  72. else
  73. --win:close()
  74. end
  75. elseif win:key_pressed("f4") then
  76. win:close()
  77. elseif win:key_pressed("m") then
  78. toggle_mute()
  79. end
  80. if self"hex_backdrop" then
  81. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.005 * am.delta_time, math.pi*2)
  82. end
  83. end
  84. function main_scene(do_backdrop, do_logo)
  85. local group = am.group()
  86. if do_backdrop then
  87. local map = hex_hexagonal_map(30)
  88. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  89. for i,_ in pairs(map) do
  90. for j,n in pairs(map[i]) do
  91. local color = map_elevation_to_color(n)
  92. color = color{a=color.a - 0.1}
  93. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  94. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  95. node"circle":action(am.tween(0.6, { color = color }))
  96. hex_backdrop:append(node)
  97. end
  98. end
  99. group:append(hex_backdrop)
  100. else
  101. group:append(
  102. pack_texture_into_sprite(TEXTURES.CURTAIN, win.width, win.height)
  103. )
  104. end
  105. -- version/author info
  106. group:append(
  107. am.translate(win.right - 10, win.bottom + 10)
  108. ^ am.text(string.format("v%s, by %s", version, author), COLORS.WHITE, "right", "bottom")
  109. )
  110. if do_logo then
  111. local position = vec2(0, win.top - 20 - TEXTURES.LOGO.height/2)
  112. local logo =
  113. am.translate(position)
  114. ^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
  115. local selected = false
  116. logo:action(function(self)
  117. local mouse = win:mouse_position()
  118. if math.distance(mouse, position) < TEXTURES.LOGO.height/2 then
  119. selected = true
  120. self"sprite".color = vec4(1)
  121. if win:mouse_pressed("left") then
  122. vplay_sfx(math.random(1000000000))
  123. end
  124. else
  125. selected = false
  126. self"sprite".color = vec4(0.95)
  127. end
  128. end)
  129. group:append(logo)
  130. end
  131. local seed_textfield, get_seed_textfield_value = gui_make_textfield{
  132. position = vec2(win.left + 500, 50),
  133. dimensions = vec2(90, 40),
  134. max = math.ceil(math.log(HEX_GRID_WIDTH * HEX_GRID_HEIGHT, 10)),
  135. validate = function(string)
  136. return not string.match(string, "%D")
  137. end,
  138. }
  139. group:append(
  140. seed_textfield
  141. )
  142. group:append(
  143. am.translate(win.left + 220, 50) ^ pack_texture_into_sprite(TEXTURES.SEED_COLON_TEXT)
  144. )
  145. local main_scene_options = {
  146. false,
  147. {
  148. texture = TEXTURES.NEW_GAME_HEX,
  149. action = function()
  150. game_init(nil, tonumber(get_seed_textfield_value()))
  151. end
  152. },
  153. false,
  154. false,
  155. false,
  156. {
  157. texture = TEXTURES.LOAD_GAME_HEX,
  158. action = function()
  159. local save = am.load_state("save", "json")
  160. if save then
  161. game_init(save)
  162. else
  163. gui_alert("no saved games")
  164. end
  165. end
  166. },
  167. {
  168. texture = TEXTURES.MAP_EDITOR_HEX,
  169. action = function()
  170. map_editor_init()
  171. end
  172. },
  173. false,
  174. {
  175. texture = TEXTURES.SETTINGS_HEX,
  176. action = function()
  177. gui_alert("not yet :)")
  178. end
  179. },
  180. {
  181. texture = TEXTURES.QUIT_HEX,
  182. action = function()
  183. win:close()
  184. end
  185. },
  186. false
  187. }
  188. group:append(make_scene_menu(main_scene_options, "main_menu"))
  189. group:action(main_action)
  190. return group
  191. end
  192. function make_scene_menu(scene_options, tag, do_curtain)
  193. -- calculate the dimensions of the whole grid
  194. local spacing = 150
  195. local grid_width = 6
  196. local grid_height = 2
  197. local hhs = hex_horizontal_spacing(spacing)
  198. local hvs = hex_vertical_spacing(spacing)
  199. local grid_pixel_width = grid_width * hhs
  200. local grid_pixel_height = grid_height * hvs
  201. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  202. -- 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
  203. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  204. local group = am.group():tag(tag or "menu")
  205. if do_curtain then
  206. group:append(pack_texture_into_sprite(TEXTURES.CURTAIN, win.width, win.height))
  207. end
  208. local menu = am.group()
  209. local option_index = 1
  210. for i,_ in pairs(map) do
  211. for j,_ in pairs(map[i]) do
  212. local hex = vec2(i, j)
  213. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  214. local option = scene_options[option_index]
  215. local texture = option and option.texture or TEXTURES.SHADED_HEX
  216. local color = option and COLORS.TRANSPARENT3 or vec4(0.3)
  217. local node = am.translate(position)
  218. ^ pack_texture_into_sprite(texture, texture.width, texture.height, color)
  219. hex_map_set(map, i, j, {
  220. node = node,
  221. option = option
  222. })
  223. local tile = hex_map_get(map, i, j)
  224. local selected = false
  225. node:action(function(self)
  226. local mouse = win:mouse_position()
  227. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  228. if tile.option then
  229. if tile.option.keys and tile.option.action then
  230. for _,key in pairs(win:keys_pressed()) do
  231. if table.find(tile.option.keys, function(_key) return _key == key end) then
  232. tile.option.action()
  233. end
  234. end
  235. end
  236. if hex == hex_ then
  237. if not selected then
  238. play_sfx(SOUNDS.SELECT1)
  239. end
  240. selected = true
  241. tile.node"sprite".color = vec4(1)
  242. if win:mouse_pressed("left") then
  243. tile.option.action()
  244. end
  245. else
  246. selected = false
  247. tile.node"sprite".color = COLORS.TRANSPARENT3
  248. end
  249. end
  250. end)
  251. menu:append(node)
  252. option_index = option_index + 1
  253. end
  254. end
  255. group:append(am.translate(pixel_offset) ^ menu)
  256. return group
  257. end
  258. function switch_context(scene, action)
  259. win.scene:remove("menu")
  260. if action then
  261. win.scene:replace("context", scene:action(action):tag"context")
  262. else
  263. win.scene:replace("context", scene:tag"context")
  264. end
  265. end
  266. function init()
  267. init_entity_specs()
  268. switch_context(main_scene(true, true))
  269. end
  270. win.scene = am.group(am.group():tag"context")
  271. init()
  272. noglobals()