ControlManager.py 11 KB

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