DistributedObjectBase.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from direct.showbase.DirectObject import DirectObject
  2. from direct.directnotify.DirectNotifyGlobal import directNotify
  3. class DistributedObjectBase(DirectObject):
  4. """
  5. The Distributed Object class is the base class for all network based
  6. (i.e. distributed) objects. These will usually (always?) have a
  7. dclass entry in a \\*.dc file.
  8. """
  9. notify = directNotify.newCategory("DistributedObjectBase")
  10. def __init__(self, cr):
  11. assert self.notify.debugStateCall(self)
  12. self.cr = cr
  13. self.parentId = None
  14. self.zoneId = None
  15. if __debug__:
  16. def status(self, indent=0):
  17. """
  18. print out "doId(parentId, zoneId) className"
  19. """
  20. spaces = ' ' * (indent + 2)
  21. try:
  22. print("%s%s:" % (' ' * indent, self.__class__.__name__))
  23. print("%sfrom DistributedObject doId:%s, parent:%s, zone:%s" % (
  24. spaces, self.doId, self.parentId, self.zoneId))
  25. except Exception as e:
  26. print("%serror printing status %s" % (spaces, e))
  27. def getLocation(self):
  28. try:
  29. if self.parentId == 0 and self.zoneId == 0:
  30. return None
  31. # This is a -1 stuffed into a uint32
  32. if self.parentId == 0xffffffff and self.zoneId == 0xffffffff:
  33. return None
  34. return (self.parentId, self.zoneId)
  35. except AttributeError:
  36. return None
  37. def handleChildArrive(self, childObj, zoneId):
  38. """
  39. A new child has just setLocation beneath us. Give us a
  40. chance to run code when a new child sets location to us. For
  41. example, we may want to scene graph reparent the child to
  42. some subnode we own.
  43. """
  44. assert self.notify.debugCall()
  45. # Inheritors should override
  46. pass
  47. def handleChildArriveZone(self, childObj, zoneId):
  48. """
  49. A child has just changed zones beneath us with setLocation.
  50. Give us a chance to run code when an existing child sets
  51. location to us. For example, we may want to scene graph
  52. reparent the child to some subnode we own.
  53. """
  54. assert self.notify.debugCall()
  55. # Inheritors should override
  56. pass
  57. def handleChildLeave(self, childObj, zoneId):
  58. """
  59. A child is about to setLocation away from us. Give us a
  60. chance to run code just before a child sets location away from us.
  61. """
  62. assert self.notify.debugCall()
  63. # Inheritors should override
  64. pass
  65. def handleChildLeaveZone(self, childObj, zoneId):
  66. """
  67. A child is about to setLocation to another zone beneath us.
  68. Give us a chance to run code just before a child sets
  69. location to that zone.
  70. """
  71. assert self.notify.debugCall()
  72. # Inheritors should override
  73. pass
  74. def handleQueryObjectChildrenLocalDone(self, context):
  75. assert self.notify.debugCall()
  76. # Inheritors should override
  77. pass
  78. def getParentObj(self):
  79. if self.parentId is None:
  80. return None
  81. return self.cr.doId2do.get(self.parentId)
  82. def hasParentingRules(self):
  83. return self.dclass.getFieldByName('setParentingRules') != None
  84. def delete(self):
  85. """
  86. Override this to handle cleanup right before this object
  87. gets deleted.
  88. """
  89. pass