DistributedSmoothNodeBase.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """DistributedSmoothNodeBase module: contains the DistributedSmoothNodeBase class"""
  2. from .ClockDelta import *
  3. from direct.task import Task
  4. from direct.showbase.PythonUtil import randFloat, Enum
  5. from panda3d.direct import CDistributedSmoothNodeBase
  6. class DummyTaskClass:
  7. def setDelay(self, blah):
  8. pass
  9. DummyTask = DummyTaskClass()
  10. class DistributedSmoothNodeBase:
  11. """common base class for DistributedSmoothNode and DistributedSmoothNodeAI
  12. """
  13. BroadcastTypes = Enum('FULL, XYH, XY')
  14. def __init__(self):
  15. self.__broadcastPeriod = None
  16. def generate(self):
  17. self.cnode = CDistributedSmoothNodeBase()
  18. self.cnode.setClockDelta(globalClockDelta)
  19. self.d_broadcastPosHpr = None
  20. def disable(self):
  21. del self.cnode
  22. # make sure our task is gone
  23. self.stopPosHprBroadcast()
  24. def delete(self):
  25. pass
  26. def b_clearSmoothing(self):
  27. self.d_clearSmoothing()
  28. self.clearSmoothing()
  29. def d_clearSmoothing(self):
  30. self.sendUpdate("clearSmoothing", [0])
  31. ### posHprBroadcast ###
  32. def getPosHprBroadcastTaskName(self):
  33. # presumably, we have a doId at this point
  34. return "sendPosHpr-%s" % self.doId
  35. def setPosHprBroadcastPeriod(self, period):
  36. # call this at any time to change the delay between broadcasts
  37. self.__broadcastPeriod = period
  38. def getPosHprBroadcastPeriod(self):
  39. # query the current delay between broadcasts
  40. return self.__broadcastPeriod
  41. def stopPosHprBroadcast(self):
  42. taskMgr.remove(self.getPosHprBroadcastTaskName())
  43. # Delete this callback because it maintains a reference to self
  44. self.d_broadcastPosHpr = None
  45. def posHprBroadcastStarted(self):
  46. return self.d_broadcastPosHpr != None
  47. def wantSmoothPosBroadcastTask(self):
  48. return True
  49. def startPosHprBroadcast(self, period=.2, stagger=0, type=None):
  50. if self.cnode == None:
  51. self.initializeCnode()
  52. BT = DistributedSmoothNodeBase.BroadcastTypes
  53. if type is None:
  54. type = BT.FULL
  55. # set the broadcast type
  56. self.broadcastType = type
  57. broadcastFuncs = {
  58. BT.FULL: self.cnode.broadcastPosHprFull,
  59. BT.XYH: self.cnode.broadcastPosHprXyh,
  60. BT.XY: self.cnode.broadcastPosHprXy,
  61. }
  62. # this comment is here so it will show up in a grep for 'def d_broadcastPosHpr'
  63. self.d_broadcastPosHpr = broadcastFuncs[self.broadcastType]
  64. # Set stagger to non-zero to randomly delay the initial task execution
  65. # over 'period' seconds, to spread out task processing over time
  66. # when a large number of SmoothNodes are created simultaneously.
  67. taskName = self.getPosHprBroadcastTaskName()
  68. # Set up telemetry optimization variables
  69. self.cnode.initialize(self, self.dclass, self.doId)
  70. self.setPosHprBroadcastPeriod(period)
  71. # Broadcast our initial position
  72. self.b_clearSmoothing()
  73. self.cnode.sendEverything()
  74. # remove any old tasks
  75. taskMgr.remove(taskName)
  76. # spawn the new task
  77. delay = 0.
  78. if stagger:
  79. delay = randFloat(period)
  80. if self.wantSmoothPosBroadcastTask():
  81. taskMgr.doMethodLater(self.__broadcastPeriod + delay,
  82. self._posHprBroadcast, taskName)
  83. def _posHprBroadcast(self, task=DummyTask):
  84. # TODO: we explicitly stagger the initial task timing in
  85. # startPosHprBroadcast; we should at least make an effort to keep
  86. # this task accurately aligned with its period and starting time.
  87. self.d_broadcastPosHpr()
  88. task.setDelay(self.__broadcastPeriod)
  89. return Task.again
  90. def sendCurrentPosition(self):
  91. # if we're not currently broadcasting, make sure things are set up
  92. if self.d_broadcastPosHpr is None:
  93. self.cnode.initialize(self, self.dclass, self.doId)
  94. self.cnode.sendEverything()