forked from TheNexusCity/ic-trade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (139 loc) · 4.46 KB
/
Copy pathindex.js
File metadata and controls
166 lines (139 loc) · 4.46 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
import metaversefile from "metaversefile";
const { useApp, useWorld, useLocalPlayer, useActivate, useLoaders, usePhysics, useCleanup } =
metaversefile;
const baseUrl = import.meta.url; // .replace(/(\/)[^\/\\]*$/, "$1");
export default (e) => {
const app = useApp();
const physics = usePhysics();
const minScale = 0;
const maxScale = 2;
const minPosition = 0;
const maxPosition = 2;
const lerpTime = 0.5;
const lerpScale = (a, b, t) => a + (b - a) * t;
const lerpPosition = (a, b, t) => a + (b - a) * t;
let activated = false;
app.name = "NXS-001 HelperBot";
window.openInWebaverse = (item) => {
// get a reference to the 'iframe' class element inside the iframe-container-2 class element
const iframe = document.querySelector('.iframe-container-2 iframe');
// set the width and height to 680px by 680px
iframe.width = '600px';
iframe.height = '400px';
// set pointer events to all
iframe.style.pointerEvents = 'all';
console.log("openInWebaverse: ", item);
console.log("item url: ", item.url);
metaversefile.createAppAsync({
start_url: item.url,
position: [app.position.x + Math.random() * .1, app.position.y + 1 + Math.random() * .1, app.position.z + 1 + Math.random() * .1],
// 90 degree y rotation quaternion
quaternion: [0, 0.7071067811865476, 0, 0.7071067811865475],
scale: [0.5, 0.5, 0.5],
}).then(newApp => {
useWorld().appManager.importApp(newApp);
})
};
window.setCurrentTradeId = (id) => {
console.log("setCurrentTradeId: ", id);
app.setComponent('trade', {id});
};
let reactAdded = false;
const addReact = async () => {
if(reactAdded) return;
reactAdded = true;
{
const u = `${baseUrl}/trade.react`;
reactApp = await metaversefile.createAppAsync({
start_url: u,
});
if (!live) {
reactApp.destroy();
return;
}
// reactApp.rotation.y = Math.PI / 2;
reactApp.scale.set(0, 0, 0);
app.add(reactApp);
reactApp.updateMatrixWorld();
}
}
const activateCb = async (activated) => {
const wearComponent = app.getComponent('wear');
const tradeComponent = app.getComponent('trade');
console.log("wearComponent: ", wearComponent);
console.log("tradeComponent: ", tradeComponent);
if(tradeComponent) {
window.tradeId = tradeComponent.id;
console.log('set window.tradeId: ', window.tradeId);
}
addReact(app);
const startTime = Date.now();
let currentTime = 0;
const timer = setInterval(() => {
if(!reactApp) return;
currentTime = Date.now();
const time = (currentTime - startTime) / 1000;
const scale = lerpScale(
activated ? minScale : maxScale,
activated ? maxScale : minScale,
time / lerpTime
);
reactApp.scale.set(scale, scale, scale);
reactApp.position.set(0, maxPosition, reactApp.position.z);
if (time > lerpTime) {
const finalScale = activated ? maxScale : minScale;
reactApp.scale.set(finalScale, finalScale, finalScale);
clearInterval(timer);
}
reactApp.updateMatrixWorld();
// if the player walks more than 5 meters away from the console, deactivate it
const player = useLocalPlayer();
const distance = player.position.distanceTo(app.position);
if (distance > 2) {
activated = false;
reactApp.scale.set(minScale, minScale, minScale);
reactApp.updateMatrixWorld();
// stop the timer
clearInterval(timer);
}
}, 1000 / 60);
};
app.addEventListener("activate", (e) => {
activateCb && activateCb(true);
});
app.addEventListener("wearupdate", (e) => {
if (!e.wear) {
activateCb && activateCb(false);
}
});
// add an event listener for the 'wear' event
let live = true;
let reactApp = null;
const physicsIds = [];
e.waitUntil(
(async () => {
const u = `${baseUrl}HelperBot.glb`;
let o = await new Promise((accept, reject) => {
const { gltfLoader } = useLoaders();
gltfLoader.load(u, accept, function onprogress() { }, reject);
});
if (!live) {
o.destroy();
return;
}
app.glb = o;
o = o.scene;
app.add(o);
const physicsId = physics.addGeometry(o);
physicsIds.push(physicsId);
})()
);
useCleanup(() => {
live = false;
reactApp && reactApp.destroy();
for (const physicsId of physicsIds) {
physics.removeGeometry(physicsId);
}
});
return app;
};