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.

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