-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
214 lines (186 loc) · 6.62 KB
/
index.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
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
import * as THREE from 'three';
import metaversefile from 'metaversefile';
const {useApp, useFrame, useActivate, useLocalPlayer, useVoices, useChatManager, useLoreAI, useLoreAIScene, useAvatarAnimations, useNpcManager, useScene, usePhysics, useCleanup} = metaversefile;
const localVector = new THREE.Vector3();
export default e => {
const app = useApp();
const scene = useScene();
const npcManager = useNpcManager();
const localPlayer = useLocalPlayer();
const physics = usePhysics();
const chatManager = useChatManager();
const loreAIScene = useLoreAIScene();
const voices = useVoices();
const animations = useAvatarAnimations();
const hurtAnimation = animations.find(a => a.isHurt);
const hurtAnimationDuration = hurtAnimation.duration;
const mode = app.getComponent('mode') ?? 'attached';
if (mode === 'attached') {
const npcName = app.getComponent('name') ?? 'Anon';
const npcVoiceName = app.getComponent('voice') ?? 'Shining armor';
const npcBio = app.getComponent('bio') ?? 'A generic avatar.';
const npcAvatarUrl = app.getComponent('avatarUrl') ?? `/avatars/Drake_hacker_v6_Guilty.vrm`;
let npcWear = app.getComponent('wear') ?? [];
if (!Array.isArray(npcWear)) {
npcWear = [npcWear];
}
let live = true;
let vrmApp = null;
let npcPlayer = null;
e.waitUntil((async () => {
const u2 = npcAvatarUrl;
const m = await metaversefile.import(u2);
if (!live) return;
vrmApp = metaversefile.createApp({
name: u2,
});
vrmApp.matrixWorld.copy(app.matrixWorld);
vrmApp.matrix.copy(app.matrixWorld)
.decompose(vrmApp.position, vrmApp.quaternion, vrmApp.scale);
vrmApp.name = 'npc';
vrmApp.setComponent('physics', true);
vrmApp.setComponent('activate', true);
await vrmApp.addModule(m);
if (!live) return;
const position = vrmApp.position.clone()
.add(new THREE.Vector3(0, 1, 0));
const {quaternion, scale} = vrmApp;
const newNpcPlayer = await npcManager.createNpc({
name: npcName,
avatarApp: vrmApp,
position,
quaternion,
scale,
});
if (!live) return;
const _updateWearables = async () => {
const wearablePromises = npcWear.map(wear => (async () => {
const {start_url} = wear;
const app = await metaversefile.createAppAsync({
start_url,
});
if (!live) return;
newNpcPlayer.wear(app);
})());
await wearablePromises;
};
await _updateWearables();
if (!live) return;
const _setVoice = () => {
const voice = voices.voiceEndpoints.find(v => v.name === npcVoiceName);
if (voice) {
newNpcPlayer.setVoiceEndpoint(voice.drive_id);
} else {
console.warn('unknown voice name', npcVoiceName, voices.voiceEndpoints);
}
};
_setVoice();
scene.add(vrmApp);
npcPlayer = newNpcPlayer;
})());
app.getPhysicsObjects = () => npcPlayer ? [npcPlayer.characterController] : [];
app.addEventListener('hit', e => {
if (!npcPlayer.hasAction('hurt')) {
const newAction = {
type: 'hurt',
animation: 'pain_back',
};
npcPlayer.addAction(newAction);
setTimeout(() => {
npcPlayer.removeAction('hurt');
}, hurtAnimationDuration * 1000);
}
});
let targetSpec = null;
useActivate(() => {
// console.log('activate npc');
if (targetSpec?.object !== localPlayer) {
targetSpec = {
type: 'follow',
object: localPlayer,
};
} else {
targetSpec = null;
}
});
/* console.log('got deets', {
npcName,
npcVoice,
npcBio,
npcAvatarUrl,
}); */
const character = loreAIScene.addCharacter({
name: npcName,
bio: npcBio,
});
// console.log('got character', character);
character.addEventListener('say', e => {
console.log('got character say', e.data);
const {message, emote, action, object, target} = e.data;
chatManager.addPlayerMessage(npcPlayer, message);
if (emote === 'supersaiyan' || action === 'supersaiyan' || /supersaiyan/i.test(object) || /supersaiyan/i.test(target)) {
const newSssAction = {
type: 'sss',
};
npcPlayer.addAction(newSssAction);
} else if (action === 'follow' || (object === 'none' && target === localPlayer.name)) { // follow player
targetSpec = {
type: 'follow',
object: localPlayer,
};
} else if (action === 'stop') { // stop
targetSpec = null;
} else if (action === 'moveto' || (object !== 'none' && target === 'none')) { // move to object
console.log('move to object', object);
/* target = localPlayer;
targetType = 'follow'; */
} else if (action === 'moveto' || (object === 'none' && target !== 'none')) { // move to player
// console.log('move to', object);
targetSpec = {
type: 'moveto',
object: localPlayer,
};
} else if (['pickup', 'grab', 'take', 'get'].includes(action)) { // pick up object
console.log('pickup', action, object, target);
} else if (['use', 'activate'].includes(action)) { // use object
console.log('use', action, object, target);
}
});
const slowdownFactor = 0.4;
const walkSpeed = 0.075 * slowdownFactor;
const runSpeed = walkSpeed * 8;
const speedDistanceRate = 0.07;
useFrame(({timestamp, timeDiff}) => {
if (npcPlayer && physics.getPhysicsEnabled()) {
if (targetSpec) {
const target = targetSpec.object;
const v = localVector.setFromMatrixPosition(target.matrixWorld)
.sub(npcPlayer.position);
v.y = 0;
const distance = v.length();
if (targetSpec.type === 'moveto' && distance < 2) {
targetSpec = null;
} else {
const speed = Math.min(Math.max(walkSpeed + ((distance - 1.5) * speedDistanceRate), 0), runSpeed);
v.normalize()
.multiplyScalar(speed * timeDiff);
npcPlayer.characterPhysics.applyWasd(v);
}
}
npcPlayer.eyeballTarget.copy(localPlayer.position);
npcPlayer.eyeballTargetEnabled = true;
npcPlayer.updatePhysics(timestamp, timeDiff);
npcPlayer.updateAvatar(timestamp, timeDiff);
}
});
useCleanup(() => {
live = false;
scene.remove(vrmApp);
if (npcPlayer) {
npcPlayer.destroy();
}
loreAIScene.removeCharacter(character);
});
}
return app;
};