ControlManager.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. from direct.showbase.InputStateGlobal import inputState
  2. #from DirectGui import *
  3. #from PythonUtil import *
  4. #from IntervalGlobal import *
  5. #from otp.avatar import Avatar
  6. from direct.directnotify import DirectNotifyGlobal
  7. #import GhostWalker
  8. #import GravityWalker
  9. #import NonPhysicsWalker
  10. #import PhysicsWalker
  11. #if __debug__:
  12. # import DevWalker
  13. from direct.task import Task
  14. CollisionHandlerRayStart = 4000.0 # This is a hack, it may be better to use a line instead of a ray.
  15. class ControlManager:
  16. notify = DirectNotifyGlobal.directNotify.newCategory("ControlManager")
  17. wantAvatarPhysicsIndicator = base.config.GetBool('want-avatar-physics-indicator', 0)
  18. wantAvatarPhysicsDebug = base.config.GetBool('want-avatar-physics-debug', 0)
  19. wantWASD = base.config.GetBool('want-WASD', 0)
  20. def __init__(self):
  21. assert self.notify.debugCall(id(self))
  22. self.enableJumpCounter = 1
  23. self.controls = {}
  24. self.currentControls = None
  25. self.currentControlsName = None
  26. self.isEnabled = 1
  27. #self.monitorTask = taskMgr.add(self.monitor, "ControlManager-%s"%(id(self)), priority=-1)
  28. inputState.watch("run", "running-on", "running-off")
  29. inputState.watchWithModifiers("forward", "arrow_up")
  30. inputState.watch("forward", "force-forward", "force-forward-stop")
  31. inputState.watchWithModifiers("reverse", "arrow_down")
  32. inputState.watchWithModifiers("reverse", "mouse4")
  33. if self.wantWASD:
  34. inputState.watchWithModifiers("slideLeft", "arrow_left")
  35. inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done")
  36. inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop")
  37. inputState.watchWithModifiers("slideRight", "arrow_right")
  38. inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done")
  39. inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop")
  40. inputState.watchWithModifiers("forward", "w")
  41. inputState.watchWithModifiers("reverse", "s")
  42. inputState.watchWithModifiers("slideLeft", "a")
  43. inputState.watchWithModifiers("slideRight", "d")
  44. inputState.watchWithModifiers("turnLeft", "q")
  45. inputState.watchWithModifiers("turnRight", "e")
  46. else:
  47. inputState.watchWithModifiers("turnLeft", "arrow_left")
  48. inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done")
  49. inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop")
  50. inputState.watchWithModifiers("turnRight", "arrow_right")
  51. inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done")
  52. inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop")
  53. # Jump controls
  54. if self.wantWASD:
  55. inputState.watchWithModifiers("jump", "space")
  56. else:
  57. inputState.watch("jump", "control", "control-up")
  58. def add(self, controls, name="basic"):
  59. """
  60. controls is an avatar control system.
  61. name is any key that you want to use to refer to the
  62. the controls later (e.g. using the use(<name>) call).
  63. Add a control instance to the list of available control systems.
  64. See also: use().
  65. """
  66. assert self.notify.debugCall(id(self))
  67. assert controls is not None
  68. oldControls = self.controls.get(name)
  69. if oldControls is not None:
  70. print "Replacing controls:", name
  71. oldControls.disableAvatarControls()
  72. oldControls.setCollisionsActive(0)
  73. oldControls.delete()
  74. controls.disableAvatarControls()
  75. controls.setCollisionsActive(0)
  76. self.controls[name] = controls
  77. def remove(self, name):
  78. """
  79. name is any key that was used to refer to the
  80. the controls when they were added (e.g.
  81. using the add(<controls>, <name>) call).
  82. Remove a control instance from the list of available control systems.
  83. See also: add().
  84. """
  85. assert self.notify.debugCall(id(self))
  86. oldControls = self.controls.get(name)
  87. if oldControls is not None:
  88. print "Removing controls:", name
  89. oldControls.disableAvatarControls()
  90. oldControls.setCollisionsActive(0)
  91. oldControls.delete()
  92. del self.controls[name]
  93. if __debug__:
  94. def lockControls(self):
  95. self.ignoreUse=True
  96. def unlockControls(self):
  97. if hasattr(self, "ignoreUse"):
  98. del self.ignoreUse
  99. def use(self, name, avatar):
  100. """
  101. name is a key (string) that was previously passed to add().
  102. Use a previously added control system.
  103. See also: add().
  104. """
  105. assert self.notify.debugCall(id(self))
  106. if __debug__ and hasattr(self, "ignoreUse"):
  107. return
  108. controls = self.controls.get(name)
  109. if controls is not None:
  110. if controls is not self.currentControls:
  111. if self.currentControls is not None:
  112. self.currentControls.disableAvatarControls()
  113. self.currentControls.setCollisionsActive(0)
  114. self.currentControls.setAvatar(None)
  115. self.currentControls = controls
  116. self.currentControlsName = name
  117. self.currentControls.setAvatar(avatar)
  118. self.currentControls.setCollisionsActive(1)
  119. if self.isEnabled:
  120. self.currentControls.enableAvatarControls()
  121. messenger.send('use-%s-controls'%(name,), [avatar])
  122. #else:
  123. # print "Controls are already", name
  124. else:
  125. print "Unkown controls:", name
  126. def setSpeeds(self, forwardSpeed, jumpForce,
  127. reverseSpeed, rotateSpeed, strafeLeft=0, strafeRight=0):
  128. assert self.notify.debugCall(id(self))
  129. for controls in self.controls.values():
  130. controls.setWalkSpeed(
  131. forwardSpeed, jumpForce, reverseSpeed, rotateSpeed)
  132. def delete(self):
  133. assert self.notify.debugCall(id(self))
  134. self.disable()
  135. del self.controls
  136. del self.currentControls
  137. inputState.ignore("forward")
  138. inputState.ignore("reverse")
  139. inputState.ignore("turnLeft")
  140. inputState.ignore("turnRight")
  141. inputState.ignore("jump")
  142. inputState.ignore("run")
  143. if self.wantWASD:
  144. inputState.ignore("slideLeft")
  145. inputState.ignore("slideRight")
  146. #self.monitorTask.remove()
  147. def getSpeeds(self):
  148. return self.currentControls.getSpeeds()
  149. def setTag(self, key, value):
  150. assert self.notify.debugCall(id(self))
  151. for controls in self.controls.values():
  152. controls.setTag(key, value)
  153. def deleteCollisions(self):
  154. assert self.notify.debugCall(id(self))
  155. for controls in self.controls.values():
  156. controls.deleteCollisions()
  157. def collisionsOn(self):
  158. assert self.notify.debugCall(id(self))
  159. self.currentControls.setCollisionsActive(1)
  160. def collisionsOff(self):
  161. assert self.notify.debugCall(id(self))
  162. self.currentControls.setCollisionsActive(0)
  163. def placeOnFloor(self):
  164. assert self.notify.debugCall(id(self))
  165. self.currentControls.placeOnFloor()
  166. def enable(self):
  167. assert self.notify.debugCall(id(self))
  168. self.isEnabled = 1
  169. self.currentControls.enableAvatarControls()
  170. def disable(self):
  171. assert self.notify.debugCall(id(self))
  172. self.isEnabled = 0
  173. self.currentControls.disableAvatarControls()
  174. def enableAvatarJump(self):
  175. """
  176. Stop forcing the ctrl key to return 0's
  177. """
  178. assert self.notify.debugCall(id(self))
  179. self.enableJumpCounter+=1
  180. if self.enableJumpCounter:
  181. assert self.enableJumpCounter == 1
  182. self.enableJumpCounter = 1
  183. inputState.unforce("jump")
  184. def disableAvatarJump(self):
  185. """
  186. Force the ctrl key to return 0's
  187. """
  188. assert self.notify.debugCall(id(self))
  189. self.enableJumpCounter-=1
  190. if self.enableJumpCounter <= 0:
  191. inputState.force("jump", 0)
  192. def monitor(self, foo):
  193. #assert self.debugPrint("monitor()")
  194. #if 1:
  195. # airborneHeight=self.avatar.getAirborneHeight()
  196. # onScreenDebug.add("airborneHeight", "% 10.4f"%(airborneHeight,))
  197. if 0:
  198. onScreenDebug.add("InputState forward", "%d"%(inputState.isSet("forward")))
  199. onScreenDebug.add("InputState reverse", "%d"%(inputState.isSet("reverse")))
  200. onScreenDebug.add("InputState turnLeft", "%d"%(inputState.isSet("turnLeft")))
  201. onScreenDebug.add("InputState turnRight", "%d"%(inputState.isSet("turnRight")))
  202. onScreenDebug.add("InputState slideLeft", "%d"%(inputState.isSet("slideLeft")))
  203. onScreenDebug.add("InputState slideRight", "%d"%(inputState.isSet("slideRight")))
  204. return Task.cont