ControlManager.py 12 KB

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