BasicEntities.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """BasicEntities module: contains fundamental entity types and base classes"""
  2. import Entity
  3. import DistributedEntity
  4. import NodePath
  5. # this is an abstract class, do not instantiate.
  6. class NodePathAttribs:
  7. """Derive from this class to give an entity the behavior of a
  8. NodePath, without necessarily deriving from NodePath. Derived class
  9. must implement getNodePath()."""
  10. def initNodePathAttribs(self, doReparent=1):
  11. """Call this after the entity has been initialized"""
  12. self.callSetters('pos','x','y','z',
  13. 'hpr','h','p','r',
  14. 'scale','sx','sy','sz')
  15. if doReparent and hasattr(self, 'parent'):
  16. self.level.requestReparent(self.getNodePath(), self.parent)
  17. def reparentTo(self, *args): self.getNodePath().reparentTo(*args)
  18. def setPos(self, *args): self.getNodePath().setPos(*args)
  19. def setX(self, *args): self.getNodePath().setX(*args)
  20. def setY(self, *args): self.getNodePath().setY(*args)
  21. def setZ(self, *args): self.getNodePath().setZ(*args)
  22. def setHpr(self, *args): self.getNodePath().setHpr(*args)
  23. def setH(self, *args): self.getNodePath().setH(*args)
  24. def setP(self, *args): self.getNodePath().setP(*args)
  25. def setR(self, *args): self.getNodePath().setR(*args)
  26. def setScale(self, *args): self.getNodePath().setScale(*args)
  27. def setSx(self, *args): self.getNodePath().setSx(*args)
  28. def setSy(self, *args): self.getNodePath().setSy(*args)
  29. def setSz(self, *args): self.getNodePath().setSz(*args)
  30. class privNodePathImpl(NodePath.NodePath):
  31. def __init__(self, name):
  32. node = hidden.attachNewNode(name)
  33. NodePath.NodePath.__init__(self, node)
  34. def initNodePathAttribs(self):
  35. """Call this after the entity has been initialized, and all
  36. of its attributes have been set"""
  37. self.callSetters('pos','x','y','z',
  38. 'hpr','h','p','r',
  39. 'scale','sx','sy','sz')
  40. self.level.requestReparent(self, self.parent)
  41. def destroy(self):
  42. self.removeNode()
  43. def getNodePath(self):
  44. return self
  45. class NodePathEntity(Entity.Entity, privNodePathImpl):
  46. """This is an entity that represents a NodePath on the client.
  47. It may be instantiated directly or used as a base class for other
  48. entity types."""
  49. def __init__(self, level, entId):
  50. privNodePathImpl.__init__(self, '')
  51. Entity.Entity.__init__(self, level, entId)
  52. self.setName(str(self))
  53. privNodePathImpl.initNodePathAttribs(self)
  54. def destroy(self):
  55. Entity.Entity.destroy(self)
  56. privNodePathImpl.destroy(self)
  57. def getNodePath(self):
  58. return self
  59. class DistributedNodePathEntity(DistributedEntity.DistributedEntity,
  60. privNodePathImpl):
  61. """This is a distributed version of NodePathEntity. It should not
  62. be instantiated directly; derive your client-side distEntity from
  63. this class instead of DistributedEntity."""
  64. def __init__(self, cr):
  65. DistributedEntity.DistributedEntity.__init__(self, cr)
  66. privNodePathImpl.__init__(self, 'DistributedNodePathEntity')
  67. def announceGenerate(self):
  68. DistributedEntity.DistributedEntity.announceGenerate(self)
  69. privNodePathImpl.initNodePathAttribs(self)
  70. def destroy(self):
  71. DistributedEntity.DistributedEntity.destroy(self)
  72. privNodePathImpl.destroy(self)
  73. def getNodePath(self):
  74. return self