ControlManager.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.controls = {}
  23. self.currentControls = None
  24. self.currentControlsName = None
  25. self.isEnabled = 1
  26. #self.monitorTask = taskMgr.add(self.monitor, "ControlManager-%s"%(id(self)), priority=-1)
  27. self.forceAvJumpToken = None
  28. # keep track of what we do on the inputState so we can undo it later on
  29. self.inputStateTokens = []
  30. ist=self.inputStateTokens
  31. ist.append(inputState.watch("run", 'runningEvent', "running-on", "running-off"))
  32. ist.append(inputState.watchWithModifiers("forward", "arrow_up", inputSource=inputState.ArrowKeys))
  33. ist.append(inputState.watch("forward", "force-forward", "force-forward-stop"))
  34. ist.append(inputState.watchWithModifiers("reverse", "arrow_down", inputSource=inputState.ArrowKeys))
  35. ist.append(inputState.watchWithModifiers("reverse", "mouse4", inputSource=inputState.Mouse))
  36. if self.wantWASD:
  37. ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
  38. ist.append(inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done"))
  39. ist.append(inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop"))
  40. ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
  41. ist.append(inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done"))
  42. ist.append(inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop"))
  43. ist.append(inputState.watchWithModifiers("forward", "w", inputSource=inputState.WASD))
  44. ist.append(inputState.watchWithModifiers("reverse", "s", inputSource=inputState.WASD))
  45. ist.append(inputState.watchWithModifiers("slideLeft", "q", inputSource=inputState.WASD))
  46. ist.append(inputState.watchWithModifiers("slideRight", "e", inputSource=inputState.WASD))
  47. # Used to switch between strafe and turn. We will default to turn
  48. self.WASDTurnTokens = ()
  49. self.setWASDTurn(1)
  50. else:
  51. ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
  52. ist.append(inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done"))
  53. ist.append(inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop"))
  54. ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
  55. ist.append(inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done"))
  56. ist.append(inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop"))
  57. # Jump controls
  58. if self.wantWASD:
  59. ist.append(inputState.watchWithModifiers("jump", "space"))
  60. else:
  61. ist.append(inputState.watch("jump", "control", "control-up"))
  62. def add(self, controls, name="basic"):
  63. """
  64. controls is an avatar control system.
  65. name is any key that you want to use to refer to the
  66. the controls later (e.g. using the use(<name>) call).
  67. Add a control instance to the list of available control systems.
  68. See also: use().
  69. """
  70. assert self.notify.debugCall(id(self))
  71. assert controls is not None
  72. oldControls = self.controls.get(name)
  73. if oldControls is not None:
  74. print "Replacing controls:", name
  75. oldControls.disableAvatarControls()
  76. oldControls.setCollisionsActive(0)
  77. oldControls.delete()
  78. controls.disableAvatarControls()
  79. controls.setCollisionsActive(0)
  80. self.controls[name] = controls
  81. def get(self, name):
  82. return self.controls.get(name)
  83. def remove(self, name):
  84. """
  85. name is any key that was used to refer to the
  86. the controls when they were added (e.g.
  87. using the add(<controls>, <name>) call).
  88. Remove a control instance from the list of
  89. available control systems.
  90. See also: add().
  91. """
  92. assert self.notify.debugCall(id(self))
  93. oldControls = self.controls.pop(name,None)
  94. if oldControls is not None:
  95. print "Removing controls:", name
  96. oldControls.disableAvatarControls()
  97. oldControls.setCollisionsActive(0)
  98. if __debug__:
  99. def lockControls(self):
  100. self.ignoreUse=True
  101. def unlockControls(self):
  102. if hasattr(self, "ignoreUse"):
  103. del self.ignoreUse
  104. def use(self, name, avatar):
  105. """
  106. name is a key (string) that was previously passed to add().
  107. Use a previously added control system.
  108. See also: add().
  109. """
  110. assert self.notify.debugCall(id(self))
  111. if __debug__ and hasattr(self, "ignoreUse"):
  112. return
  113. controls = self.controls.get(name)
  114. if controls is not None:
  115. if controls is not self.currentControls:
  116. if self.currentControls is not None:
  117. self.currentControls.disableAvatarControls()
  118. self.currentControls.setCollisionsActive(0)
  119. self.currentControls.setAvatar(None)
  120. self.currentControls = controls
  121. self.currentControlsName = name
  122. self.currentControls.setAvatar(avatar)
  123. self.currentControls.setCollisionsActive(1)
  124. if self.isEnabled:
  125. self.currentControls.enableAvatarControls()
  126. messenger.send('use-%s-controls'%(name,), [avatar])
  127. #else:
  128. # print "Controls are already", name
  129. else:
  130. print "Unkown controls:", name
  131. def setSpeeds(self, forwardSpeed, jumpForce,
  132. reverseSpeed, rotateSpeed, strafeLeft=0, strafeRight=0):
  133. assert self.notify.debugCall(id(self))
  134. for controls in self.controls.values():
  135. controls.setWalkSpeed(
  136. forwardSpeed, jumpForce, reverseSpeed, rotateSpeed)
  137. def delete(self):
  138. assert self.notify.debugCall(id(self))
  139. self.disable()
  140. for controls in self.controls.keys():
  141. self.remove(controls)
  142. del self.controls
  143. del self.currentControls
  144. for token in self.inputStateTokens:
  145. token.release()
  146. if self.wantWASD:
  147. for token in self.WASDTurnTokens:
  148. token.release()
  149. #self.monitorTask.remove()
  150. def getSpeeds(self):
  151. return self.currentControls.getSpeeds()
  152. def getIsAirborne(self):
  153. return self.currentControls.getIsAirborne()
  154. def setTag(self, key, value):
  155. assert self.notify.debugCall(id(self))
  156. for controls in self.controls.values():
  157. controls.setTag(key, value)
  158. def deleteCollisions(self):
  159. assert self.notify.debugCall(id(self))
  160. for controls in self.controls.values():
  161. controls.deleteCollisions()
  162. def collisionsOn(self):
  163. assert self.notify.debugCall(id(self))
  164. if self.currentControls:
  165. self.currentControls.setCollisionsActive(1)
  166. def collisionsOff(self):
  167. assert self.notify.debugCall(id(self))
  168. if self.currentControls:
  169. self.currentControls.setCollisionsActive(0)
  170. def placeOnFloor(self):
  171. assert self.notify.debugCall(id(self))
  172. if self.currentControls:
  173. self.currentControls.placeOnFloor()
  174. def enable(self):
  175. assert self.notify.debugCall(id(self))
  176. self.isEnabled = 1
  177. if self.currentControls:
  178. self.currentControls.enableAvatarControls()
  179. def disable(self):
  180. assert self.notify.debugCall(id(self))
  181. self.isEnabled = 0
  182. if self.currentControls:
  183. self.currentControls.disableAvatarControls()
  184. def stop(self):
  185. self.disable()
  186. if self.currentControls:
  187. self.currentControls.setCollisionsActive(0)
  188. self.currentControls.setAvatar(None)
  189. self.currentControls = None
  190. def disableAvatarJump(self):
  191. """
  192. prevent
  193. """
  194. assert self.forceAvJumpToken is None
  195. self.forceAvJumpToken=inputState.force(
  196. "jump", 0, 'ControlManager.disableAvatarJump')
  197. def enableAvatarJump(self):
  198. """
  199. Stop forcing the ctrl key to return 0's
  200. """
  201. assert self.forceAvJumpToken is not None
  202. self.forceAvJumpToken.release()
  203. self.forceAvJumpToken = None
  204. def monitor(self, foo):
  205. #assert self.debugPrint("monitor()")
  206. #if 1:
  207. # airborneHeight=self.avatar.getAirborneHeight()
  208. # onScreenDebug.add("airborneHeight", "% 10.4f"%(airborneHeight,))
  209. if 0:
  210. onScreenDebug.add("InputState forward", "%d"%(inputState.isSet("forward")))
  211. onScreenDebug.add("InputState reverse", "%d"%(inputState.isSet("reverse")))
  212. onScreenDebug.add("InputState turnLeft", "%d"%(inputState.isSet("turnLeft")))
  213. onScreenDebug.add("InputState turnRight", "%d"%(inputState.isSet("turnRight")))
  214. onScreenDebug.add("InputState slideLeft", "%d"%(inputState.isSet("slideLeft")))
  215. onScreenDebug.add("InputState slideRight", "%d"%(inputState.isSet("slideRight")))
  216. return Task.cont
  217. def setWASDTurn(self, turn):
  218. turnLeftSet = inputState.isSet("turnLeft")
  219. turnRightSet = inputState.isSet("turnRight")
  220. slideLeftSet = inputState.isSet("slideLeft")
  221. slideRightSet = inputState.isSet("slideRight")
  222. for token in self.WASDTurnTokens:
  223. token.release()
  224. if turn:
  225. self.WASDTurnTokens = (
  226. inputState.watchWithModifiers("turnLeft", "a", inputSource=inputState.WASD),
  227. inputState.watchWithModifiers("turnRight", "d", inputSource=inputState.WASD),
  228. )
  229. else:
  230. self.WASDTurnTokens = (
  231. inputState.watchWithModifiers("slideLeft", "a", inputSource=inputState.WASD),
  232. inputState.watchWithModifiers("slideRight", "d", inputSource=inputState.WASD),
  233. )
  234. """
  235. inputState.set("turnLeft", turnLeftSet, inputSource=inputState.WASD)
  236. inputState.set("turnRight", turnRightSet, inputSource=inputState.WASD)
  237. inputState.set("slideLeft", slideLeftSet, inputSource=inputState.WASD)
  238. inputState.set("slideRight", slideRightSet, inputSource=inputState.WASD)
  239. """