-
Notifications
You must be signed in to change notification settings - Fork 0
/
walkManager.js
67 lines (57 loc) · 1.93 KB
/
walkManager.js
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
import {ViewService} from "./worldcore";
export class WalkManager extends ViewService {
constructor(name) {
super(name || "WalkManager");
this.walkers = [];
}
setupDefaultWalkers() {
[
["BuiltinWalker", "WalkerPawn", "checkPortal"],
["BuiltinWalker", "WalkerPawn", "backoutFromFall"],
["BuiltinWalker", "WalkerPawn", "bvh"]
].forEach((spec) => this.append(spec));
}
append(walkerSpec) {
// [ModuleName, BehaviorName, methodName]
if (walkerSpec.length !== 3) {
throw new Error(`walker spec should be in the form of "[ModuleName, BehaviorName, methodName]"`);
}
this.walkers.push(walkerSpec);
}
insertBefore(walkerSpec, prevSpec) {
if (!prevSpec) {
return this.append(walkerSpec);
}
let index = this.findIndex(prevSpec);
if (index >= 0) {
if (walkerSpec.length !== 3) {
throw new Error(`walker spec should be in the form of "[ModuleName, BehaviorName, methodName]"`);
}
this.walkers.splice(index, 0, walkerSpec);
}
}
remove(walkerSpec) {
let index = this.findIndex(walkerSpec);
if (index >= 0) {
this.walkers.splice(index, 1);
}
}
findIndex(walkerSpec) {
return this.walkers.findIndex((a) => a[0] == `${walkerSpec[0]}$${walkerSpec[1]}` && a[2] === walkerSpec[2]);
}
removeAll() {
this.walkers = [];
}
walk(avatar, vq, time, delta) {
for (let i = 0; i < this.walkers.length; i++) {
let walker = this.walkers[i];
let behavior = avatar.actor.behaviorManager.lookup(walker[0], walker[1]);
let [newVq, isFinal] = behavior.invoke(avatar, walker[2], vq, time, delta);
if (isFinal) {
return newVq;
}
vq = newVq;
}
return vq;
}
}