ControlManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. # Jump controls
  100. if self.wantWASD:
  101. inputState.watch("jump", "space", "space-up")
  102. else:
  103. inputState.watch("jump", "control", "control-up")
  104. def add(self, controls, name="basic"):
  105. """
  106. controls is an avatar control system.
  107. name is any key that you want to use to refer to the
  108. the controls later (e.g. using the use(<name>) call).
  109. Add a control instance to the list of available control systems.
  110. See also: use().
  111. """
  112. assert self.notify.debugCall(id(self))
  113. assert controls is not None
  114. oldControls = self.controls.get(name)
  115. if oldControls is not None:
  116. print "Replacing controls:", name
  117. oldControls.disableAvatarControls()
  118. oldControls.setCollisionsActive(0)
  119. oldControls.delete()
  120. controls.disableAvatarControls()
  121. controls.setCollisionsActive(0)
  122. self.controls[name] = controls
  123. def remove(self, name):
  124. """
  125. name is any key that was used to refer to the
  126. the controls when they were added (e.g.
  127. using the add(<controls>, <name>) call).
  128. Remove a control instance from the list of available control systems.
  129. See also: add().
  130. """
  131. assert self.notify.debugCall(id(self))
  132. oldControls = self.controls.get(name)
  133. if oldControls is not None:
  134. print "Removing controls:", name
  135. oldControls.disableAvatarControls()
  136. oldControls.setCollisionsActive(0)
  137. oldControls.delete()
  138. del self.controls[name]
  139. if __debug__:
  140. def lockControls(self):
  141. self.ignoreUse=True
  142. def unlockControls(self):
  143. if hasattr(self, "ignoreUse"):
  144. del self.ignoreUse
  145. def use(self, name, avatar):
  146. """
  147. name is a key (string) that was previously passed to add().
  148. Use a previously added control system.
  149. See also: add().
  150. """
  151. assert self.notify.debugCall(id(self))
  152. if __debug__ and hasattr(self, "ignoreUse"):
  153. return
  154. controls = self.controls.get(name)
  155. if controls is not None:
  156. if controls is not self.currentControls:
  157. if self.currentControls is not None:
  158. self.currentControls.disableAvatarControls()
  159. self.currentControls.setCollisionsActive(0)
  160. self.currentControls.setAvatar(None)
  161. self.currentControls = controls
  162. self.currentControls.setAvatar(avatar)
  163. self.currentControls.setCollisionsActive(1)
  164. if self.isEnabled:
  165. self.currentControls.enableAvatarControls()
  166. messenger.send('use-%s-controls'%(name,), [avatar])
  167. #else:
  168. # print "Controls are already", name
  169. else:
  170. print "Unkown controls:", name
  171. def setSpeeds(self, forwardSpeed, jumpForce,
  172. reverseSpeed, rotateSpeed):
  173. assert self.notify.debugCall(id(self))
  174. for controls in self.controls.values():
  175. controls.setWalkSpeed(
  176. forwardSpeed, jumpForce, reverseSpeed, rotateSpeed)
  177. def delete(self):
  178. assert self.notify.debugCall(id(self))
  179. self.disable()
  180. del self.controls
  181. del self.currentControls
  182. inputState.ignore("forward")
  183. inputState.ignore("reverse")
  184. inputState.ignore("turnLeft")
  185. inputState.ignore("turnRight")
  186. inputState.ignore("jump")
  187. inputState.ignore("run")
  188. if self.wantWASD:
  189. inputState.ignore("slideLeft")
  190. inputState.ignore("slideRight")
  191. #self.monitorTask.remove()
  192. def getSpeeds(self):
  193. return self.currentControls.getSpeeds()
  194. def setTag(self, key, value):
  195. assert self.notify.debugCall(id(self))
  196. for controls in self.controls.values():
  197. controls.setTag(key, value)
  198. def deleteCollisions(self):
  199. assert self.notify.debugCall(id(self))
  200. for controls in self.controls.values():
  201. controls.deleteCollisions()
  202. def collisionsOn(self):
  203. assert self.notify.debugCall(id(self))
  204. self.currentControls.setCollisionsActive(1)
  205. def collisionsOff(self):
  206. assert self.notify.debugCall(id(self))
  207. self.currentControls.setCollisionsActive(0)
  208. def placeOnFloor(self):
  209. assert self.notify.debugCall(id(self))
  210. self.currentControls.placeOnFloor()
  211. def enable(self):
  212. assert self.notify.debugCall(id(self))
  213. self.isEnabled = 1
  214. self.currentControls.enableAvatarControls()
  215. def disable(self):
  216. assert self.notify.debugCall(id(self))
  217. self.isEnabled = 0
  218. self.currentControls.disableAvatarControls()
  219. def enableAvatarJump(self):
  220. """
  221. Stop forcing the ctrl key to return 0's
  222. """
  223. assert self.notify.debugCall(id(self))
  224. self.enableJumpCounter+=1
  225. if self.enableJumpCounter:
  226. assert self.enableJumpCounter == 1
  227. self.enableJumpCounter = 1
  228. inputState.unforce("jump")
  229. def disableAvatarJump(self):
  230. """
  231. Force the ctrl key to return 0's
  232. """
  233. assert self.notify.debugCall(id(self))
  234. self.enableJumpCounter-=1
  235. if self.enableJumpCounter <= 0:
  236. inputState.force("jump", 0)
  237. def monitor(self, foo):
  238. #assert(self.debugPrint("monitor()"))
  239. #if 1:
  240. # airborneHeight=self.avatar.getAirborneHeight()
  241. # onScreenDebug.add("airborneHeight", "% 10.4f"%(airborneHeight,))
  242. if 0:
  243. onScreenDebug.add("InputState forward", "%d"%(inputState.isSet("forward")))
  244. onScreenDebug.add("InputState reverse", "%d"%(inputState.isSet("reverse")))
  245. onScreenDebug.add("InputState turnLeft", "%d"%(inputState.isSet("turnLeft")))
  246. onScreenDebug.add("InputState turnRight", "%d"%(inputState.isSet("turnRight")))
  247. return Task.cont