Skip to content
This repository was archived by the owner on Jan 24, 2020. It is now read-only.

Commit 3cacb81

Browse files
author
Avaer Kazmer
committed
Scene token tracking cleanup
1 parent df49c81 commit 3cacb81

File tree

1 file changed

+119
-102
lines changed

1 file changed

+119
-102
lines changed

index.html

Lines changed: 119 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,26 +1886,28 @@
18861886
},
18871887
});
18881888
} else {
1889-
return data.tokens[_getKey(x, z)] || {
1890-
owner: ONE_ADDRESS,
1889+
const token = data.tokens[_getKey(x, z)] || {
1890+
owner: NULL_ADDRESS,
18911891
id: 0,
18921892
x,
18931893
y: z,
18941894
lastTimestamp: 0,
1895-
sceneId: 0,
18961895
};
1897-
}
1898-
};
1899-
const _getScene = async sceneId => {
1900-
if (_isInMultiverse()) {
1901-
return await user.execute({
1902-
method: 'getScene',
1903-
data: {
1904-
sceneId,
1905-
},
1906-
});
1907-
} else {
1908-
return data.scenes[sceneId];
1896+
token.scene = (() => {
1897+
for (const k in data.scenes) {
1898+
const scene = data.scenes[k];
1899+
if (scene.coords.some(coord => coord[0] === x && coord[1] === z)) {
1900+
return scene;
1901+
}
1902+
}
1903+
return {
1904+
id: 0,
1905+
coords: [[x, z]],
1906+
apps: [],
1907+
owner: NULL_ADDRESS,
1908+
};
1909+
})();
1910+
return token;
19091911
}
19101912
};
19111913
const _setScene = async (coords, apps) => {
@@ -1958,12 +1960,27 @@
19581960
id: sceneId,
19591961
coords,
19601962
apps,
1963+
owner: _getUserAddress(),
19611964
};
19621965
data.scenes[sceneId] = scene;
19631966
_saveData();
19641967
return sceneId;
19651968
}
19661969
};
1970+
const _setSceneApps = async (sceneId, apps) => {
1971+
if (_isInMultiverse()) {
1972+
return await user.execute({
1973+
method: 'setSceneApps',
1974+
data: {
1975+
sceneId,
1976+
apps,
1977+
},
1978+
});
1979+
} else {
1980+
data.scenes[sceneId].apps = apps;
1981+
_saveData();
1982+
}
1983+
};
19671984
const _makeSceneObjectMesh = () => {
19681985
const object = new THREE.Object3D();
19691986
object.token = null;
@@ -2012,11 +2029,12 @@
20122029
id: 0,
20132030
coords: [[x, z]],
20142031
apps: [],
2032+
owner: NULL_ADDRESS,
20152033
};
20162034
_getToken(x, z)
20172035
.then(token => {
2018-
const {sceneId, owner} = token;
2019-
const _isValid = () => !sceneId || !sceneMeshes.some(sceneMesh => sceneMesh.token.id === sceneId);
2036+
const {scene: sceneSpec, owner} = token;
2037+
const _isValid = () => !sceneSpec.id || !sceneMeshes.some(sceneMesh => sceneMesh.token.id === sceneSpec.id);
20202038
if (_isValid()) {
20212039
const _setDefaultSubMesh = () => {
20222040
if (_isInMultiverse() && owner === NULL_ADDRESS) {
@@ -2028,53 +2046,48 @@
20282046
}
20292047
};
20302048

2031-
if (sceneId) {
2032-
object.token.id = sceneId;
2033-
_getScene(sceneId)
2034-
.then(token => {
2035-
object.token = token;
2036-
const {coords, apps} = token;
2037-
object.token.apps = []; // re-added below below
2038-
2039-
for (let i = 0; i < apps.length; i++) {
2040-
const app = apps[i];
2041-
const {appType: type, url, position, orientation} = app;
2042-
const iconMesh = _makeIconMesh(url, faviconImg, type, () => {
2043-
selectedIconCoord.set(iconMesh.x, iconMesh.y);
2044-
trayMesh.labelMesh.textMesh.setText(iconMesh.url);
2045-
}, i => {
2046-
_moveApp(iconMesh, i);
2047-
}, () => {
2048-
_removeApp(iconMesh);
2049-
_destroyApp(iconMesh)
2050-
}, true);
2051-
_addApp(iconMesh);
2052-
iconMesh.moveMesh.position.fromArray(position);
2053-
iconMesh.moveMesh.quaternion.fromArray(orientation);
2054-
iconMesh.iframe.position = position;
2055-
iconMesh.iframe.orientation = orientation;
2056-
iconMesh.setSceneBinding(object);
2057-
}
2058-
2059-
console.log('apps vs', apps.length, object.token.apps.length);
2049+
object.token = sceneSpec;
2050+
if (sceneSpec.id) {
2051+
const {coords, apps} = sceneSpec;
2052+
object.token.apps = []; // re-added below below
2053+
2054+
for (let i = 0; i < apps.length; i++) {
2055+
const app = apps[i];
2056+
const {appType: type, url, position, orientation} = app;
2057+
const iconMesh = _makeIconMesh(url, faviconImg, type, () => {
2058+
selectedIconCoord.set(iconMesh.x, iconMesh.y);
2059+
trayMesh.labelMesh.textMesh.setText(iconMesh.url);
2060+
}, i => {
2061+
_moveApp(iconMesh, i);
2062+
}, () => {
2063+
_removeApp(iconMesh);
2064+
_destroyApp(iconMesh)
2065+
}, true);
2066+
_addApp(iconMesh);
2067+
iconMesh.moveMesh.position.fromArray(position);
2068+
iconMesh.moveMesh.quaternion.fromArray(orientation);
2069+
iconMesh.iframe.position = position;
2070+
iconMesh.iframe.orientation = orientation;
2071+
iconMesh.setSceneBindingRaw(object);
2072+
console.log('load app', app, object.apps, object.token.apps);
2073+
}
20602074

2061-
if (owner !== NULL_ADDRESS) {
2062-
for (let i = 0; i < coords.length; i++) {
2063-
const [x, z] = coords[i];
2064-
const sceneSubMesh = _makeMiningPlaceholderMesh(x, z, owner);
2065-
object.add(sceneSubMesh);
2066-
}
2067-
} else if (_isInMultiverse()) {
2068-
for (let i = 0; i < coords.length; i++) {
2069-
const [x, z] = coords[i];
2070-
const sceneSubMesh = _makeMiningMesh(x, z);
2071-
object.add(sceneSubMesh);
2072-
}
2073-
}
2075+
if (owner !== NULL_ADDRESS) {
2076+
for (let i = 0; i < coords.length; i++) {
2077+
const [x, z] = coords[i];
2078+
const sceneSubMesh = _makeMiningPlaceholderMesh(x, z, owner);
2079+
object.add(sceneSubMesh);
2080+
}
2081+
} else if (_isInMultiverse()) {
2082+
for (let i = 0; i < coords.length; i++) {
2083+
const [x, z] = coords[i];
2084+
const sceneSubMesh = _makeMiningMesh(x, z);
2085+
object.add(sceneSubMesh);
2086+
}
2087+
}
20742088

2075-
lastSceneCoords[0] = NaN;
2076-
lastSceneCoords[1] = NaN;
2077-
});
2089+
lastSceneCoords[0] = NaN;
2090+
lastSceneCoords[1] = NaN;
20782091
} else {
20792092
_setDefaultSubMesh();
20802093
}
@@ -2833,59 +2846,63 @@
28332846
}],
28342847
[saveImg, 4, 2, () => {
28352848
if (selectedSceneToken) {
2836-
const {coords} = selectedSceneToken;
2849+
const coords = selectedSceneToken.coords.slice();
28372850
const tokenApps = [];
28382851
const apps = [];
2839-
const children = [];
2840-
for (;;) {
2841-
const index = sceneMeshes.findIndex(sceneMesh => {
2842-
for (let i = 0; i < sceneMesh.token.coords.length; i++) {
2843-
const coord = sceneMesh.token.coords[i];
2844-
for (let j = 0; j < coords.length; j++) {
2845-
const coord2 = coords[j];
2846-
if (coord[0] === coord2[0] && coord[1] === coord2[1]) {
2847-
return true;
2848-
}
2852+
const selectedSceneMeshes = sceneMeshes.filter(sceneMesh => {
2853+
for (let i = 0; i < sceneMesh.token.coords.length; i++) {
2854+
const coord = sceneMesh.token.coords[i];
2855+
for (let j = 0; j < coords.length; j++) {
2856+
const coord2 = coords[j];
2857+
if (coord[0] === coord2[0] && coord[1] === coord2[1]) {
2858+
return true;
28492859
}
28502860
}
2851-
return false;
2852-
});
2853-
if (index !== -1) {
2854-
const sceneMesh = sceneMeshes[index];
2855-
for (let i = 0; i < sceneMesh.apps.length; i++) {
2856-
const iconMesh = sceneMesh.apps[i];
2857-
if (coords.some(coord => {
2858-
const minX = coord[0]*7 - 3 - 0.5;
2859-
const maxX = (coord[0]+1)*7 - 3 - 0.5;
2860-
const minY = coord[1]*7 - 3 - 0.5;
2861-
const maxY = (coord[1]+1)*7 - 3 - 0.5;
2862-
return iconMesh.moveMesh.position.x >= minX && iconMesh.moveMesh.position.x < maxX && iconMesh.moveMesh.position.z >= minY && iconMesh.moveMesh.position.z < maxY;
2863-
})) {
2864-
tokenApps.push(sceneMesh.token.apps[i]);
2865-
apps.push(iconMesh);
2866-
} else {
2867-
_removeApp(iconMesh);
2868-
_destroyApp(iconMesh);
2869-
}
2861+
}
2862+
return false;
2863+
});
2864+
for (let i = 0; i < selectedSceneMeshes.length; i++) {
2865+
const sceneMesh = selectedSceneMeshes[i];
2866+
for (let j = 0; j < sceneMesh.apps.length; j++) {
2867+
const iconMesh = sceneMesh.apps[j];
2868+
if (coords.some(coord => {
2869+
const minX = coord[0]*7 - 3 - 0.5;
2870+
const maxX = (coord[0]+1)*7 - 3 - 0.5;
2871+
const minY = coord[1]*7 - 3 - 0.5;
2872+
const maxY = (coord[1]+1)*7 - 3 - 0.5;
2873+
return iconMesh.moveMesh.position.x >= minX && iconMesh.moveMesh.position.x < maxX && iconMesh.moveMesh.position.z >= minY && iconMesh.moveMesh.position.z < maxY;
2874+
})) {
2875+
tokenApps.push(sceneMesh.token.apps[i]);
2876+
apps.push(iconMesh);
28702877
}
2871-
children.push.apply(children, sceneMesh.children);
2872-
sceneMesh.apps = [];
2873-
sceneMesh.children = [];
2874-
sceneMesh.destroy();
2875-
scene.remove(sceneMesh);
2876-
sceneMeshes.splice(index, 1);
2877-
} else {
2878-
break;
28792878
}
28802879
}
28812880

2882-
_setScene(coords.slice(), tokenApps)
2881+
_setScene(coords, tokenApps)
28832882
.then(sceneId => {
2883+
const children = [];
2884+
for (let i = 0; i < selectedSceneMeshes.length; i++) {
2885+
const oldSceneMesh = selectedSceneMeshes[i];
2886+
for (let j = 0; j < oldSceneMesh.apps.length; j++) {
2887+
if (apps.indexOf(oldSceneMesh.apps[j]) === -1) {
2888+
_removeApp(iconMesh);
2889+
_destroyApp(iconMesh);
2890+
}
2891+
}
2892+
children.push.apply(children, oldSceneMesh.children);
2893+
oldSceneMesh.apps = [];
2894+
oldSceneMesh.children = [];
2895+
oldSceneMesh.destroy();
2896+
scene.remove(oldSceneMesh);
2897+
sceneMeshes.splice(sceneMeshes.indexOf(oldSceneMesh), 1);
2898+
}
2899+
28842900
const sceneMesh = _makeSceneObjectMesh();
28852901
sceneMesh.token = {
28862902
id: sceneId,
2887-
coords: coords.slice(),
2903+
coords,
28882904
apps: tokenApps,
2905+
owner: _getUserAddress(),
28892906
};
28902907
sceneMesh.apps = apps;
28912908
for (let i = 0; i < children.length; i++) {

0 commit comments

Comments
 (0)