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.

307 lines
9.6 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
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. local num_of_types = #table.keys(TOWER_TYPE)
  92. if WIN:key_down"lshift" then
  93. select_tower_type((SELECTED_TOWER_TYPE + num_of_types - 2) % num_of_types + 1)
  94. else
  95. select_tower_type((SELECTED_TOWER_TYPE) % num_of_types + 1)
  96. end
  97. elseif WIN:key_pressed"1" then select_tower_type(TOWER_TYPE.REDEYE)
  98. elseif WIN:key_pressed"2" then select_tower_type(2)
  99. elseif WIN:key_pressed"3" then select_tower_type(3)
  100. elseif WIN:key_pressed"4" then --select_tower_type(4)
  101. elseif WIN:key_pressed"5" then --select_tower_type(5)
  102. elseif WIN:key_pressed"6" then --select_tower_type(6)
  103. elseif WIN:key_pressed"7" then --select_tower_type(7)
  104. elseif WIN:key_pressed"8" then --select_tower_type(8)
  105. elseif WIN:key_pressed"9" then --select_tower_type(9)
  106. elseif WIN:key_pressed"0" then --select_tower_type(10)
  107. elseif WIN:key_pressed"-" then --select_tower_type(1)
  108. elseif WIN:key_pressed"=" then --select_tower_type(1)
  109. end
  110. if tile and hot then
  111. WIN.scene"hex_cursor".center = rounded_mouse
  112. else
  113. WIN.scene"hex_cursor".center = OFF_SCREEN
  114. end
  115. WIN.scene"score".text = string.format("SCORE: %.2f", SCORE)
  116. WIN.scene"money".text = string.format("MONEY: %d", MONEY)
  117. do
  118. local str = ""
  119. if TRDT == TRDTS.CENTERED_EVENQ then
  120. str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)"
  121. elseif TRDT == TRDTS.EVENQ then
  122. str = evenq.x .. "," .. evenq.y .. " (evenq)"
  123. elseif TRDT == TRDTS.HEX then
  124. str = hex.x .. "," .. hex.y .. " (hex)"
  125. elseif TRDT == TRDTS.PLATFORM then
  126. str = string.format("%s %s lang %s", am.platform, am.version, am.language())
  127. elseif TRDT == TRDTS.PERF then
  128. str = table.tostring(PERF_STATS)
  129. elseif TRDT == TRDTS.SEED then
  130. str = "SEED: " .. HEX_MAP.seed
  131. elseif TRDT == TRDTS.TILE then
  132. str = table.tostring(HEX_MAP.get(hex.x, hex.y))
  133. end
  134. WIN.scene"coords".text = str
  135. end
  136. --do_day_night_cycle()
  137. end
  138. function do_day_night_cycle()
  139. local tstep = (math.sin(TIME) / PERF_STATS.avg_fps + 1)/8
  140. WORLD"negative_mask".color = vec4(tstep)
  141. end
  142. function game_end()
  143. -- de-initialize stuff
  144. delete_all_entities()
  145. TIME = 0
  146. SCORE = 0
  147. MONEY = 0
  148. WORLD = false
  149. WIN.scene = am.scale(1) ^ game_scene()
  150. end
  151. function update_score(diff)
  152. SCORE = SCORE + diff
  153. end
  154. local function toolbelt()
  155. local toolbelt_height = hex_height(HEX_SIZE) * 2
  156. local tower_tooltip = am.translate(WIN.left + 10, WIN.bottom + toolbelt_height + 20)
  157. ^ am.text(tower_type_tostring(SELECTED_TOWER_TYPE), "left"):tag"tower_tooltip"
  158. local toolbelt = am.group{
  159. tower_tooltip,
  160. am.rect(WIN.left, WIN.bottom, WIN.right, WIN.bottom + toolbelt_height, COLORS.TRANSPARENT)
  161. }:tag"toolbelt"
  162. local padding = 15
  163. local size = toolbelt_height - padding
  164. local half_size = size/2
  165. local offset = vec2(WIN.left + padding*3, WIN.bottom + padding/3)
  166. local tab_button = am.translate(vec2(0, half_size) + offset)
  167. ^ am.group{
  168. pack_texture_into_sprite(TEX_WIDER_BUTTON1, 54, 32),
  169. pack_texture_into_sprite(TEX_TAB_ICON, 25, 25)
  170. }
  171. toolbelt:append(tab_button)
  172. local tower_select_square = (
  173. am.translate(vec2(size + padding, half_size) + offset)
  174. ^ am.rect(-size/2-3, -size/2-3, size/2+3, size/2+3, COLORS.SUNRAY)
  175. ):tag"tower_select_square"
  176. toolbelt:append(tower_select_square)
  177. local tower_type_values = table.values(TOWER_TYPE)
  178. local keys = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=' }
  179. for i = 1, #keys do
  180. if tower_type_values[i] then
  181. toolbelt:append(
  182. am.translate(vec2(size + padding, 0) * i + offset)
  183. ^ am.group{
  184. am.translate(0, half_size)
  185. ^ pack_texture_into_sprite(TEX_BUTTON1, size, size),
  186. am.translate(0, half_size)
  187. ^ pack_texture_into_sprite(get_tower_texture(tower_type_values[i]), size, size),
  188. am.translate(vec2(half_size))
  189. ^ am.group{
  190. pack_texture_into_sprite(TEX_BUTTON1, half_size, half_size),
  191. am.scale(2)
  192. ^ am.text(keys[i], COLORS.BLACK)
  193. }
  194. }
  195. )
  196. else
  197. toolbelt:append(
  198. am.translate(vec2(size + padding, 0) * i + offset)
  199. ^ am.group{
  200. am.translate(0, half_size)
  201. ^ pack_texture_into_sprite(TEX_BUTTON1, size, size),
  202. am.translate(vec2(half_size))
  203. ^ am.group{
  204. pack_texture_into_sprite(TEX_BUTTON1, half_size, half_size),
  205. am.scale(2)
  206. ^ am.text(keys[i], COLORS.BLACK)
  207. }
  208. }
  209. )
  210. end
  211. end
  212. select_tower_type = function(tower_type)
  213. SELECTED_TOWER_TYPE = tower_type
  214. WIN.scene"tower_tooltip".text = tower_type_tostring(tower_type)
  215. local new_position = vec2((size + padding) * tower_type, size/2) + offset
  216. WIN.scene"tower_select_square":action(am.tween(0.1, { position2d = new_position }))
  217. WIN.scene:action(am.play(am.sfxr_synth(SOUNDS.SELECT1), false, 1, SFX_VOLUME))
  218. end
  219. return toolbelt
  220. end
  221. -- @NOTE must be global to allow the game_action to reference it
  222. function game_scene()
  223. local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
  224. local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
  225. local coords = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"coords"
  226. local hex_cursor = am.circle(OFF_SCREEN, HEX_SIZE, COLORS.TRANSPARENT, 6):tag"hex_cursor"
  227. local curtain = am.rect(WIN.left, WIN.bottom, WIN.right, WIN.top, COLORS.TRUE_BLACK)
  228. curtain:action(coroutine.create(function()
  229. am.wait(am.tween(curtain, 3, { color = vec4(0) }, am.ease.out(am.ease.hyperbola)))
  230. WIN.scene:remove(curtain)
  231. end))
  232. HEX_MAP, WORLD = random_map()
  233. local scene = am.group{
  234. WORLD,
  235. curtain,
  236. hex_cursor,
  237. toolbelt(),
  238. score,
  239. money,
  240. coords,
  241. }
  242. scene:action(game_action)
  243. --scene:action(am.play(SOUNDS.TRACK1))
  244. return scene
  245. end
  246. load_textures()
  247. WIN.scene = am.scale(vec2(1)) ^ game_scene()
  248. noglobals()