ControlManager.py 12 KB

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