BasicEntities.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """BasicEntities module: contains fundamental entity types and base classes"""
  2. import Entity
  3. import DistributedEntity
  4. from pandac import NodePath
  5. # base class for entities that support NodePath attributes
  6. # *** Don't derive directly from this class; derive from the appropriate
  7. # specialized class from the classes defined below.
  8. class NodePathEntityBase:
  9. # we don't call this __init__ because it doesn't have to be called
  10. # upon object init
  11. def initNodePathAttribs(self, doReparent=1):
  12. """Call this after the entity has been initialized"""
  13. self.callSetters('pos','x','y','z',
  14. 'hpr','h','p','r',
  15. 'scale','sx','sy','sz')
  16. if doReparent:
  17. self.callSetters('parentEntId')
  18. self.getNodePath().setName('%s-%s' %
  19. (self.__class__.__name__, self.entId))
  20. if __dev__:
  21. # for the editor
  22. self.getNodePath().setTag('entity', '1')
  23. def setParentEntId(self, parentEntId):
  24. self.parentEntId = parentEntId
  25. self.level.requestReparent(self, self.parentEntId)
  26. def destroy(self):
  27. if __dev__:
  28. # for the editor
  29. self.getNodePath().clearTag('entity')
  30. # Entities that already derive from NodePath and Entity should derive
  31. # from this class
  32. class NodePathAttribs(NodePathEntityBase):
  33. def initNodePathAttribs(self, doReparent=1):
  34. NodePathEntityBase.initNodePathAttribs(self, doReparent)
  35. def destroy(self):
  36. NodePathEntityBase.destroy(self)
  37. def getNodePath(self):
  38. return self
  39. # Entities that already derive from Entity, and do not derive from NodePath,
  40. # but want to be a NodePath, should derive from this.
  41. class NodePathAndAttribs(NodePathEntityBase, NodePath.NodePath):
  42. def __init__(self):
  43. node = hidden.attachNewNode('EntityNodePath')
  44. NodePath.NodePath.__init__(self, node)
  45. def initNodePathAttribs(self, doReparent=1):
  46. NodePathEntityBase.initNodePathAttribs(self, doReparent)
  47. def destroy(self):
  48. NodePathEntityBase.destroy(self)
  49. self.removeNode()
  50. def getNodePath(self):
  51. return self
  52. # Entities that already derive from Entity, and do not derive from NodePath,
  53. # but HAVE a NodePath that they want to represent them, should derive from
  54. # this. They must define getNodePath(), which should return their 'proxy'
  55. # NodePath instance.
  56. class NodePathAttribsProxy(NodePathEntityBase):
  57. def initNodePathAttribs(self, doReparent=1):
  58. """Call this after the entity has been initialized"""
  59. NodePathEntityBase.initNodePathAttribs(self, doReparent)
  60. assert self.getNodePath() != self
  61. def destroy(self):
  62. NodePathEntityBase.destroy(self)
  63. def setPos(self, *args): self.getNodePath().setPos(*args)
  64. def setX(self, *args): self.getNodePath().setX(*args)
  65. def setY(self, *args): self.getNodePath().setY(*args)
  66. def setZ(self, *args): self.getNodePath().setZ(*args)
  67. def setHpr(self, *args): self.getNodePath().setHpr(*args)
  68. def setH(self, *args): self.getNodePath().setH(*args)
  69. def setP(self, *args): self.getNodePath().setP(*args)
  70. def setR(self, *args): self.getNodePath().setR(*args)
  71. def setScale(self, *args): self.getNodePath().setScale(*args)
  72. def setSx(self, *args): self.getNodePath().setSx(*args)
  73. def setSy(self, *args): self.getNodePath().setSy(*args)
  74. def setSz(self, *args): self.getNodePath().setSz(*args)
  75. def reparentTo(self, *args): self.getNodePath().reparentTo(*args)
  76. # This is an entity that represents a NodePath on the client.
  77. # It may be instantiated directly or used as a base class for other
  78. # entity types that 'are' NodePaths.
  79. class NodePathEntity(Entity.Entity, NodePath.NodePath, NodePathAttribs):
  80. def __init__(self, level, entId):
  81. node = hidden.attachNewNode('NodePathEntity')
  82. NodePath.NodePath.__init__(self, node)
  83. Entity.Entity.__init__(self, level, entId)
  84. self.initNodePathAttribs(self)
  85. def destroy(self):
  86. NodePathAttribs.destroy(self)
  87. Entity.Entity.destroy(self)
  88. self.removeNode()
  89. # This is a distributed version of NodePathEntity. It should not
  90. # be instantiated directly; distributed entities that are also NodePaths
  91. # may derive from this instead of DistributedEntity.
  92. class DistributedNodePathEntity(DistributedEntity.DistributedEntity,
  93. NodePath.NodePath, NodePathAttribs):
  94. def __init__(self, cr):
  95. DistributedEntity.DistributedEntity.__init__(self, cr)
  96. def generateInit(self):
  97. DistributedEntity.DistributedEntity.generateInit(self)
  98. node = hidden.attachNewNode('DistributedNodePathEntity')
  99. NodePath.NodePath.__init__(self, node)
  100. def announceGenerate(self):
  101. DistributedEntity.DistributedEntity.announceGenerate(self)
  102. self.initNodePathAttribs(self)
  103. def delete(self):
  104. self.removeNode()
  105. DistributedEntity.DistributedEntity.delete(self)