forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlManager.py
More file actions
executable file
·346 lines (276 loc) · 13.6 KB
/
ControlManager.py
File metadata and controls
executable file
·346 lines (276 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from direct.showbase.InputStateGlobal import inputState
#from DirectGui import *
#from PythonUtil import *
#from IntervalGlobal import *
#from otp.avatar import Avatar
from direct.directnotify import DirectNotifyGlobal
#import GhostWalker
#import GravityWalker
#import NonPhysicsWalker
#import PhysicsWalker
#if __debug__:
# import DevWalker
from direct.task import Task
from panda3d.core import ConfigVariableBool
# This is a hack, it may be better to use a line instead of a ray.
CollisionHandlerRayStart = 4000.0
class ControlManager:
notify = DirectNotifyGlobal.directNotify.newCategory("ControlManager")
wantWASD = ConfigVariableBool('want-WASD', False)
def __init__(self, enable=True, passMessagesThrough = False):
assert self.notify.debug("init control manager %s" % (passMessagesThrough))
assert self.notify.debugCall(id(self))
self.passMessagesThrough = passMessagesThrough
self.inputStateTokens = []
# Used to switch between strafe and turn. We will reset to whatever was last set.
self.WASDTurnTokens = []
self.__WASDTurn = True
self.controls = {}
self.currentControls = None
self.currentControlsName = None
self.isEnabled = 0
if enable:
self.enable()
#self.monitorTask = taskMgr.add(self.monitor, "ControlManager-%s"%(id(self)), priority=-1)
self.forceAvJumpToken = None
if self.passMessagesThrough: # for not breaking toontown
ist=self.inputStateTokens
ist.append(inputState.watchWithModifiers("forward", "arrow_up", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("reverse", "arrow_down", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
def __str__(self):
return 'ControlManager: using \'%s\'' % self.currentControlsName
def add(self, controls, name="basic"):
"""Add a control instance to the list of available control systems.
Args:
controls: an avatar control system.
name (str): any key that you want to use to refer to the controls
later (e.g. using the use(<name>) call).
See also: :meth:`use()`.
"""
assert self.notify.debugCall(id(self))
assert controls is not None
oldControls = self.controls.get(name)
if oldControls is not None:
assert self.notify.debug("Replacing controls: %s" % name)
oldControls.disableAvatarControls()
oldControls.setCollisionsActive(0)
oldControls.delete()
controls.disableAvatarControls()
controls.setCollisionsActive(0)
self.controls[name] = controls
def get(self, name):
return self.controls.get(name)
def remove(self, name):
"""Remove a control instance from the list of available control
systems.
Args:
name: any key that was used to refer to the controls when they were
added (e.g. using the add(<controls>, <name>) call).
See also: :meth:`add()`.
"""
assert self.notify.debugCall(id(self))
oldControls = self.controls.pop(name,None)
if oldControls is not None:
assert self.notify.debug("Removing controls: %s" % name)
oldControls.disableAvatarControls()
oldControls.setCollisionsActive(0)
if __debug__:
def lockControls(self):
self.ignoreUse=True
def unlockControls(self):
if hasattr(self, "ignoreUse"):
del self.ignoreUse
def use(self, name, avatar):
"""
name is a key (string) that was previously passed to add().
Use a previously added control system.
See also: :meth:`add()`.
"""
assert self.notify.debugCall(id(self))
if __debug__ and hasattr(self, "ignoreUse"):
return
controls = self.controls.get(name)
if controls is not None:
if controls is not self.currentControls:
if self.currentControls is not None:
self.currentControls.disableAvatarControls()
self.currentControls.setCollisionsActive(0)
self.currentControls.setAvatar(None)
self.currentControls = controls
self.currentControlsName = name
self.currentControls.setAvatar(avatar)
self.currentControls.setCollisionsActive(1)
if self.isEnabled:
self.currentControls.enableAvatarControls()
messenger.send('use-%s-controls'%(name,), [avatar])
#else:
# print "Controls are already", name
else:
assert self.notify.debug("Unkown controls: %s" % name)
def setSpeeds(self, forwardSpeed, jumpForce,
reverseSpeed, rotateSpeed, strafeLeft=0, strafeRight=0):
assert self.notify.debugCall(id(self))
for controls in self.controls.values():
controls.setWalkSpeed(
forwardSpeed, jumpForce, reverseSpeed, rotateSpeed)
def delete(self):
assert self.notify.debugCall(id(self))
self.disable()
for controls in list(self.controls.keys()):
self.remove(controls)
del self.controls
del self.currentControls
for token in self.inputStateTokens:
token.release()
for token in self.WASDTurnTokens:
token.release()
self.WASDTurnTokens = []
#self.monitorTask.remove()
def getSpeeds(self):
if self.currentControls:
return self.currentControls.getSpeeds()
return None
def getIsAirborne(self):
if self.currentControls:
return self.currentControls.getIsAirborne()
return False
def setTag(self, key, value):
assert self.notify.debugCall(id(self))
for controls in self.controls.values():
controls.setTag(key, value)
def deleteCollisions(self):
assert self.notify.debugCall(id(self))
for controls in self.controls.values():
controls.deleteCollisions()
def collisionsOn(self):
assert self.notify.debugCall(id(self))
if self.currentControls:
self.currentControls.setCollisionsActive(1)
def collisionsOff(self):
assert self.notify.debugCall(id(self))
if self.currentControls:
self.currentControls.setCollisionsActive(0)
def placeOnFloor(self):
assert self.notify.debugCall(id(self))
if self.currentControls:
self.currentControls.placeOnFloor()
def enable(self):
assert self.notify.debugCall(id(self))
if self.isEnabled:
assert self.notify.debug('already isEnabled')
return
self.isEnabled = 1
# keep track of what we do on the inputState so we can undo it later on
#self.inputStateTokens = []
ist = self.inputStateTokens
ist.append(inputState.watch("run", 'runningEvent', "running-on", "running-off"))
ist.append(inputState.watchWithModifiers("forward", "arrow_up", inputSource=inputState.ArrowKeys))
ist.append(inputState.watch("forward", "force-forward", "force-forward-stop"))
ist.append(inputState.watchWithModifiers("reverse", "arrow_down", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("reverse", "mouse4", inputSource=inputState.Mouse))
if self.wantWASD:
ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
ist.append(inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done"))
ist.append(inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop"))
ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
ist.append(inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done"))
ist.append(inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop"))
ist.append(inputState.watchWithModifiers("forward", "w", inputSource=inputState.WASD))
ist.append(inputState.watchWithModifiers("reverse", "s", inputSource=inputState.WASD))
ist.append(inputState.watchWithModifiers("slideLeft", "q", inputSource=inputState.QE))
ist.append(inputState.watchWithModifiers("slideRight", "e", inputSource=inputState.QE))
self.setWASDTurn(self.__WASDTurn)
else:
ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
ist.append(inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done"))
ist.append(inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop"))
ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
ist.append(inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done"))
ist.append(inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop"))
# Jump controls
if self.wantWASD:
ist.append(inputState.watchWithModifiers("jump", "space"))
else:
ist.append(inputState.watch("jump", "control", "control-up"))
if self.currentControls:
self.currentControls.enableAvatarControls()
def disable(self):
assert self.notify.debugCall(id(self))
self.isEnabled = 0
for token in self.inputStateTokens:
token.release()
self.inputStateTokens = []
for token in self.WASDTurnTokens:
token.release()
self.WASDTurnTokens = []
if self.currentControls:
self.currentControls.disableAvatarControls()
if self.passMessagesThrough: # for not breaking toontown
ist=self.inputStateTokens
ist.append(inputState.watchWithModifiers("forward", "arrow_up", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("reverse", "arrow_down", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("turnLeft", "arrow_left", inputSource=inputState.ArrowKeys))
ist.append(inputState.watchWithModifiers("turnRight", "arrow_right", inputSource=inputState.ArrowKeys))
def stop(self):
self.disable()
if self.currentControls:
self.currentControls.setCollisionsActive(0)
self.currentControls.setAvatar(None)
self.currentControls = None
def disableAvatarJump(self):
"""
prevent
"""
assert self.forceAvJumpToken is None
self.forceAvJumpToken=inputState.force(
"jump", 0, 'ControlManager.disableAvatarJump')
def enableAvatarJump(self):
"""
Stop forcing the ctrl key to return 0's
"""
assert self.forceAvJumpToken is not None
self.forceAvJumpToken.release()
self.forceAvJumpToken = None
def monitor(self, foo):
#assert self.debugPrint("monitor()")
#if 1:
# airborneHeight=self.avatar.getAirborneHeight()
# onScreenDebug.add("airborneHeight", "% 10.4f"%(airborneHeight,))
if 0:
onScreenDebug.add("InputState forward", "%d"%(inputState.isSet("forward")))
onScreenDebug.add("InputState reverse", "%d"%(inputState.isSet("reverse")))
onScreenDebug.add("InputState turnLeft", "%d"%(inputState.isSet("turnLeft")))
onScreenDebug.add("InputState turnRight", "%d"%(inputState.isSet("turnRight")))
onScreenDebug.add("InputState slideLeft", "%d"%(inputState.isSet("slideLeft")))
onScreenDebug.add("InputState slideRight", "%d"%(inputState.isSet("slideRight")))
return Task.cont
def setWASDTurn(self, turn):
self.__WASDTurn = turn
if not self.isEnabled:
return
turnLeftWASDSet = inputState.isSet("turnLeft", inputSource=inputState.WASD)
turnRightWASDSet = inputState.isSet("turnRight", inputSource=inputState.WASD)
slideLeftWASDSet = inputState.isSet("slideLeft", inputSource=inputState.WASD)
slideRightWASDSet = inputState.isSet("slideRight", inputSource=inputState.WASD)
for token in self.WASDTurnTokens:
token.release()
if turn:
self.WASDTurnTokens = (
inputState.watchWithModifiers("turnLeft", "a", inputSource=inputState.WASD),
inputState.watchWithModifiers("turnRight", "d", inputSource=inputState.WASD),
)
inputState.set("turnLeft", slideLeftWASDSet, inputSource=inputState.WASD)
inputState.set("turnRight", slideRightWASDSet, inputSource=inputState.WASD)
inputState.set("slideLeft", False, inputSource=inputState.WASD)
inputState.set("slideRight", False, inputSource=inputState.WASD)
else:
self.WASDTurnTokens = (
inputState.watchWithModifiers("slideLeft", "a", inputSource=inputState.WASD),
inputState.watchWithModifiers("slideRight", "d", inputSource=inputState.WASD),
)
inputState.set("slideLeft", turnLeftWASDSet, inputSource=inputState.WASD)
inputState.set("slideRight", turnRightWASDSet, inputSource=inputState.WASD)
inputState.set("turnLeft", False, inputSource=inputState.WASD)
inputState.set("turnRight", False, inputSource=inputState.WASD)