-
Notifications
You must be signed in to change notification settings - Fork 22
/
CharacterControl.py
407 lines (304 loc) · 15.3 KB
/
CharacterControl.py
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import avango
import avango.script
from avango.gua.skelanim.AnimationControl import AnimationControl
from avango.gua.skelanim.AnimationControl import AnimationConfig
import avango.daemon
import math
class CharacterControl(avango.script.Script):
XBOX_X = avango.SFFloat()
XBOX_Y = avango.SFFloat()
XBOX_BTN_A = avango.SFFloat()
XBOX_BTN_X = avango.SFFloat()
FPS = 60.0
def __init__(self):
self.super(CharacterControl).__init__()
# animations
self._animation_control = AnimationControl()
self._queued_animations = []
self._on_animation_end = {}
# keys
self._pressed_keys = []
self._animations_kd = []
self._animations_ku = []
# transformations
self._transformations = []
self._delta_transformations = []
self._translations = {}
self._rotations = []
self._current_translation = avango.gua.Vec3(0.0, 0.0, 0.0)
self._last_translation = avango.gua.Vec3(0.0, 0.0, 0.0)
# wall detection
self._scene_graph = None
self._ray = avango.gua.nodes.Ray()
self._wall_detect_height = 0.0
self._wall_detect_offset = 0.0
self._wall_detected = False
self._wall_detect_idle = None
self._xbox_events = []
self._xbox_animation_speeds = {}
self._listen_keyboard = True
self._timer = avango.nodes.TimeSensor()
self._last_time_rot = self._timer.Time.value
self._last_time_trans = self._timer.Time.value
self._init_nav_transform = avango.gua.make_identity_mat()
def my_constructor(self, character_node, navigation_node,
start_animation_config,
application_window=avango.gua.nodes.GlfwWindow()):
# character node
self._character = character_node
# navigation node
self._navigation = navigation_node
self._init_nav_transform = navigation_node.Transform.value
self._animation_control.my_constructor(character_node,
start_animation_config)
self.always_evaluate(True)
application_window.on_key_press(self._handle_key)
def reset_transform(self):
self._navigation.Transform.value = self._init_nav_transform
def listen_keyboard(self, do_listen=True):
self._listen_keyboard = do_listen
def bind_transformation(self, key_nr, current_animation_name,
transform_matrix):
delta_list_id = len(self._delta_transformations)
self._delta_transformations.append(avango.gua.make_identity_mat())
self._transformations.append((key_nr, current_animation_name,
delta_list_id, transform_matrix))
def bind_rotation(self, key_nr, current_animation_name, rotation_vec):
self._rotations.append((key_nr, current_animation_name, rotation_vec))
def on_key_down(self, key_nr, current_animation_name,
next_animation_config, blend_duration=0.5):
self._animations_kd.append((key_nr, current_animation_name,
next_animation_config, blend_duration))
def on_key_up(self, key_nr, current_animation_name, next_animation_config,
blend_duration=0.5):
self._animations_ku.append((key_nr, current_animation_name,
next_animation_config, blend_duration))
def on_animation_end(self, animation_name, next_animation_config,
blend_duration=0.5):
self._on_animation_end[animation_name] = \
(next_animation_config, blend_duration)
def bind_translation(self, animation_name, translation_vec):
self._translations[animation_name] = translation_vec
def activate_wall_detection(self, dir_offset, height_offset,
idle_animation_name, scene_graph):
self._wall_detect_offset = dir_offset
self._wall_detect_height = height_offset
self._wall_detect_idle = AnimationConfig(idle_animation_name)
self._scene_graph = scene_graph
def queue_animation(self, animation_config, blend_duration=0.5):
self._queued_animations.append((animation_config, blend_duration))
def get_current_animation(self):
return self._animation_control.get_current_animation()
def xbox_override_key(self, field, key, threshold=0.0, multiplier=1.0):
self._xbox_events.append((field, key, threshold, multiplier))
def xbox_animation_speed(self, field, animation_name):
self._xbox_animation_speeds[animation_name] = field
def evaluate(self):
self._check_xbox_overrides()
self._check_animation_loop()
# add transformation delta from keys
for mat in self._delta_transformations:
self._navigation.Transform.value = \
self._navigation.Transform.value * mat
delta_time = self._timer.Time.value - self._last_time_rot
self._last_time_rot = self._timer.Time.value
# add rotation from keys
tmp_rot = avango.gua.make_identity_mat()
for rot in self._rotations:
if rot[0] in self._pressed_keys and \
self.get_current_animation().name == rot[1]:
tmp_vec = avango.gua.Vec4(rot[2].x,
rot[2].y,
rot[2].z,
rot[2].w)
# time dependent:
tmp_vec.x *= delta_time * self.FPS
#xbox_controller:
if math.fabs(self.XBOX_X.value) > 0.0:
tmp_vec.x *= math.fabs(self.XBOX_X.value)
tmp_rot = avango.gua.make_rot_mat(tmp_vec)
self._navigation.Transform.value = \
self._navigation.Transform.value * tmp_rot
self._blend_translations()
def switch_animation(self, animation_config):
# look for already pressed keys with propreitary current animation
# abort blending if already pressed key changed state
if self._check_pressed_keys(animation_config.name):
return
next_animation_blending = \
self._check_on_animation_end(animation_config.name)
tmp_last_trans = self._last_translation
self._apply_animation_changes(self.get_current_animation(),
animation_config)
wall_detected = self._blend_translations()
if not wall_detected:
self._animation_control.switch_to(animation_config)
# queue next animation
if next_animation_blending is not None:
self.queue_animation(next_animation_blending[0],
next_animation_blending[1])
elif(self.get_current_animation() is not None and
self._wall_detect_idle is not None and
self.get_current_animation().name != self._wall_detect_idle.name):
self._apply_animation_changes(self.get_current_animation(),
self._wall_detect_idle)
self._animation_control.switch_to(self._wall_detect_idle)
self._pressed_keys = []
if wall_detected:
self._current_translation = self._last_translation
self._last_translation = tmp_last_trans
self._pressed_keys = []
def blend_animation(self, animation_config, blending_duration=0.5):
# look for already pressed keys with propreitary current animation
# abort blending if already pressed key changed state
if self._check_pressed_keys(animation_config.name):
return
if blending_duration < 0.001:
self.switch_animation(animation_config)
return
next_animation_blending = \
self._check_on_animation_end(animation_config.name)
tmp_last_trans = self._last_translation
self._apply_animation_changes(self.get_current_animation(),
animation_config)
wall_detected = self._blend_translations()
if not wall_detected:
self._animation_control.blend_to(animation_config,
blending_duration)
# queue next animation
if next_animation_blending is not None:
self.queue_animation(next_animation_blending[0],
next_animation_blending[1])
elif(self.get_current_animation() is not None and
self._wall_detect_idle is not None and
self.get_current_animation().name != self._wall_detect_idle.name):
self._apply_animation_changes(self.get_current_animation(),
self._wall_detect_idle)
self._animation_control.blend_to(self._wall_detect_idle)
self._pressed_keys = []
if wall_detected:
self._current_translation = self._last_translation
self._last_translation = tmp_last_trans
self._pressed_keys = []
def _handle_key(self, ascii, unknown, event, unknown2,
animation_name=None):
if self._listen_keyboard:
# additional animation parameter for proprietary animation states
if(animation_name is None and
self.get_current_animation() is not None):
animation_name = self.get_current_animation().name
# animation trigger key down
if event == 1:
if ascii not in self._pressed_keys:
self._pressed_keys.append(ascii)
for a in self._animations_kd:
if ascii == a[0] and animation_name == a[1]:
self.blend_animation(a[2], a[3])
# animation trigger key up
if event == 0:
if ascii in self._pressed_keys:
self._pressed_keys.remove(ascii)
for a in self._animations_ku:
if ascii == a[0] and animation_name == a[1]:
self.blend_animation(a[2], a[3])
# transformation trigger
for t in self._transformations:
if ascii == t[0] and event == 1 and animation_name == t[1]:
self._delta_transformations[t[2]] = t[3]
if ascii == t[0] and event == 0 and animation_name == t[1]:
self._delta_transformations[t[2]] = \
avango.gua.make_identity_mat()
def _blend_translations(self):
# blend transformations binded to animations
blendFact = self._animation_control.get_blending_factor()
trans_vec = (self._last_translation * (1-blendFact)) +\
(self._current_translation * blendFact)
# time dependent
delta_time = self._timer.Time.value - self._last_time_trans
self._last_time_trans = self._timer.Time.value
trans_vec *= delta_time * self.FPS
#xbox_controller:
if math.fabs(self.XBOX_Y.value) > 0.0:
trans_vec.z *= math.fabs(self.XBOX_Y.value)
if math.fabs(self.XBOX_X.value) > 0.0:
trans_vec.x *= math.fabs(self.XBOX_X.value)
# translation delta in world coordinate system:
before_trans = self._navigation.WorldTransform.value.get_translate()
new_navigation_transform = self._navigation.WorldTransform.value *\
avango.gua.make_trans_mat(trans_vec)
after_trans = new_navigation_transform.get_translate()
delta_trans = after_trans - before_trans
if not self._wall_detection(delta_trans):
self._navigation.Transform.value = \
self._navigation.Transform.value *\
avango.gua.make_trans_mat(trans_vec)
return False
elif(self.get_current_animation() is not None and
self._wall_detect_idle is not None and
self.get_current_animation().name != self._wall_detect_idle.name):
self._pressed_keys = []
self._apply_animation_changes(self.get_current_animation(),
self._wall_detect_idle)
self._animation_control.blend_to(self._wall_detect_idle)
return True
def _wall_detection(self, delta_translation):
#wall detection
if self._scene_graph and delta_translation.length() > 0.00001:
delta_norm = avango.gua.Vec3(delta_translation)
delta_norm.normalize()
in_translation = \
self._character.WorldTransform.value.get_translate()
# + (delta_norm * self._wall_detect_offset)
in_translation.y += self._wall_detect_height
self._ray.Origin.value = in_translation
#self._ray.Direction.value = delta_translation
self._ray.Direction.value = delta_norm * self._wall_detect_offset
results = self._scene_graph.ray_test(self._ray, 0)
return len(results.value) > 0
else:
return False
def _check_animation_loop(self):
if(not self._animation_control.is_looping() and
len(self._queued_animations) > 0):
queued = self._queued_animations.pop(0)
self.blend_animation(queued[0], queued[1])
def _check_on_animation_end(self, animation_name):
next_animation_blending = None
if animation_name in self._on_animation_end:
next_animation_blending = self._on_animation_end[animation_name]
return next_animation_blending
def _check_pressed_keys(self, next_animation_name):
current_animation = self.get_current_animation()
if current_animation is not None:
for ascii in self._pressed_keys:
self._handle_key(ascii, None, 1, None, next_animation_name)
if(self.get_current_animation() is not None and
current_animation.name !=
self.get_current_animation().name):
return True
return False
def _check_xbox_overrides(self):
for tup in self._xbox_events:
if (tup[0].value*tup[3]) > (tup[2]*tup[3]):
if not tup[1] in self._pressed_keys:
self._handle_key(tup[1], None, 1, None)
elif tup[1] in self._pressed_keys:
self._handle_key(tup[1], None, 0, None)
cur_anim = self.get_current_animation()
if(cur_anim is not None and
cur_anim.name in self._xbox_animation_speeds):
self._animation_control._current_animation.speed = \
math.fabs(self._xbox_animation_speeds[cur_anim.name].value)
def _apply_animation_changes(self, last_animation, current_animation):
if last_animation.name in self._translations:
self._last_translation = self._translations[last_animation.name]
else:
self._last_translation = avango.gua.Vec3(0.0, 0.0, 0.0)
if(current_animation is not None and
current_animation.name in self._translations):
self._current_translation = \
self._translations[current_animation.name]
else:
self._current_translation = avango.gua.Vec3(0.0, 0.0, 0.0)
for index in range(0, len(self._delta_transformations)):
self._delta_transformations[index] = avango.gua.make_identity_mat()