DistributedSmoothNodeBase.py 3.9 KB

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