ControlManager.py 12 KB

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