ControlManager.py 14 KB

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