|
1886 | 1886 | }, |
1887 | 1887 | }); |
1888 | 1888 | } else { |
1889 | | - return data.tokens[_getKey(x, z)] || { |
1890 | | - owner: ONE_ADDRESS, |
| 1889 | + const token = data.tokens[_getKey(x, z)] || { |
| 1890 | + owner: NULL_ADDRESS, |
1891 | 1891 | id: 0, |
1892 | 1892 | x, |
1893 | 1893 | y: z, |
1894 | 1894 | lastTimestamp: 0, |
1895 | | - sceneId: 0, |
1896 | 1895 | }; |
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; |
1909 | 1911 | } |
1910 | 1912 | }; |
1911 | 1913 | const _setScene = async (coords, apps) => { |
|
1958 | 1960 | id: sceneId, |
1959 | 1961 | coords, |
1960 | 1962 | apps, |
| 1963 | + owner: _getUserAddress(), |
1961 | 1964 | }; |
1962 | 1965 | data.scenes[sceneId] = scene; |
1963 | 1966 | _saveData(); |
1964 | 1967 | return sceneId; |
1965 | 1968 | } |
1966 | 1969 | }; |
| 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 | +}; |
1967 | 1984 | const _makeSceneObjectMesh = () => { |
1968 | 1985 | const object = new THREE.Object3D(); |
1969 | 1986 | object.token = null; |
|
2012 | 2029 | id: 0, |
2013 | 2030 | coords: [[x, z]], |
2014 | 2031 | apps: [], |
| 2032 | + owner: NULL_ADDRESS, |
2015 | 2033 | }; |
2016 | 2034 | _getToken(x, z) |
2017 | 2035 | .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); |
2020 | 2038 | if (_isValid()) { |
2021 | 2039 | const _setDefaultSubMesh = () => { |
2022 | 2040 | if (_isInMultiverse() && owner === NULL_ADDRESS) { |
|
2028 | 2046 | } |
2029 | 2047 | }; |
2030 | 2048 |
|
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 | + } |
2060 | 2074 |
|
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 | + } |
2074 | 2088 |
|
2075 | | - lastSceneCoords[0] = NaN; |
2076 | | - lastSceneCoords[1] = NaN; |
2077 | | - }); |
| 2089 | + lastSceneCoords[0] = NaN; |
| 2090 | + lastSceneCoords[1] = NaN; |
2078 | 2091 | } else { |
2079 | 2092 | _setDefaultSubMesh(); |
2080 | 2093 | } |
|
2833 | 2846 | }], |
2834 | 2847 | [saveImg, 4, 2, () => { |
2835 | 2848 | if (selectedSceneToken) { |
2836 | | - const {coords} = selectedSceneToken; |
| 2849 | + const coords = selectedSceneToken.coords.slice(); |
2837 | 2850 | const tokenApps = []; |
2838 | 2851 | 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; |
2849 | 2859 | } |
2850 | 2860 | } |
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); |
2870 | 2877 | } |
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; |
2879 | 2878 | } |
2880 | 2879 | } |
2881 | 2880 |
|
2882 | | - _setScene(coords.slice(), tokenApps) |
| 2881 | + _setScene(coords, tokenApps) |
2883 | 2882 | .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 | + |
2884 | 2900 | const sceneMesh = _makeSceneObjectMesh(); |
2885 | 2901 | sceneMesh.token = { |
2886 | 2902 | id: sceneId, |
2887 | | - coords: coords.slice(), |
| 2903 | + coords, |
2888 | 2904 | apps: tokenApps, |
| 2905 | + owner: _getUserAddress(), |
2889 | 2906 | }; |
2890 | 2907 | sceneMesh.apps = apps; |
2891 | 2908 | for (let i = 0; i < children.length; i++) { |
|
0 commit comments