| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class MapGlobalUsersPositionMarkerController : IDisposable |
| | 8 | | { |
| | 9 | | private const float UPDATE_INTERVAL_INITIAL = 10f; |
| | 10 | | private const float UPDATE_INTERVAL_FOREGROUND = 60f; |
| | 11 | | private const float UPDATE_INTERVAL_BACKGROUND = 5 * 60f; |
| | 12 | |
|
| | 13 | | private const int MAX_MARKERS = 100; |
| | 14 | |
|
| | 15 | | private const int COMMS_RADIUS_THRESHOLD = 2; |
| | 16 | |
|
| | 17 | | public enum UpdateMode |
| | 18 | | { |
| | 19 | | FOREGROUND, |
| | 20 | | BACKGROUND |
| | 21 | | } |
| | 22 | |
|
| | 23 | | FetchScenesHandler fetchScenesHandler; |
| | 24 | | MarkersHandler markersHandler; |
| | 25 | | UserPositionHandler userPositionHandler; |
| | 26 | | Transform markersContainer; |
| | 27 | |
|
| 10 | 28 | | int commsRadius = 4; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Constructor |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="markerPrefab">prefab for markers</param> |
| | 34 | | /// <param name="overlayContainer">parent for markers</param> |
| | 35 | | /// <param name="coordToMapPosFunc">function to transform coords to map position</param> |
| 10 | 36 | | public MapGlobalUsersPositionMarkerController(UserMarkerObject markerPrefab, Transform overlayContainer, Func<fl |
| | 37 | | { |
| 10 | 38 | | fetchScenesHandler = new FetchScenesHandler(UPDATE_INTERVAL_INITIAL, UPDATE_INTERVAL_FOREGROUND, UPDATE_INTE |
| 10 | 39 | | markersHandler = new MarkersHandler(markerPrefab, overlayContainer, MAX_MARKERS, coordToMapPosFunc); |
| 10 | 40 | | userPositionHandler = new UserPositionHandler(); |
| 10 | 41 | | markersContainer = overlayContainer; |
| | 42 | |
|
| 10 | 43 | | fetchScenesHandler.OnScenesFetched += OnScenesFetched; |
| 10 | 44 | | userPositionHandler.OnPlayerCoordsChanged += OnPlayerCoordsChanged; |
| 10 | 45 | | CommonScriptableObjects.rendererState.OnChange += OnRenderStateChanged; |
| | 46 | |
|
| 10 | 47 | | KernelConfig.i.EnsureConfigInitialized() |
| | 48 | | .Then(config => |
| | 49 | | { |
| 9 | 50 | | commsRadius = (int) config.comms.commRadius + COMMS_RADIUS_THRESHOLD; |
| 9 | 51 | | OnPlayerCoordsChanged(userPositionHandler.playerCoords); |
| 9 | 52 | | }); |
| 10 | 53 | | OnRenderStateChanged(CommonScriptableObjects.rendererState.Get(), false); |
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Set update mode. Scene's fetch intervals will smaller when updating in FOREGROUND than when updating in BACK |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="updateMode">update mode</param> |
| | 60 | | public void SetUpdateMode(UpdateMode updateMode) |
| | 61 | | { |
| 10 | 62 | | fetchScenesHandler.SetUpdateMode(updateMode); |
| 10 | 63 | | markersContainer.gameObject.SetActive(updateMode == UpdateMode.FOREGROUND); |
| 10 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Dispose() |
| | 67 | | { |
| 12 | 68 | | fetchScenesHandler.OnScenesFetched -= OnScenesFetched; |
| 12 | 69 | | userPositionHandler.OnPlayerCoordsChanged -= OnPlayerCoordsChanged; |
| 12 | 70 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderStateChanged; |
| | 71 | |
|
| 12 | 72 | | fetchScenesHandler.Dispose(); |
| 12 | 73 | | markersHandler.Dispose(); |
| 12 | 74 | | userPositionHandler.Dispose(); |
| 12 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | private void OnScenesFetched(List<HotScenesController.HotSceneInfo> sceneList) { markersHandler.SetMarkers(scene |
| | 78 | |
|
| 18 | 79 | | private void OnPlayerCoordsChanged(Vector2Int coords) { markersHandler.SetExclusionArea(coords, commsRadius); } |
| | 80 | |
|
| | 81 | | private void OnRenderStateChanged(bool current, bool prev) |
| | 82 | | { |
| 10 | 83 | | if (!current) |
| 0 | 84 | | return; |
| | 85 | |
|
| | 86 | | // NOTE: we start fetching scenes after the renderer is activated for the first time |
| 10 | 87 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderStateChanged; |
| 10 | 88 | | fetchScenesHandler.Init(); |
| 10 | 89 | | } |
| | 90 | | } |
| | 91 | | } |