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.

84 lines
2.9 KiB

4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
  1. --[[
  2. entity structure:
  3. {
  4. TOB - number - time of birth, const
  5. hex - vec2 - current occupied hex, if any
  6. position - vec2 - current pixel position of it's translate (forced parent) node
  7. update - function - runs every frame with itself and its index in some array as an argument
  8. node - node - scene graph node - should be initialized by caller after, though all entities have a node
  9. z - number - z-index of the scene node, what layer should this node be rendered at?
  10. we currently reserve z index 0 for just the 'floor', no towers or mobs or anything.
  11. most things will set this to 1 (or leave it unset, 1 is the default)
  12. we might use index -1 for underwater shit at some point
  13. type - enum - sub type
  14. props - table - table of properties specific to this entity subtype
  15. }
  16. --]]
  17. function make_basic_entity(hex, update_f, position, z)
  18. local entity = {}
  19. entity.TOB = game_state.time
  20. -- usually you'll provide a hex and not a position, and the entity will spawn in the center
  21. -- of the hex. if you want an entity to exist not at the center of a hex, you can provide a
  22. -- pixel position instead, then the provided hex is ignored and instead we calculate what hex
  23. -- corresponds to the provided pixel position
  24. if position then
  25. entity.position = position
  26. entity.hex = pixel_to_hex(entity.position, vec2(HEX_SIZE))
  27. else
  28. entity.hex = hex
  29. entity.position = hex_to_pixel(hex, vec2(HEX_SIZE))
  30. end
  31. entity.update = update_f
  32. entity.z = z or 1
  33. entity.props = {}
  34. return entity
  35. end
  36. function register_entity(t, entity)
  37. table.insert(t, entity)
  38. game_state.world(world_layer_tag(entity.z)):append(entity.node)
  39. end
  40. -- |t| is the source table, probably game_state.mobs, game_state.towers, or game_state.projectiles
  41. function delete_entity(t, index)
  42. if not t then error("splat!") end
  43. local entity = t[index]
  44. game_state.world(world_layer_tag(entity.z)):remove(entity.node)
  45. t[index] = false -- leave empty indexes so other entities can learn that this entity was deleted
  46. end
  47. function do_entity_updates()
  48. do_mob_updates()
  49. do_tower_updates()
  50. do_projectile_updates()
  51. end
  52. function entity_basic_devectored_copy(entity)
  53. local copy = table.shallow_copy(entity)
  54. copy.position = { copy.position.x, copy.position.y }
  55. copy.hex = { copy.hex.x, copy.hex.y }
  56. return copy
  57. end
  58. function entity_basic_json_parse(json_string)
  59. local entity = am.parse_json(json_string)
  60. entity.position = vec2(entity.position[1], entity.position[2])
  61. entity.hex = vec2(entity.hex[1], entity.hex[2])
  62. return entity
  63. end
  64. function init_entity_specs()
  65. init_tower_specs()
  66. end