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.

184 lines
4.8 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
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. }
  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 (not whole program runtime)
  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. -- global audio settings
  30. MUSIC_VOLUME = 0.1
  31. SFX_VOLUME = 0.1
  32. -- top right display types
  33. local TRDTS = {
  34. NOTHING = -1,
  35. CENTERED_EVENQ = 0,
  36. EVENQ = 1,
  37. HEX = 2,
  38. PLATFORM = 3,
  39. PERF = 4,
  40. SEED = 5
  41. }
  42. local TRDT = TRDTS.SEED
  43. local function game_action(scene)
  44. if SCORE < 0 then game_end() end
  45. TIME = TIME + am.delta_time
  46. SCORE = SCORE + am.delta_time
  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 game_end()
  63. elseif WIN:key_pressed"f1" then TRDT = (TRDT + 1) % #table.keys(TRDTS)
  64. end
  65. if tile and hot then
  66. WIN.scene"hex_cursor".center = rounded_mouse
  67. else
  68. WIN.scene"hex_cursor".center = OFF_SCREEN
  69. end
  70. WIN.scene"score".text = string.format("SCORE: %.2f", SCORE)
  71. WIN.scene"money".text = string.format("MONEY: %d", MONEY)
  72. do
  73. local str = ""
  74. if TRDT == TRDTS.CENTERED_EVENQ then
  75. str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)"
  76. elseif TRDT == TRDTS.EVENQ then
  77. str = evenq.x .. "," .. evenq.y .. " (evenq)"
  78. elseif TRDT == TRDTS.HEX then
  79. str = hex.x .. "," .. hex.y .. " (hex)"
  80. elseif TRDT == TRDTS.PLATFORM then
  81. str = string.format("%s %s lang %s", am.platform, am.version, am.language())
  82. elseif TRDT == TRDTS.PERF then
  83. str = table.tostring(am.perf_stats())
  84. elseif TRDT == TRDTS.SEED then
  85. str = "SEED: " .. HEX_MAP.seed
  86. end
  87. WIN.scene"coords".text = str
  88. end
  89. end
  90. function game_end()
  91. WIN.scene.paused = true
  92. -- de-initialize stuff
  93. delete_all_entities()
  94. SCORE = 0
  95. WIN.scene = game_scene()
  96. end
  97. function update_score(diff)
  98. SCORE = SCORE + diff
  99. end
  100. local function toolbelt()
  101. local toolbelt_height = hex_height(HEX_SIZE) * 2
  102. local toolbelt = am.group{
  103. am.rect(WIN.left, WIN.bottom, WIN.right, WIN.bottom + toolbelt_height, COLORS.TRANSPARENT)
  104. }
  105. --[[
  106. local padding = 22
  107. local size = toolbelt_height - padding
  108. for i = 0, 0 do
  109. toolbelt:append(
  110. am.translate(vec2(size + padding, 0) * i + vec2(WIN.left + padding/3, WIN.bottom + padding/3))
  111. ^ am.rect(0, 0, size, size, COLORS.BLACK)
  112. )
  113. end
  114. ]]
  115. return toolbelt
  116. end
  117. -- @NOTE must be global to allow the game_action to reference it
  118. function game_scene()
  119. local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
  120. local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
  121. local coords = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"coords"
  122. local hex_cursor = am.circle(OFF_SCREEN, HEX_SIZE, COLORS.TRANSPARENT, 6):tag"hex_cursor"
  123. local curtain = am.rect(WIN.left, WIN.bottom, WIN.right, WIN.top, COLORS.TRUE_BLACK)
  124. curtain:action(coroutine.create(function()
  125. am.wait(am.tween(curtain, 3, { color = vec4(0) }, am.ease.out(am.ease.hyperbola)))
  126. WIN.scene:remove(curtain)
  127. end))
  128. HEX_MAP, WORLD = random_map()
  129. local scene = am.group{
  130. WORLD,
  131. curtain,
  132. hex_cursor,
  133. toolbelt(),
  134. score,
  135. money,
  136. coords,
  137. }
  138. scene:action(game_action)
  139. --scene:action(am.play(SOUNDS.TRACK1))
  140. return scene
  141. end
  142. load_textures()
  143. WIN.scene = game_scene()
  144. noglobals()