GridParent.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from __future__ import annotations
  2. from panda3d.core import NodePath
  3. #
  4. # GridParent.py
  5. # Any object that can be parented to the ocean grid
  6. # (or any grid whose size is too large to represent in 16 bits),
  7. # should derive from GridParent. Can be used on client and AI code.
  8. # GridParent will put a node inbetween the object and the grid so
  9. # that the object is broadcasting its position relative to the gridCell
  10. # it lies in.
  11. class GridParent:
  12. # this lets GridParents share CellOrigins
  13. GridZone2CellOrigin: dict[tuple, NodePath] = {}
  14. GridZone2count: dict[tuple, int] = {}
  15. @staticmethod
  16. def getCellOrigin(grid, zoneId):
  17. tup = (grid, zoneId)
  18. if tup not in GridParent.GridZone2count:
  19. GridParent.GridZone2count[tup] = 0
  20. # For readability when debugging, append the zone to the name
  21. GridParent.GridZone2CellOrigin[tup] = grid.attachNewNode("cellOrigin-%s" % zoneId)
  22. # Get grid cell origin
  23. cellPos = grid.getZoneCellOrigin(zoneId)
  24. # Set the gridNode's position
  25. GridParent.GridZone2CellOrigin[tup].setPos(*cellPos)
  26. GridParent.GridZone2count[tup] += 1
  27. return GridParent.GridZone2CellOrigin[tup]
  28. @staticmethod
  29. def releaseCellOrigin(grid, zoneId):
  30. tup = (grid, zoneId)
  31. GridParent.GridZone2count[tup] -= 1
  32. if GridParent.GridZone2count[tup] == 0:
  33. del GridParent.GridZone2count[tup]
  34. GridParent.GridZone2CellOrigin[tup].removeNode()
  35. del GridParent.GridZone2CellOrigin[tup]
  36. def __init__(self, av):
  37. # The object on the grid will need to broadcast his position relative to
  38. # his current grid cell in order to use 16 bit
  39. # telemetry. To do this, we will have a node attached to the
  40. # grid cell origin, and the object will wrtReparent himself to it when
  41. # crossing into that grid cell. We don't need to create a node for each
  42. # cell origin. We just need two nodes: one that we are currently parented
  43. # to, and the other that we will wrtReparentTo. Just before wrtReparenting
  44. # to the new node, set it's position to the new grid cell origin.
  45. self.av = av
  46. self.grid = None
  47. # NOTE: this node gets renamed when it is put on a zone, so if you
  48. # are looking for it by name, try cellOrigin*.
  49. self.ownCellOrigin = NodePath("cellOrigin")
  50. self.cellOrigin = self.ownCellOrigin
  51. def delete(self):
  52. if self.av:
  53. if self.av.getParent() == self.cellOrigin:
  54. self.av.detachNode()
  55. del self.av
  56. self.av = None
  57. # Remove the gridNodes
  58. if self.ownCellOrigin is not None:
  59. self.ownCellOrigin.removeNode()
  60. self.ownCellOrigin = None
  61. if self.grid is not None:
  62. self.releaseCellOrigin(self.grid, self.zoneId)
  63. self.grid = None
  64. self.zoneId = None
  65. def setGridParent(self, grid, zoneId, teleport=0):
  66. # If teleport=0, preserve the avatar's absolute position. If teleport=1
  67. # the avatars previous world position is invalid, so don't wrtReparent,
  68. # just do a regular reparent, and let the cellOrigin give us our new position
  69. # Also, if the avatar has no parent, then force teleport=1
  70. if self.av.getParent().isEmpty():
  71. teleport = 1
  72. if not teleport:
  73. # Stick the avatar under hidden while we move the cellOrigin into
  74. # position so we do not lose the avatars absolute position.
  75. self.av.wrtReparentTo(hidden)
  76. if self.grid is not None:
  77. self.releaseCellOrigin(self.grid, self.zoneId)
  78. self.grid = grid
  79. self.zoneId = zoneId
  80. self.cellOrigin = self.getCellOrigin(self.grid, self.zoneId)
  81. # Reparent our avatar to this node
  82. if not teleport:
  83. self.av.wrtReparentTo(self.cellOrigin)
  84. else:
  85. self.av.reparentTo(self.cellOrigin)
  86. #print("gridParent: reparent to %s" % self.av)
  87. #print("gridParent: pos = %s, %s" % (self.av.getPos(), self.av.getParent().getPos()))