ControlManager.py 14 KB

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