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.

221 lines
6.1 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
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
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
5 years ago
5 years ago
  1. math.randomseed(os.time())
  2. math.random()
  3. math.random()
  4. math.random()
  5. -- assets/non-or-trivial code
  6. require "color"
  7. require "sound"
  8. require "texture"
  9. require "src/entity"
  10. require "src/extra"
  11. require "src/geometry"
  12. require "src/hexyz"
  13. require "src/grid"
  14. require "src/mob"
  15. require "src/projectile"
  16. require "src/tower"
  17. -- Globals
  18. WIN = am.window{
  19. width = 1920,
  20. height = 1080,
  21. title = "hexyz",
  22. highdpi = true,
  23. letterbox = true,
  24. clear_color = color_at(0)
  25. }
  26. OFF_SCREEN = vec2(WIN.width * 2) -- arbitrary pixel position that is garunteed to be off screen
  27. WORLD = false -- root scene node of everything considered to be in the game world
  28. -- aka non gui stuff
  29. TIME = 0 -- runtime of the current game in seconds (not whole program runtime)
  30. SCORE = 0 -- score of the player
  31. MONEY = 0 -- available resources
  32. MOUSE = false -- position of the mouse at the start of every frame, if an action is tracking it
  33. -- global audio settings
  34. MUSIC_VOLUME = 0.1
  35. SFX_VOLUME = 0.1
  36. -- game stuff
  37. SELECTED_TOWER_TYPE = TOWER_TYPE.REDEYE
  38. -- top right display types
  39. local TRDTS = {
  40. NOTHING = -1,
  41. CENTERED_EVENQ = 0,
  42. EVENQ = 1,
  43. HEX = 2,
  44. PLATFORM = 3,
  45. PERF = 4,
  46. SEED = 5,
  47. TILE = 6
  48. }
  49. local TRDT = TRDTS.SEED
  50. local function select_tower(tower_type)
  51. SELECTED_TOWER_TYPE = tower_type
  52. WIN.scene"tower_tooltip".text = tower_type_tostring(tower_type)
  53. end
  54. local function game_action(scene)
  55. if SCORE < 0 then game_end() end
  56. TIME = TIME + am.delta_time
  57. SCORE = SCORE + am.delta_time
  58. MOUSE = WIN:mouse_position()
  59. local mwd = WIN:mouse_wheel_delta()
  60. local hex = pixel_to_hex(MOUSE - WORLDSPACE_COORDINATE_OFFSET)
  61. local rounded_mouse = hex_to_pixel(hex) + WORLDSPACE_COORDINATE_OFFSET
  62. local evenq = hex_to_evenq(hex)
  63. local centered_evenq = evenq{ y = -evenq.y } - vec2(math.floor(HEX_GRID_WIDTH/2)
  64. , math.floor(HEX_GRID_HEIGHT/2))
  65. local tile = HEX_MAP.get(hex.x, hex.y)
  66. local hot = is_interactable(tile, evenq{ y = -evenq.y })
  67. do_entity_updates()
  68. do_mob_spawning()
  69. if WIN:mouse_pressed"left" then
  70. if hot and is_buildable(hex, tile, nil) then
  71. make_and_register_tower(hex, SELECTED_TOWER_TYPE)
  72. end
  73. end
  74. if WIN:mouse_pressed"middle" then
  75. WIN.scene"scale".scale2d = vec2(1)
  76. else
  77. local small_mwd = vec2(mwd.y / 1000)
  78. WIN.scene"scale".scale = WIN.scene"scale".scale + small_mwd
  79. WIN.scene"scale".scale = WIN.scene"scale".scale + small_mwd
  80. end
  81. if WIN:key_pressed"escape" then
  82. game_end()
  83. elseif WIN:key_pressed"f1" then
  84. TRDT = (TRDT + 1) % #table.keys(TRDTS)
  85. elseif WIN:key_pressed"f2" then
  86. WORLD"priority_overlay".hidden = not WORLD"priority_overlay".hidden
  87. elseif WIN:key_pressed"tab" then
  88. select_tower((SELECTED_TOWER_TYPE + 1) % #table.keys(TOWER_TYPE))
  89. end
  90. if tile and hot then
  91. WIN.scene"hex_cursor".center = rounded_mouse
  92. else
  93. WIN.scene"hex_cursor".center = OFF_SCREEN
  94. end
  95. WIN.scene"score".text = string.format("SCORE: %.2f", SCORE)
  96. WIN.scene"money".text = string.format("MONEY: %d", MONEY)
  97. do
  98. local str = ""
  99. if TRDT == TRDTS.CENTERED_EVENQ then
  100. str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)"
  101. elseif TRDT == TRDTS.EVENQ then
  102. str = evenq.x .. "," .. evenq.y .. " (evenq)"
  103. elseif TRDT == TRDTS.HEX then
  104. str = hex.x .. "," .. hex.y .. " (hex)"
  105. elseif TRDT == TRDTS.PLATFORM then
  106. str = string.format("%s %s lang %s", am.platform, am.version, am.language())
  107. elseif TRDT == TRDTS.PERF then
  108. str = table.tostring(am.perf_stats())
  109. elseif TRDT == TRDTS.SEED then
  110. str = "SEED: " .. HEX_MAP.seed
  111. elseif TRDT == TRDTS.TILE then
  112. str = table.tostring(HEX_MAP.get(hex.x, hex.y))
  113. end
  114. WIN.scene"coords".text = str
  115. end
  116. end
  117. function game_end()
  118. WIN.scene.paused = true
  119. -- de-initialize stuff
  120. delete_all_entities()
  121. SCORE = 0
  122. WIN.scene = game_scene()
  123. end
  124. function update_score(diff)
  125. SCORE = SCORE + diff
  126. end
  127. local function toolbelt()
  128. local toolbelt_height = hex_height(HEX_SIZE) * 2
  129. local tower_tooltip = am.translate(WIN.left + 10, WIN.bottom + toolbelt_height + 10)
  130. ^ am.text(tower_type_tostring(SELECTED_TOWER_TYPE), "left"):tag"tower_tooltip"
  131. local toolbelt = am.group{
  132. tower_tooltip,
  133. am.rect(WIN.left, WIN.bottom, WIN.right, WIN.bottom + toolbelt_height, COLORS.TRANSPARENT)
  134. }
  135. --[[
  136. local padding = 22
  137. local size = toolbelt_height - padding
  138. for i = 0, 0 do
  139. toolbelt:append(
  140. am.translate(vec2(size + padding, 0) * i + vec2(WIN.left + padding/3, WIN.bottom + padding/3))
  141. ^ am.rect(0, 0, size, size, COLORS.BLACK)
  142. )
  143. end
  144. ]]
  145. return toolbelt
  146. end
  147. -- @NOTE must be global to allow the game_action to reference it
  148. function game_scene()
  149. local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
  150. local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
  151. local coords = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"coords"
  152. local hex_cursor = am.circle(OFF_SCREEN, HEX_SIZE, COLORS.TRANSPARENT, 6):tag"hex_cursor"
  153. local curtain = am.rect(WIN.left, WIN.bottom, WIN.right, WIN.top, COLORS.TRUE_BLACK)
  154. curtain:action(coroutine.create(function()
  155. am.wait(am.tween(curtain, 3, { color = vec4(0) }, am.ease.out(am.ease.hyperbola)))
  156. WIN.scene:remove(curtain)
  157. end))
  158. HEX_MAP, WORLD = random_map()
  159. local scene = am.group{
  160. WORLD,
  161. curtain,
  162. hex_cursor,
  163. toolbelt(),
  164. score,
  165. money,
  166. coords,
  167. }
  168. scene:action(game_action)
  169. --scene:action(am.play(SOUNDS.TRACK1))
  170. return scene
  171. end
  172. load_textures()
  173. WIN.scene = am.scale(vec2(1)) ^ game_scene()
  174. noglobals()