-
Notifications
You must be signed in to change notification settings - Fork 209
/
character-hups.js
177 lines (160 loc) · 4.76 KB
/
character-hups.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
/* this is the character heads up player implementation.
it controls the animated dioramas that happen when players perform actions.
the HTML part of this code lives as part of the React app. */
// import * as THREE from 'three';
// import metaversefile from 'metaversefile';
import {chatManager} from './chat-manager.js';
const deadTimeoutTime = 2000;
let nextHupId = 0;
class Hup extends EventTarget {
constructor(type, parent) {
super();
this.type = type;
this.parent = parent;
this.hupId = ++nextHupId;
this.actionIds = [];
this.playerName = '';
this.fullText = '';
this.emote = null;
this.live = false;
this.deadTimeout = null;
}
static isHupAction(action) {
return action.type === 'chat';
}
mergeAction(action) {
const {playerName, message, emote} = action;
if (playerName) {
this.playerName = playerName;
}
this.actionIds.push(action.actionId);
this.clearDeadTimeout();
// this.dispatchEvent(new MessageEvent('update'));
}
async updateVoicer(message, emote) {
// this.parent.player === metaversefile.useLocalPlayer() && console.log('emit voice start');
this.dispatchEvent(new MessageEvent('voicequeue', {
data: {
message,
},
}));
const preloadedMessage = this.parent.player.voicer.preloadMessage(message);
await chatManager.waitForVoiceTurn(() => {
if (message) {
if (this.fullText.length > 0) {
this.fullText += '\n';
}
this.fullText += message;
}
this.emote = emote ?? null;
this.dispatchEvent(new MessageEvent('voicestart', {
data: {
message,
fullText: this.fullText,
},
}));
return this.parent.player.voicer.start(preloadedMessage);
});
// this.parent.player === metaversefile.useLocalPlayer() && console.log('emit voice end');
this.dispatchEvent(new MessageEvent('voiceend', {
data: {
fullText: this.fullText,
},
}));
}
unmergeAction(action) {
const index = this.actionIds.indexOf(action.actionId);
if (index !== -1) {
this.actionIds.splice(index, 1);
}
}
clearDeadTimeout() {
if (this.deadTimeout) {
clearTimeout(this.deadTimeout);
this.deadTimeout = null;
}
}
startDeadTimeout() {
this.clearDeadTimeout();
this.deadTimeout = setTimeout(() => {
this.dispatchEvent(new MessageEvent('deadtimeout'));
}, deadTimeoutTime);
}
destroy() {
this.dispatchEvent(new MessageEvent('destroy'));
}
}
class CharacterHups extends EventTarget {
constructor(player) {
super();
this.player = player;
this.hups = [];
player.addEventListener('actionadd', e => {
const {action} = e;
const {type, actionId} = action;
// console.log('action add', action);
const oldHup = this.hups.find(hup => hup.type === type);
// console.log('got old hup', oldHup, actionId, this.hups.map(h => h.actionIds).flat());
if (oldHup) {
oldHup.mergeAction(action);
oldHup.updateVoicer(action.message, action.emote);
} else if (Hup.isHupAction(action)) {
const newHup = new Hup(action.type, this);
newHup.mergeAction(action);
let pendingVoices = 0;
newHup.addEventListener('voicequeue', () => {
pendingVoices++;
newHup.clearDeadTimeout();
});
newHup.addEventListener('voiceend', () => {
if (--pendingVoices === 0) {
newHup.startDeadTimeout();
}
});
newHup.addEventListener('deadtimeout', () => {
newHup.destroy();
const index = this.hups.indexOf(newHup);
this.hups.splice(index, 1);
this.dispatchEvent(new MessageEvent('hupremove', {
data: {
player,
hup: newHup,
},
}));
});
this.hups.push(newHup);
this.dispatchEvent(new MessageEvent('hupadd', {
data: {
player,
hup: newHup,
},
}));
newHup.updateVoicer(action.message, action.emote);
}
});
player.addEventListener('actionremove', e => {
const {action} = e;
const {actionId} = action;
// console.log('action remove', action);
const oldHup = this.hups.find(hup => hup.actionIds.includes(actionId));
if (oldHup) {
oldHup.unmergeAction(action);
}
});
}
addChatHupAction(text) {
this.player.addAction({
type: 'chat',
text,
});
}
update(timestamp) {
// nothing
}
destroy() {
// nothing
}
}
export {
CharacterHups,
};