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.

174 lines
4.7 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
5 years ago
5 years ago
  1. settings = am.load_state("settings", "json") or {
  2. fullscreen = false,
  3. window_width = 1920,
  4. window_height = 1080,
  5. music_volume = 0.1,
  6. sfx_volume = 0.1,
  7. }
  8. math.randomseed(os.time())
  9. math.random()
  10. math.random()
  11. math.random()
  12. math.random()
  13. do
  14. win = am.window{
  15. width = settings.window_width,
  16. height = settings.window_height,
  17. title = "hexyz",
  18. mode = settings.fullscreen and "fullscreen" or "windowed",
  19. highdpi = true,
  20. letterbox = true,
  21. resizable = true, -- user should probably set their resolution instead of resizing the window, but hey.
  22. }
  23. end
  24. -- asset interfaces and/or trivial code
  25. require "color"
  26. require "sound"
  27. require "texture"
  28. require "src/entity"
  29. require "src/extra"
  30. require "src/geometry"
  31. require "src/hexyz"
  32. require "src/game"
  33. require "src/gui"
  34. require "src/grid"
  35. require "src/mob"
  36. require "src/projectile"
  37. require "src/tower"
  38. function main_action(self)
  39. self"hex_backdrop""rotate".angle = math.wrapf(self"hex_backdrop""rotate".angle - 0.002 * am.delta_time, math.pi*2)
  40. end
  41. function make_main_scene_toolbelt()
  42. local options = {
  43. false,
  44. false,
  45. false,
  46. false,
  47. {
  48. label = "new game",
  49. texture = TEXTURES.NEW_GAME_HEX,
  50. action = function() end
  51. },
  52. {
  53. label = "load game",
  54. texture = TEXTURES.LOAD_GAME_HEX,
  55. action = function() game_init(am.load_state("save", "json")) end
  56. },
  57. false,
  58. {
  59. label = "settings",
  60. texture = TEXTURES.SETTINGS_HEX,
  61. action = function() end
  62. },
  63. {
  64. label = "about",
  65. texture = TEXTURES.ABOUT_HEX,
  66. action = function() end
  67. },
  68. false,
  69. false,
  70. false,
  71. {
  72. label = "map editor",
  73. texture = TEXTURES.MAP_EDITOR_HEX,
  74. action = function() log("map editor not implemented") end
  75. },
  76. false
  77. }
  78. local spacing = 160
  79. -- calculate the dimensions of the whole grid
  80. local grid_width = 10
  81. local grid_height = 2
  82. local hhs = hex_horizontal_spacing(spacing)
  83. local hvs = hex_vertical_spacing(spacing)
  84. local grid_pixel_width = grid_width * hhs
  85. local grid_pixel_height = grid_height * hvs
  86. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  87. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  88. local group = am.group()
  89. local option_index = 1
  90. for i,_ in pairs(map) do
  91. for j,_ in pairs(map[i]) do
  92. local hex = vec2(i, j)
  93. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  94. local option = options[option_index]
  95. local texture = option and option.texture or TEXTURES.SHADED_HEX
  96. local node = am.translate(position)
  97. ^ pack_texture_into_sprite(texture, texture.width, texture.height, COLORS.TRANSPARENT)
  98. hex_map_set(map, i, j, {
  99. node = node,
  100. option = option
  101. })
  102. local tile = hex_map_get(map, i, j)
  103. node:action(function(self)
  104. local mouse = win:mouse_position()
  105. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  106. if hex == hex_ and tile.option then
  107. tile.node"sprite".color = vec4(1)
  108. if win:mouse_pressed("left") then
  109. tile.option.action()
  110. end
  111. else
  112. tile.node"sprite".color = COLORS.TRANSPARENT
  113. end
  114. end)
  115. group:append(node)
  116. option_index = option_index + 1
  117. end
  118. end
  119. return am.translate(pixel_offset) ^ group
  120. end
  121. function main_scene()
  122. local group = am.group()
  123. local map = hex_hexagonal_map(30)
  124. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  125. for i,_ in pairs(map) do
  126. for j,n in pairs(map[i]) do
  127. local color = map_elevation_color(n)
  128. color = color{a=color.a - 0.1}
  129. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  130. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  131. node"circle":action(am.tween(1, { color = color }))
  132. hex_backdrop:append(node)
  133. end
  134. end
  135. group:append(hex_backdrop)
  136. local logo_height = 480
  137. group:append(am.translate(0, win.top - 20 - logo_height/2) ^ am.sprite("res/logo.png"))
  138. group:append(make_main_scene_toolbelt())
  139. group:action(main_action)
  140. return group
  141. end
  142. win.scene = am.group()
  143. win.scene = main_scene()
  144. --game_init()
  145. noglobals()