BasicEntities.py 3.6 KB

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