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.

291 lines
15 KiB

  1. --[[
  2. the following is a list of tower specifications, which are declarations of a variety of properties describing what a tower is, and how it functions
  3. this a lua file. a quick run-down of what writing code in lua looks like: https://www.amulet.xyz/doc/#lua-primer
  4. each tower spec is a lua table. lua tables are the thing that you use to represent bundles of data (both arrays and hashtables are represented by tables)
  5. the format of the bundles in our case are described below.
  6. some propreties are optional. required properties are marked with an asterisk (*), and are generally included at the top of the list.
  7. # TOWER SPEC TABLE
  8. | --------------------------| -------- | -------------------------------------------------------------- |
  9. | property name, required* | datatype | general description / details |
  10. | --------------------------| -------- | -------------------------------------------------------------- |
  11. | name* | string | exact one-line display name text of the tower |
  12. | placement_rules_text* | string | one-line description of the placement rules for this tower |
  13. | short_description* | string | one-line description of the nature of this tower |
  14. | texture* | userdata | @TODO |
  15. | icon_texture* | userdata | @TODO |
  16. | cost* | number | the starting cost of placing this tower |
  17. | | | |
  18. | weapons* | table | an array of weapons. |
  19. | | | order matters - two weapons share a 'choke' value, and both |
  20. | | | could acquire a target in a frame, the first one is choosen. |
  21. | | | |
  22. | placement_rules | table | @TODO |
  23. | | | |
  24. | | | |
  25. | | | |
  26. | | | |
  27. | | | |
  28. | | | |
  29. | update_f | function | default value is complicated @TODO |
  30. | grow_f | function | default value is false/nil. @TODO |
  31. | size | number | default value of 1, which means the tower occupies one hex. |
  32. | height | number | default value of 1. height is relevant for mob pathing and |
  33. | | | projectile collision |
  34. | | | |
  35. | --------------------------| -------- | -------------------------------------------------------------- |
  36. # WEAPON TABLE
  37. | --------------------------| -------- | -------------------------------------------------------------- |
  38. | property name, required* | datatype | general description / details |
  39. | --------------------------| -------- | -------------------------------------------------------------- |
  40. | type | number | sometimes, instead of specifying everything for a weapon, it's |
  41. | | | convenient to refer to a base type. if this is provided all of |
  42. | | | the weapon's other fields will be initialized to preset values |
  43. | | | and any other values you provide with the weapon spec will |
  44. | | | overwrite those preset values. |
  45. | | | if you provide a value here, all other properties become |
  46. | | | optional. |
  47. | | | values you can provide, and what they mean: |
  48. | | | @TODO |
  49. | | | |
  50. | fire_rate* | number | 'shots' per second, if the weapon has a valid target |
  51. | range* | number | max distance (in hexes) at which this weapon acquires targets |
  52. | | | |
  53. | min-range | number | default of 0. min distance (in hexes) at which this weapon acquires targets |
  54. | target_acquisition_f | function | default value is complicated @TODO |
  55. | choke | number | default of false/nil. @TODO |
  56. | | | |
  57. | --------------------------| -------- | -------------------------------------------------------------- |
  58. ]]
  59. return {
  60. {
  61. name = "Wall",
  62. placement_rules_text = "Place on Ground",
  63. short_description = "Restricts movement, similar to a mountain.",
  64. texture = TEXTURES.TOWER_WALL,
  65. icon_texture = TEXTURES.TOWER_WALL_ICON,
  66. cost = 10,
  67. range = 0,
  68. fire_rate = 2,
  69. update = false,
  70. placement_rules = {
  71. }
  72. },
  73. {
  74. name = "Gattler",
  75. placement_rules_text = "Place on Ground",
  76. short_description = "Short-range, fast-fire rate single-target tower.",
  77. texture = TEXTURES.TOWER_GATTLER,
  78. icon_texture = TEXTURES.TOWER_GATTLER_ICON,
  79. cost = 20,
  80. range = 4,
  81. fire_rate = 0.5,
  82. update = function(tower, tower_index)
  83. if not tower.target_index then
  84. -- we should try and acquire a target
  85. for index,mob in pairs(game_state.mobs) do
  86. if mob then
  87. local d = math.distance(mob.hex, tower.hex)
  88. if d <= tower.range then
  89. tower.target_index = index
  90. break
  91. end
  92. end
  93. end
  94. -- passive animation
  95. tower.node("rotate").angle = math.wrapf(tower.node("rotate").angle + 0.1 * am.delta_time, math.pi*2)
  96. else
  97. -- should have a target, so we should try and shoot it
  98. if not game_state.mobs[tower.target_index] then
  99. -- the target we have was invalidated
  100. tower.target_index = false
  101. else
  102. -- the target we have is valid
  103. local mob = game_state.mobs[tower.target_index]
  104. local vector = math.normalize(mob.position - tower.position)
  105. if (game_state.time - tower.last_shot_time) > tower.fire_rate then
  106. local projectile = make_and_register_projectile(
  107. tower.hex,
  108. PROJECTILE_TYPE.BULLET,
  109. vector
  110. )
  111. tower.last_shot_time = game_state.time
  112. play_sfx(SOUNDS.HIT1)
  113. end
  114. -- point the cannon at the dude
  115. local theta = math.rad(90) - math.atan((tower.position.y - mob.position.y)/(tower.position.x - mob.position.x))
  116. local diff = tower.node("rotate").angle - theta
  117. tower.node("rotate").angle = -theta + math.pi/2
  118. end
  119. end
  120. end
  121. },
  122. {
  123. name = "Howitzer",
  124. placement_rules_text = "Place on Ground, with a 1 space gap between other towers and mountains - walls/moats don't count.",
  125. short_description = "Medium-range, medium fire-rate area of effect artillery tower.",
  126. texture = TEXTURES.TOWER_HOWITZER,
  127. icon_texture = TEXTURES.TOWER_HOWITZER_ICON,
  128. cost = 50,
  129. range = 6,
  130. fire_rate = 4,
  131. update = function(tower, tower_index)
  132. if not tower.target_index then
  133. -- we don't have a target
  134. for index,mob in pairs(game_state.mobs) do
  135. if mob then
  136. local d = math.distance(mob.hex, tower.hex)
  137. if d <= tower.range then
  138. tower.target_index = index
  139. break
  140. end
  141. end
  142. end
  143. -- passive animation
  144. tower.node("rotate").angle = math.wrapf(tower.node("rotate").angle + 0.1 * am.delta_time, math.pi*2)
  145. else
  146. -- we should have a target
  147. -- @NOTE don't compare to false, empty indexes appear on game reload
  148. if not game_state.mobs[tower.target_index] then
  149. -- the target we have was invalidated
  150. tower.target_index = false
  151. else
  152. -- the target we have is valid
  153. local mob = game_state.mobs[tower.target_index]
  154. local vector = math.normalize(mob.position - tower.position)
  155. if (game_state.time - tower.last_shot_time) > tower.fire_rate then
  156. local projectile = make_and_register_projectile(
  157. tower.hex,
  158. PROJECTILE_TYPE.SHELL,
  159. vector
  160. )
  161. -- @HACK, the projectile will explode if it encounters something taller than it,
  162. -- but the tower it spawns on quickly becomes taller than it, so we just pad it
  163. -- if it's not enough the shell explodes before it leaves its spawning hex
  164. projectile.props.z = tower.props.z + 0.1
  165. tower.last_shot_time = game_state.time
  166. play_sfx(SOUNDS.EXPLOSION2)
  167. end
  168. -- point the cannon at the dude
  169. local theta = math.rad(90) - math.atan((tower.position.y - mob.position.y)/(tower.position.x - mob.position.x))
  170. local diff = tower.node("rotate").angle - theta
  171. tower.node("rotate").angle = -theta + math.pi/2
  172. end
  173. end
  174. end
  175. },
  176. {
  177. name = "Redeye",
  178. placement_rules_text = "Place on Mountains.",
  179. short_description = "Long-range, penetrating high-velocity laser tower.",
  180. texture = TEXTURES.TOWER_REDEYE,
  181. icon_texture = TEXTURES.TOWER_REDEYE_ICON,
  182. cost = 75,
  183. range = 9,
  184. fire_rate = 3,
  185. update = function(tower, tower_index)
  186. if not tower.target_index then
  187. for index,mob in pairs(game_state.mobs) do
  188. if mob then
  189. local d = math.distance(mob.hex, tower.hex)
  190. if d <= tower.range then
  191. tower.target_index = index
  192. break
  193. end
  194. end
  195. end
  196. else
  197. if not game_state.mobs[tower.target_index] then
  198. tower.target_index = false
  199. elseif (game_state.time - tower.last_shot_time) > tower.fire_rate then
  200. local mob = game_state.mobs[tower.target_index]
  201. make_and_register_projectile(
  202. tower.hex,
  203. PROJECTILE_TYPE.LASER,
  204. math.normalize(mob.position - tower.position)
  205. )
  206. tower.last_shot_time = game_state.time
  207. vplay_sfx(SOUNDS.LASER2)
  208. end
  209. end
  210. end
  211. },
  212. {
  213. name = "Moat",
  214. placement_rules_text = "Place on Ground",
  215. short_description = "Restricts movement, similar to water.",
  216. texture = TEXTURES.TOWER_MOAT,
  217. icon_texture = TEXTURES.TOWER_MOAT_ICON,
  218. cost = 10,
  219. range = 0,
  220. fire_rate = 2,
  221. height = -1,
  222. update = false
  223. },
  224. {
  225. name = "Radar",
  226. placement_rules_text = "n/a",
  227. short_description = "Doesn't do anything right now :(",
  228. texture = TEXTURES.TOWER_RADAR,
  229. icon_texture = TEXTURES.TOWER_RADAR_ICON,
  230. cost = 100,
  231. range = 0,
  232. fire_rate = 1,
  233. update = false
  234. },
  235. {
  236. name = "Lighthouse",
  237. placement_rules_text = "Place on Ground, adjacent to Water or Moats",
  238. short_description = "Attracts nearby mobs; temporarily redirects their path",
  239. texture = TEXTURES.TOWER_LIGHTHOUSE,
  240. icon_texture = TEXTURES.TOWER_LIGHTHOUSE_ICON,
  241. cost = 150,
  242. range = 7,
  243. fire_rate = 1,
  244. target_aquisition_f = function(tower, tower_index)
  245. end,
  246. update = function(tower, tower_index)
  247. -- check if there's a mob on a hex in our perimeter
  248. for _,h in pairs(tower.perimeter) do
  249. local mobs = mobs_on_hex(h)
  250. for _,m in pairs(mobs) do
  251. if not m.path and not m.seen_lighthouse then
  252. -- @TODO only attract the mob if its frame target (direction vector)
  253. -- is within some angle range...? if the mob is heading directly away from the tower, then
  254. -- the lighthouse shouldn't do much
  255. local path, made_it = hex_Astar(game_state.map, tower.hex, m.hex, grid_neighbours, grid_cost, grid_heuristic)
  256. if made_it then
  257. m.path = path
  258. m.seen_lighthouse = true -- right now mobs don't care about lighthouses if they've already seen one.
  259. end
  260. end
  261. end
  262. end
  263. end
  264. },
  265. }