|
1 | 1 | import { getPortableExperienceFromUrn } from 'unity-interface/portableExperiencesUtils' |
2 | 2 | import { ISceneLoader } from '../types' |
| 3 | +import { encodeParcelPosition } from '../../../lib/decentraland' |
| 4 | +import { Vector2 } from '@dcl/ecs-math' |
| 5 | +import { getResourcesURL } from '../../location' |
| 6 | +import { EmptyParcelController } from '../genesis-city-loader-impl/emptyParcelController' |
| 7 | +import { LoadableScene } from '../../types' |
| 8 | + |
| 9 | +function getParcels(position: Vector2, loadingRadius: number): string[] { |
| 10 | + const parcels: string[] = [] |
| 11 | + for (let x = position.x - loadingRadius; x < position.x + loadingRadius; x++) { |
| 12 | + for (let y = position.y - loadingRadius; y < position.y + loadingRadius; y++) { |
| 13 | + const v = new Vector2(x, y) |
| 14 | + if (v.subtract(position).length() < loadingRadius) { |
| 15 | + parcels.push(encodeParcelPosition(v)) |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + return parcels |
| 20 | +} |
3 | 21 |
|
4 | 22 | export async function createWorldLoader(options: { urns: string[] }): Promise<ISceneLoader> { |
| 23 | + const mappingScene = new Map<string, LoadableScene>() |
5 | 24 | const scenes = await Promise.all(options.urns.map((urn) => getPortableExperienceFromUrn(urn))) |
| 25 | + |
| 26 | + for (const parcel of scenes[0].entity.metadata.scene.parcels) { |
| 27 | + mappingScene.set(parcel, scenes[0]) |
| 28 | + } |
| 29 | + |
| 30 | + const emptyParcelController = new EmptyParcelController({ rootUrl: getResourcesURL('.') }) |
| 31 | + |
6 | 32 | return { |
7 | 33 | async fetchScenesByLocation(_parcels) { |
8 | 34 | return { scenes } |
9 | 35 | }, |
10 | | - async reportPosition(_positionReport) { |
11 | | - return { scenes } |
| 36 | + async reportPosition(positionReport) { |
| 37 | + const newScenes: Set<LoadableScene> = new Set() |
| 38 | + for await (const parcel of getParcels( |
| 39 | + new Vector2(positionReport.position.x, positionReport.position.y), |
| 40 | + positionReport.loadingRadius |
| 41 | + )) { |
| 42 | + if (mappingScene.has(parcel)) { |
| 43 | + newScenes.add(mappingScene.get(parcel)!) |
| 44 | + } else { |
| 45 | + const scene = await emptyParcelController.createFakeEntity(parcel) |
| 46 | + |
| 47 | + mappingScene.set(parcel, scene) |
| 48 | + newScenes.add(scene) |
| 49 | + } |
| 50 | + } |
| 51 | + return { scenes: Array.from(newScenes) } |
12 | 52 | }, |
13 | 53 | async stop() {}, |
14 | 54 | invalidateCache(_) {} |
|
0 commit comments