ObserverWalker.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. ObserverWalker.py is for avatars.
  3. A walker control such as this one provides:
  4. - creation of the collision nodes
  5. - handling the keyboard and mouse input for avatar movement
  6. - moving the avatar
  7. it does not:
  8. - play sounds
  9. - play animations
  10. although it does send messages that allow a listener to play sounds or
  11. animations based on walker events.
  12. """
  13. from panda3d.core import *
  14. from direct.directnotify import DirectNotifyGlobal
  15. from . import NonPhysicsWalker
  16. class ObserverWalker(NonPhysicsWalker.NonPhysicsWalker):
  17. notify = DirectNotifyGlobal.directNotify.newCategory("ObserverWalker")
  18. # Ghosts slide instead of jump:
  19. slideName = "jump"
  20. def initializeCollisions(self, collisionTraverser, avatarNodePath,
  21. avatarRadius = 1.4, floorOffset = 1.0, reach = 1.0):
  22. """
  23. Set up the avatar for collisions
  24. """
  25. """
  26. Set up the avatar for collisions
  27. """
  28. assert not avatarNodePath.isEmpty()
  29. self.cTrav = collisionTraverser
  30. self.avatarNodePath = avatarNodePath
  31. # Set up the collision sphere
  32. # This is a sphere on the ground to detect barrier collisions
  33. self.cSphere = CollisionSphere(0.0, 0.0, 0.0, avatarRadius)
  34. cSphereNode = CollisionNode('Observer.cSphereNode')
  35. cSphereNode.addSolid(self.cSphere)
  36. self.cSphereNodePath = avatarNodePath.attachNewNode(cSphereNode)
  37. cSphereNode.setFromCollideMask(self.cSphereBitMask)
  38. cSphereNode.setIntoCollideMask(BitMask32.allOff())
  39. # set up wall collision mechanism
  40. self.pusher = CollisionHandlerPusher()
  41. self.pusher.setInPattern("enter%in")
  42. self.pusher.setOutPattern("exit%in")
  43. self.pusher.addCollider(self.cSphereNodePath, avatarNodePath)
  44. # activate the collider with the traverser and pusher
  45. self.setCollisionsActive(1)
  46. class Foo:
  47. def hasContact(self):
  48. return 1
  49. self.lifter = Foo()
  50. def deleteCollisions(self):
  51. del self.cTrav
  52. del self.cSphere
  53. self.cSphereNodePath.removeNode()
  54. del self.cSphereNodePath
  55. del self.pusher
  56. def setCollisionsActive(self, active = 1):
  57. assert self.debugPrint("setCollisionsActive(active%s)"%(active,))
  58. if self.collisionsActive != active:
  59. self.collisionsActive = active
  60. if active:
  61. self.cTrav.addCollider(self.cSphereNodePath, self.pusher)
  62. else:
  63. self.cTrav.removeCollider(self.cSphereNodePath)
  64. # Now that we have disabled collisions, make one more pass
  65. # right now to ensure we aren't standing in a wall.
  66. self.oneTimeCollide()
  67. def oneTimeCollide(self):
  68. """
  69. Makes one quick collision pass for the avatar, for instance as
  70. a one-time straighten-things-up operation after collisions
  71. have been disabled.
  72. """
  73. tempCTrav = CollisionTraverser("oneTimeCollide")
  74. tempCTrav.addCollider(self.cSphereNodePath, self.pusher)
  75. tempCTrav.traverse(render)
  76. def enableAvatarControls(self):
  77. """
  78. Activate the arrow keys, etc.
  79. """
  80. assert self.debugPrint("enableAvatarControls")
  81. pass
  82. def disableAvatarControls(self):
  83. """
  84. Ignore the arrow keys, etc.
  85. """
  86. assert self.debugPrint("disableAvatarControls")
  87. pass