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.

79 lines
2.5 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
  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. type - enum - sub type - unset if 'basic' entity
  10. props - table - table of properties specific to this entity subtype
  11. ... - any - a bunch of other shit depending on what entity type it is
  12. }
  13. --]]
  14. function make_basic_entity(hex, update, position)
  15. local entity = {}
  16. entity.TOB = state.time
  17. -- usually you'll provide a hex and not a position, and the entity will spawn in the center
  18. -- of the hex. if you want an entity to exist not at the center of a hex, you can provide a
  19. -- pixel position instead, then the provided hex is ignored and instead we calculate what hex
  20. -- corresponds to the provided pixel position
  21. if position then
  22. entity.position = position
  23. entity.hex = pixel_to_hex(entity.position, vec2(HEX_SIZE))
  24. else
  25. entity.hex = hex
  26. entity.position = hex_to_pixel(hex, vec2(HEX_SIZE))
  27. end
  28. entity.update = update
  29. entity.node = false -- set by caller
  30. entity.type = false -- set by caller
  31. entity.props = {}
  32. return entity
  33. end
  34. function register_entity(t, entity)
  35. table.insert(t, entity)
  36. state.world:append(entity.node)
  37. end
  38. -- |t| is the source table, probably state.mobs, state.towers, or state.projectiles
  39. function delete_entity(t, index)
  40. if not t then
  41. error("splat!")
  42. end
  43. state.world:remove(t[index].node)
  44. t[index] = false -- leave empty indexes so other entities can learn that this entity was deleted
  45. end
  46. function do_entity_updates()
  47. do_mob_updates()
  48. do_tower_updates()
  49. do_projectile_updates()
  50. end
  51. function entity_basic_devectored_copy(entity)
  52. local copy = table.shallow_copy(entity)
  53. copy.position = { copy.position.x, copy.position.y }
  54. copy.hex = { copy.hex.x, copy.hex.y }
  55. return copy
  56. end
  57. function entity_basic_json_parse(json_string)
  58. local entity = am.parse_json(json_string)
  59. entity.position = vec2(entity.position[1], entity.position[2])
  60. entity.hex = vec2(entity.hex[1], entity.hex[2])
  61. return entity
  62. end