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.

302 lines
9.4 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
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. PERF_STATS = false -- result of am.perf_stats() -- should be called every frame
  28. WORLD = false -- root scene node of everything considered to be in the game world
  29. -- aka non gui stuff
  30. TIME = 0 -- runtime of the current game in seconds (not whole program runtime)
  31. SCORE = 0 -- score of the player
  32. MONEY = 0 -- available resources
  33. MOUSE = false -- position of the mouse at the start of every frame, if an action is tracking it
  34. -- global audio settings
  35. MUSIC_VOLUME = 0.1
  36. SFX_VOLUME = 0.1
  37. -- game stuff
  38. SELECTED_TOWER_TYPE = TOWER_TYPE.REDEYE
  39. -- top right display types
  40. local TRDTS = {
  41. NOTHING = -1,
  42. CENTERED_EVENQ = 0,
  43. EVENQ = 1,
  44. HEX = 2,
  45. PLATFORM = 3,
  46. PERF = 4,
  47. SEED = 5,
  48. TILE = 6
  49. }
  50. local TRDT = TRDTS.SEED
  51. local function select_hex(hex)
  52. local tower = tower_on_hex(hex)
  53. local tile = HEX_MAP.get(hex.x, hex.y)
  54. log(tile)
  55. end
  56. local function can_do_build(hex, tile, tower_type)
  57. return tower_is_buildable_on(hex, tile, SELECTED_TOWER_TYPE)
  58. end
  59. local function game_action(scene)
  60. if SCORE < 0 then game_end() end
  61. TIME = TIME + am.delta_time
  62. SCORE = SCORE + am.delta_time
  63. MOUSE = WIN:mouse_position()
  64. PERF_STATS = am.perf_stats()
  65. local hex = pixel_to_hex(MOUSE - WORLDSPACE_COORDINATE_OFFSET)
  66. local rounded_mouse = hex_to_pixel(hex) + WORLDSPACE_COORDINATE_OFFSET
  67. local evenq = hex_to_evenq(hex)
  68. local centered_evenq = evenq{ y = -evenq.y } - vec2(math.floor(HEX_GRID_WIDTH/2)
  69. , math.floor(HEX_GRID_HEIGHT/2))
  70. local tile = HEX_MAP.get(hex.x, hex.y)
  71. local hot = evenq_is_interactable(evenq{ y = -evenq.y })
  72. do_entity_updates()
  73. do_mob_spawning()
  74. if WIN:mouse_pressed"left" then
  75. if hot and can_do_build(hex, tile, SELECTED_TOWER_TYPE) then
  76. build_tower(hex, SELECTED_TOWER_TYPE)
  77. end
  78. end
  79. if WIN:mouse_pressed"middle" then
  80. WIN.scene"scale".scale2d = vec2(1)
  81. else
  82. local mwd = vec2(WIN:mouse_wheel_delta().y / 1000)
  83. WIN.scene"scale".scale2d = WIN.scene"scale".scale2d + mwd
  84. WIN.scene"scale".scale2d = WIN.scene"scale".scale2d + mwd
  85. end
  86. if WIN:key_pressed"escape" then
  87. game_end()
  88. elseif WIN:key_pressed"f1" then
  89. TRDT = (TRDT + 1) % #table.keys(TRDTS)
  90. elseif WIN:key_pressed"tab" then
  91. select_tower_type((SELECTED_TOWER_TYPE) % #table.keys(TOWER_TYPE) + 1)
  92. elseif WIN:key_pressed"1" then select_tower_type(TOWER_TYPE.REDEYE)
  93. elseif WIN:key_pressed"2" then select_tower_type(2)
  94. elseif WIN:key_pressed"3" then select_tower_type(3)
  95. elseif WIN:key_pressed"4" then --select_tower_type(4)
  96. elseif WIN:key_pressed"5" then --select_tower_type(5)
  97. elseif WIN:key_pressed"6" then --select_tower_type(6)
  98. elseif WIN:key_pressed"7" then --select_tower_type(7)
  99. elseif WIN:key_pressed"8" then --select_tower_type(8)
  100. elseif WIN:key_pressed"9" then --select_tower_type(9)
  101. elseif WIN:key_pressed"0" then --select_tower_type(10)
  102. elseif WIN:key_pressed"-" then --select_tower_type(1)
  103. elseif WIN:key_pressed"=" then --select_tower_type(1)
  104. end
  105. if tile and hot then
  106. WIN.scene"hex_cursor".center = rounded_mouse
  107. else
  108. WIN.scene"hex_cursor".center = OFF_SCREEN
  109. end
  110. WIN.scene"score".text = string.format("SCORE: %.2f", SCORE)
  111. WIN.scene"money".text = string.format("MONEY: %d", MONEY)
  112. do
  113. local str = ""
  114. if TRDT == TRDTS.CENTERED_EVENQ then
  115. str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)"
  116. elseif TRDT == TRDTS.EVENQ then
  117. str = evenq.x .. "," .. evenq.y .. " (evenq)"
  118. elseif TRDT == TRDTS.HEX then
  119. str = hex.x .. "," .. hex.y .. " (hex)"
  120. elseif TRDT == TRDTS.PLATFORM then
  121. str = string.format("%s %s lang %s", am.platform, am.version, am.language())
  122. elseif TRDT == TRDTS.PERF then
  123. str = table.tostring(PERF_STATS)
  124. elseif TRDT == TRDTS.SEED then
  125. str = "SEED: " .. HEX_MAP.seed
  126. elseif TRDT == TRDTS.TILE then
  127. str = table.tostring(HEX_MAP.get(hex.x, hex.y))
  128. end
  129. WIN.scene"coords".text = str
  130. end
  131. --do_day_night_cycle()
  132. end
  133. function do_day_night_cycle()
  134. local tstep = (math.sin(TIME) / PERF_STATS.avg_fps + 1)/8
  135. WORLD"negative_mask".color = vec4(tstep)
  136. end
  137. function game_end()
  138. -- de-initialize stuff
  139. delete_all_entities()
  140. TIME = 0
  141. SCORE = 0
  142. MONEY = 0
  143. WORLD = false
  144. WIN.scene = am.scale(1) ^ game_scene()
  145. end
  146. function update_score(diff)
  147. SCORE = SCORE + diff
  148. end
  149. local function toolbelt()
  150. local toolbelt_height = hex_height(HEX_SIZE) * 2
  151. local tower_tooltip = am.translate(WIN.left + 10, WIN.bottom + toolbelt_height + 20)
  152. ^ am.text(tower_type_tostring(SELECTED_TOWER_TYPE), "left"):tag"tower_tooltip"
  153. local toolbelt = am.group{
  154. tower_tooltip,
  155. am.rect(WIN.left, WIN.bottom, WIN.right, WIN.bottom + toolbelt_height, COLORS.TRANSPARENT)
  156. }:tag"toolbelt"
  157. local padding = 15
  158. local size = toolbelt_height - padding
  159. local half_size = size/2
  160. local offset = vec2(WIN.left + padding*3, WIN.bottom + padding/3)
  161. local tab_button = am.translate(vec2(0, half_size) + offset)
  162. ^ am.group{
  163. pack_texture_into_sprite(TEX_WIDER_BUTTON1, 54, 32),
  164. pack_texture_into_sprite(TEX_TAB_ICON, 25, 25)
  165. }
  166. toolbelt:append(tab_button)
  167. local tower_select_square = (
  168. am.translate(vec2(size + padding, half_size) + offset)
  169. ^ am.rect(-size/2-3, -size/2-3, size/2+3, size/2+3, COLORS.SUNRAY)
  170. ):tag"tower_select_square"
  171. toolbelt:append(tower_select_square)
  172. local tower_type_values = table.values(TOWER_TYPE)
  173. local keys = { '1', '2', '3', '4', '5', '6', '7', '9', '0', '-', '=' }
  174. for i = 1, #keys do
  175. if tower_type_values[i] then
  176. toolbelt:append(
  177. am.translate(vec2(size + padding, 0) * i + offset)
  178. ^ am.group{
  179. am.translate(0, half_size)
  180. ^ pack_texture_into_sprite(TEX_BUTTON1, size, size),
  181. am.translate(0, half_size)
  182. ^ pack_texture_into_sprite(get_tower_texture(tower_type_values[i]), size, size),
  183. am.translate(vec2(half_size))
  184. ^ am.group{
  185. pack_texture_into_sprite(TEX_BUTTON1, half_size, half_size),
  186. am.scale(2)
  187. ^ am.text(keys[i], COLORS.BLACK)
  188. }
  189. }
  190. )
  191. else
  192. toolbelt:append(
  193. am.translate(vec2(size + padding, 0) * i + offset)
  194. ^ am.group{
  195. am.translate(0, half_size)
  196. ^ pack_texture_into_sprite(TEX_BUTTON1, size, size),
  197. am.translate(vec2(half_size))
  198. ^ am.group{
  199. pack_texture_into_sprite(TEX_BUTTON1, half_size, half_size),
  200. am.scale(2)
  201. ^ am.text(keys[i], COLORS.BLACK)
  202. }
  203. }
  204. )
  205. end
  206. end
  207. select_tower_type = function(tower_type)
  208. SELECTED_TOWER_TYPE = tower_type
  209. WIN.scene"tower_tooltip".text = tower_type_tostring(tower_type)
  210. local new_position = vec2((size + padding) * tower_type, size/2) + offset
  211. WIN.scene"tower_select_square":action(am.tween(0.1, { position2d = new_position }))
  212. WIN.scene:action(am.play(am.sfxr_synth(SOUNDS.SELECT1), false, 1, SFX_VOLUME))
  213. end
  214. return toolbelt
  215. end
  216. -- @NOTE must be global to allow the game_action to reference it
  217. function game_scene()
  218. local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
  219. local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
  220. local coords = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"coords"
  221. local hex_cursor = am.circle(OFF_SCREEN, HEX_SIZE, COLORS.TRANSPARENT, 6):tag"hex_cursor"
  222. local curtain = am.rect(WIN.left, WIN.bottom, WIN.right, WIN.top, COLORS.TRUE_BLACK)
  223. curtain:action(coroutine.create(function()
  224. am.wait(am.tween(curtain, 3, { color = vec4(0) }, am.ease.out(am.ease.hyperbola)))
  225. WIN.scene:remove(curtain)
  226. end))
  227. HEX_MAP, WORLD = random_map()
  228. local scene = am.group{
  229. WORLD,
  230. curtain,
  231. hex_cursor,
  232. toolbelt(),
  233. score,
  234. money,
  235. coords,
  236. }
  237. scene:action(game_action)
  238. --scene:action(am.play(SOUNDS.TRACK1))
  239. return scene
  240. end
  241. load_textures()
  242. WIN.scene = am.scale(vec2(1)) ^ game_scene()
  243. noglobals()