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.

220 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 hex = pixel_to_hex(MOUSE - WORLDSPACE_COORDINATE_OFFSET)
  60. local rounded_mouse = hex_to_pixel(hex) + WORLDSPACE_COORDINATE_OFFSET
  61. local evenq = hex_to_evenq(hex)
  62. local centered_evenq = evenq{ y = -evenq.y } - vec2(math.floor(HEX_GRID_WIDTH/2)
  63. , math.floor(HEX_GRID_HEIGHT/2))
  64. local tile = HEX_MAP.get(hex.x, hex.y)
  65. local hot = is_interactable(tile, evenq{ y = -evenq.y })
  66. do_entity_updates()
  67. do_mob_spawning()
  68. if WIN:mouse_pressed"left" then
  69. if hot and is_buildable(hex, tile, nil) then
  70. make_and_register_tower(hex, SELECTED_TOWER_TYPE)
  71. end
  72. end
  73. if WIN:mouse_pressed"middle" then
  74. WIN.scene"scale".scale2d = vec2(1)
  75. else
  76. local mwd = vec2(WIN:mouse_wheel_delta().y / 1000)
  77. WIN.scene"scale".scale2d = WIN.scene"scale".scale2d + mwd
  78. WIN.scene"scale".scale2d = WIN.scene"scale".scale2d + mwd
  79. end
  80. if WIN:key_pressed"escape" then
  81. game_end()
  82. elseif WIN:key_pressed"f1" then
  83. TRDT = (TRDT + 1) % #table.keys(TRDTS)
  84. elseif WIN:key_pressed"f2" then
  85. WORLD"priority_overlay".hidden = not WORLD"priority_overlay".hidden
  86. elseif WIN:key_pressed"tab" then
  87. select_tower((SELECTED_TOWER_TYPE + 1) % #table.keys(TOWER_TYPE))
  88. end
  89. if tile and hot then
  90. WIN.scene"hex_cursor".center = rounded_mouse
  91. else
  92. WIN.scene"hex_cursor".center = OFF_SCREEN
  93. end
  94. WIN.scene"score".text = string.format("SCORE: %.2f", SCORE)
  95. WIN.scene"money".text = string.format("MONEY: %d", MONEY)
  96. do
  97. local str = ""
  98. if TRDT == TRDTS.CENTERED_EVENQ then
  99. str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)"
  100. elseif TRDT == TRDTS.EVENQ then
  101. str = evenq.x .. "," .. evenq.y .. " (evenq)"
  102. elseif TRDT == TRDTS.HEX then
  103. str = hex.x .. "," .. hex.y .. " (hex)"
  104. elseif TRDT == TRDTS.PLATFORM then
  105. str = string.format("%s %s lang %s", am.platform, am.version, am.language())
  106. elseif TRDT == TRDTS.PERF then
  107. str = table.tostring(am.perf_stats())
  108. elseif TRDT == TRDTS.SEED then
  109. str = "SEED: " .. HEX_MAP.seed
  110. elseif TRDT == TRDTS.TILE then
  111. str = table.tostring(HEX_MAP.get(hex.x, hex.y))
  112. end
  113. WIN.scene"coords".text = str
  114. end
  115. end
  116. function game_end()
  117. WIN.scene.paused = true
  118. -- de-initialize stuff
  119. delete_all_entities()
  120. SCORE = 0
  121. WIN.scene = am.scale(1) ^ game_scene()
  122. end
  123. function update_score(diff)
  124. SCORE = SCORE + diff
  125. end
  126. local function toolbelt()
  127. local toolbelt_height = hex_height(HEX_SIZE) * 2
  128. local tower_tooltip = am.translate(WIN.left + 10, WIN.bottom + toolbelt_height + 10)
  129. ^ am.text(tower_type_tostring(SELECTED_TOWER_TYPE), "left"):tag"tower_tooltip"
  130. local toolbelt = am.group{
  131. tower_tooltip,
  132. am.rect(WIN.left, WIN.bottom, WIN.right, WIN.bottom + toolbelt_height, COLORS.TRANSPARENT)
  133. }
  134. --[[
  135. local padding = 22
  136. local size = toolbelt_height - padding
  137. for i = 0, 0 do
  138. toolbelt:append(
  139. am.translate(vec2(size + padding, 0) * i + vec2(WIN.left + padding/3, WIN.bottom + padding/3))
  140. ^ am.rect(0, 0, size, size, COLORS.BLACK)
  141. )
  142. end
  143. ]]
  144. return toolbelt
  145. end
  146. -- @NOTE must be global to allow the game_action to reference it
  147. function game_scene()
  148. local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
  149. local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
  150. local coords = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"coords"
  151. local hex_cursor = am.circle(OFF_SCREEN, HEX_SIZE, COLORS.TRANSPARENT, 6):tag"hex_cursor"
  152. local curtain = am.rect(WIN.left, WIN.bottom, WIN.right, WIN.top, COLORS.TRUE_BLACK)
  153. curtain:action(coroutine.create(function()
  154. am.wait(am.tween(curtain, 3, { color = vec4(0) }, am.ease.out(am.ease.hyperbola)))
  155. WIN.scene:remove(curtain)
  156. end))
  157. HEX_MAP, WORLD = random_map()
  158. local scene = am.group{
  159. WORLD,
  160. curtain,
  161. hex_cursor,
  162. toolbelt(),
  163. score,
  164. money,
  165. coords,
  166. }
  167. scene:action(game_action)
  168. --scene:action(am.play(SOUNDS.TRACK1))
  169. return scene
  170. end
  171. load_textures()
  172. WIN.scene = am.scale(vec2(1)) ^ game_scene()
  173. noglobals()