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.

167 lines
4.5 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
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.005 * 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. texture = TEXTURES.NEW_GAME_HEX,
  49. action = function() game_init() end
  50. },
  51. {
  52. texture = TEXTURES.LOAD_GAME_HEX,
  53. action = function() game_init(am.load_state("save", "json")) end
  54. },
  55. false,
  56. {
  57. texture = TEXTURES.SETTINGS_HEX,
  58. action = function() end
  59. },
  60. {
  61. texture = TEXTURES.ABOUT_HEX,
  62. action = function() end
  63. },
  64. false,
  65. false,
  66. false,
  67. {
  68. texture = TEXTURES.MAP_EDITOR_HEX,
  69. action = function() log("map editor not implemented") end
  70. },
  71. false
  72. }
  73. local spacing = 160
  74. -- calculate the dimensions of the whole grid
  75. local grid_width = 10
  76. local grid_height = 2
  77. local hhs = hex_horizontal_spacing(spacing)
  78. local hvs = hex_vertical_spacing(spacing)
  79. local grid_pixel_width = grid_width * hhs
  80. local grid_pixel_height = grid_height * hvs
  81. local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
  82. local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
  83. local group = am.group()
  84. local option_index = 1
  85. for i,_ in pairs(map) do
  86. for j,_ in pairs(map[i]) do
  87. local hex = vec2(i, j)
  88. local position = hex_to_pixel(hex, vec2(spacing), HEX_ORIENTATION.POINTY)
  89. local option = options[option_index]
  90. local texture = option and option.texture or TEXTURES.SHADED_HEX
  91. local node = am.translate(position)
  92. ^ pack_texture_into_sprite(texture, texture.width, texture.height, COLORS.TRANSPARENT)
  93. hex_map_set(map, i, j, {
  94. node = node,
  95. option = option
  96. })
  97. local tile = hex_map_get(map, i, j)
  98. node:action(function(self)
  99. local mouse = win:mouse_position()
  100. local hex_ = pixel_to_hex(mouse - pixel_offset, vec2(spacing), HEX_ORIENTATION.POINTY)
  101. if hex == hex_ and tile.option then
  102. tile.node"sprite".color = vec4(1)
  103. if win:mouse_pressed("left") then
  104. tile.option.action()
  105. end
  106. else
  107. tile.node"sprite".color = COLORS.TRANSPARENT
  108. end
  109. end)
  110. group:append(node)
  111. option_index = option_index + 1
  112. end
  113. end
  114. return am.translate(pixel_offset) ^ group
  115. end
  116. function main_scene()
  117. local group = am.group()
  118. local map = hex_hexagonal_map(30)
  119. local hex_backdrop = (am.rotate(0) ^ am.group()):tag"hex_backdrop"
  120. for i,_ in pairs(map) do
  121. for j,n in pairs(map[i]) do
  122. local color = map_elevation_color(n)
  123. color = color{a=color.a - 0.1}
  124. local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
  125. ^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
  126. node"circle":action(am.tween(1, { color = color }))
  127. hex_backdrop:append(node)
  128. end
  129. end
  130. group:append(hex_backdrop)
  131. local logo_height = 480
  132. group:append(am.translate(0, win.top - 20 - logo_height/2) ^ am.sprite("res/logo.png"))
  133. group:append(make_main_scene_toolbelt())
  134. group:action(main_action)
  135. return group
  136. end
  137. win.scene = main_scene()
  138. noglobals()