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.

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