| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using TMPro; |
| | 7 | | using KernelConfigurationTypes; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class MapRenderer : MonoBehaviour |
| | 12 | | { |
| | 13 | | const int LEFT_BORDER_PARCELS = 25; |
| | 14 | | const int RIGHT_BORDER_PARCELS = 31; |
| | 15 | | const int TOP_BORDER_PARCELS = 31; |
| | 16 | | const int BOTTOM_BORDER_PARCELS = 25; |
| | 17 | | const int WORLDMAP_WIDTH_IN_PARCELS = 300; |
| | 18 | | const string MINIMAP_USER_ICONS_POOL_NAME = "MinimapUserIconsPool"; |
| | 19 | | const int MINIMAP_USER_ICONS_MAX_PREWARM = 30; |
| | 20 | | private int NAVMAP_CHUNK_LAYER; |
| | 21 | |
|
| 0 | 22 | | public static MapRenderer i { get; private set; } |
| | 23 | |
|
| 216 | 24 | | [SerializeField] private float parcelHightlightScale = 1.25f; |
| 216 | 25 | | [SerializeField] private float parcelHoldTimeInSeconds = 1f; |
| | 26 | | [SerializeField] private Button ParcelHighlightButton; |
| | 27 | | private float parcelSizeInMap; |
| 872 | 28 | | private Vector3Variable playerWorldPosition => CommonScriptableObjects.playerWorldPosition; |
| 709 | 29 | | private Vector3Variable playerRotation => CommonScriptableObjects.cameraForward; |
| 216 | 30 | | private Vector3[] mapWorldspaceCorners = new Vector3[4]; |
| | 31 | | private Vector3 worldCoordsOriginInMap; |
| | 32 | | private Vector3 lastCursorMapCoords; |
| | 33 | | private float parcelHoldCountdown; |
| 216 | 34 | | private List<RaycastResult> uiRaycastResults = new List<RaycastResult>(); |
| 216 | 35 | | private PointerEventData uiRaycastPointerEventData = new PointerEventData(EventSystem.current); |
| | 36 | |
|
| | 37 | | [HideInInspector] public Vector3 cursorMapCoords; |
| 216 | 38 | | [HideInInspector] public bool showCursorCoords = true; |
| 138 | 39 | | public Vector3 playerGridPosition => Utils.WorldToGridPositionUnclamped(playerWorldPosition.Get()); |
| | 40 | | public MapAtlas atlas; |
| | 41 | | public RawImage parcelHighlightImage; |
| | 42 | | public TextMeshProUGUI highlightedParcelText; |
| | 43 | | public Transform overlayContainer; |
| | 44 | | public Transform globalUserMarkerContainer; |
| | 45 | |
|
| | 46 | | public Image playerPositionIcon; |
| | 47 | |
|
| | 48 | | // Used as a reference of the coordinates origin in-map and as a parcel width/height reference |
| | 49 | | public RectTransform centeredReferenceParcel; |
| | 50 | |
|
| | 51 | | public MapSceneIcon scenesOfInterestIconPrefab; |
| | 52 | | public MapSceneIcon userIconPrefab; |
| | 53 | | public UserMarkerObject globalUserMarkerPrefab; |
| | 54 | |
|
| 0 | 55 | | public MapGlobalUsersPositionMarkerController usersPositionMarkerController { private set; get; } |
| | 56 | |
|
| 216 | 57 | | private HashSet<MinimapMetadata.MinimapSceneInfo> scenesOfInterest = new HashSet<MinimapMetadata.MinimapSceneInf |
| 216 | 58 | | private Dictionary<MinimapMetadata.MinimapSceneInfo, GameObject> scenesOfInterestMarkers = new Dictionary<Minima |
| 216 | 59 | | private Dictionary<string, PoolableObject> usersInfoMarkers = new Dictionary<string, PoolableObject>(); |
| | 60 | |
|
| | 61 | | private Pool usersInfoPool; |
| | 62 | |
|
| | 63 | | private bool parcelHighlightEnabledValue = false; |
| | 64 | |
|
| 216 | 65 | | List<WorldRange> validWorldRanges = new List<WorldRange> |
| | 66 | | { |
| | 67 | | new WorldRange(-150, -150, 150, 150) // default range |
| | 68 | | }; |
| | 69 | |
|
| | 70 | | public bool parcelHighlightEnabled |
| | 71 | | { |
| | 72 | | set |
| | 73 | | { |
| 1 | 74 | | parcelHighlightEnabledValue = value; |
| 1 | 75 | | parcelHighlightImage.gameObject.SetActive(parcelHighlightEnabledValue); |
| 1 | 76 | | } |
| 0 | 77 | | get { return parcelHighlightEnabledValue; } |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public static System.Action<int, int> OnParcelClicked; |
| | 81 | | public static System.Action<int, int> OnParcelHold; |
| | 82 | | public static System.Action OnParcelHoldCancel; |
| | 83 | |
|
| | 84 | | private bool isInitialized = false; |
| | 85 | |
|
| | 86 | | [HideInInspector] |
| | 87 | | public event System.Action OnMovedParcelCursor; |
| | 88 | |
|
| | 89 | | private void Awake() |
| | 90 | | { |
| 108 | 91 | | i = this; |
| 108 | 92 | | Initialize(); |
| 108 | 93 | | } |
| | 94 | |
|
| | 95 | | public void Initialize() |
| | 96 | | { |
| 110 | 97 | | if (isInitialized) |
| 1 | 98 | | return; |
| | 99 | |
|
| 109 | 100 | | isInitialized = true; |
| 109 | 101 | | EnsurePools(); |
| 109 | 102 | | atlas.InitializeChunks(); |
| | 103 | |
|
| 109 | 104 | | NAVMAP_CHUNK_LAYER = LayerMask.NameToLayer("NavmapChunk"); |
| | 105 | |
|
| 109 | 106 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated += MapRenderer_OnSceneInfoUpdated; |
| 109 | 107 | | MinimapMetadata.GetMetadata().OnUserInfoUpdated += MapRenderer_OnUserInfoUpdated; |
| 109 | 108 | | MinimapMetadata.GetMetadata().OnUserInfoRemoved += MapRenderer_OnUserInfoRemoved; |
| | 109 | |
|
| 109 | 110 | | ParcelHighlightButton.onClick.AddListener(ClickMousePositionParcel); |
| | 111 | |
|
| 109 | 112 | | playerWorldPosition.OnChange += OnCharacterMove; |
| 109 | 113 | | playerRotation.OnChange += OnCharacterRotate; |
| | 114 | |
|
| 109 | 115 | | parcelHighlightImage.rectTransform.localScale = new Vector3(parcelHightlightScale, parcelHightlightScale, 1f |
| | 116 | |
|
| 109 | 117 | | parcelHoldCountdown = parcelHoldTimeInSeconds; |
| | 118 | |
|
| 109 | 119 | | usersPositionMarkerController = new MapGlobalUsersPositionMarkerController(globalUserMarkerPrefab, |
| | 120 | | globalUserMarkerContainer, |
| | 121 | | MapUtils.GetTileToLocalPosition); |
| | 122 | |
|
| 109 | 123 | | usersPositionMarkerController.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.BACKGROUND); |
| | 124 | |
|
| 109 | 125 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 109 | 126 | | } |
| | 127 | |
|
| | 128 | | private void EnsurePools() |
| | 129 | | { |
| 109 | 130 | | usersInfoPool = PoolManager.i.GetPool(MINIMAP_USER_ICONS_POOL_NAME); |
| | 131 | |
|
| 109 | 132 | | if (usersInfoPool == null) |
| | 133 | | { |
| 106 | 134 | | usersInfoPool = PoolManager.i.AddPool( |
| | 135 | | MINIMAP_USER_ICONS_POOL_NAME, |
| | 136 | | Instantiate(userIconPrefab.gameObject, overlayContainer.transform), |
| | 137 | | maxPrewarmCount: MINIMAP_USER_ICONS_MAX_PREWARM, |
| | 138 | | isPersistent: true); |
| | 139 | |
|
| 106 | 140 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 141 | | usersInfoPool.ForcePrewarm(); |
| | 142 | | } |
| 109 | 143 | | } |
| | 144 | |
|
| 214 | 145 | | public void OnDestroy() { Cleanup(); } |
| | 146 | |
|
| | 147 | | public void Cleanup() |
| | 148 | | { |
| 600 | 149 | | if (atlas != null) |
| 600 | 150 | | atlas.Cleanup(); |
| | 151 | |
|
| 1202 | 152 | | foreach (var kvp in scenesOfInterestMarkers) |
| | 153 | | { |
| 1 | 154 | | if (kvp.Value != null) |
| 1 | 155 | | Destroy(kvp.Value); |
| | 156 | | } |
| | 157 | |
|
| 600 | 158 | | scenesOfInterestMarkers.Clear(); |
| | 159 | |
|
| 600 | 160 | | playerWorldPosition.OnChange -= OnCharacterMove; |
| 600 | 161 | | playerRotation.OnChange -= OnCharacterRotate; |
| 600 | 162 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= MapRenderer_OnSceneInfoUpdated; |
| 600 | 163 | | MinimapMetadata.GetMetadata().OnUserInfoUpdated -= MapRenderer_OnUserInfoUpdated; |
| 600 | 164 | | MinimapMetadata.GetMetadata().OnUserInfoRemoved -= MapRenderer_OnUserInfoRemoved; |
| | 165 | |
|
| 600 | 166 | | ParcelHighlightButton.onClick.RemoveListener(ClickMousePositionParcel); |
| | 167 | |
|
| 600 | 168 | | usersPositionMarkerController?.Dispose(); |
| | 169 | |
|
| 600 | 170 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 171 | |
|
| 600 | 172 | | isInitialized = false; |
| 600 | 173 | | } |
| | 174 | |
|
| | 175 | | void Update() |
| | 176 | | { |
| 13115 | 177 | | if (!parcelHighlightEnabled) |
| 13115 | 178 | | return; |
| | 179 | |
|
| 0 | 180 | | parcelSizeInMap = centeredReferenceParcel.rect.width * centeredReferenceParcel.lossyScale.x; |
| | 181 | |
|
| | 182 | | // the reference parcel has a bottom-left pivot |
| 0 | 183 | | centeredReferenceParcel.GetWorldCorners(mapWorldspaceCorners); |
| 0 | 184 | | worldCoordsOriginInMap = mapWorldspaceCorners[0]; |
| | 185 | |
|
| 0 | 186 | | UpdateCursorMapCoords(); |
| | 187 | |
|
| 0 | 188 | | UpdateParcelHighlight(); |
| | 189 | |
|
| 0 | 190 | | UpdateParcelHold(); |
| | 191 | |
|
| 0 | 192 | | lastCursorMapCoords = cursorMapCoords; |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | void UpdateCursorMapCoords() |
| | 196 | | { |
| 0 | 197 | | if (!IsCursorOverMapChunk()) |
| 0 | 198 | | return; |
| | 199 | |
|
| 0 | 200 | | cursorMapCoords = Input.mousePosition - worldCoordsOriginInMap; |
| 0 | 201 | | cursorMapCoords = cursorMapCoords / parcelSizeInMap; |
| | 202 | |
|
| 0 | 203 | | cursorMapCoords.x = (int)Mathf.Floor(cursorMapCoords.x); |
| 0 | 204 | | cursorMapCoords.y = (int)Mathf.Floor(cursorMapCoords.y); |
| 0 | 205 | | } |
| | 206 | |
|
| | 207 | | bool IsCursorOverMapChunk() |
| | 208 | | { |
| 0 | 209 | | uiRaycastPointerEventData.position = Input.mousePosition; |
| 0 | 210 | | EventSystem.current.RaycastAll(uiRaycastPointerEventData, uiRaycastResults); |
| | 211 | |
|
| 0 | 212 | | return uiRaycastResults.Count > 0 && uiRaycastResults[0].gameObject.layer == NAVMAP_CHUNK_LAYER; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | void UpdateParcelHighlight() |
| | 216 | | { |
| 0 | 217 | | if (!CoordinatesAreInsideTheWorld((int)cursorMapCoords.x, (int)cursorMapCoords.y)) |
| | 218 | | { |
| 0 | 219 | | if (parcelHighlightImage.gameObject.activeSelf) |
| 0 | 220 | | parcelHighlightImage.gameObject.SetActive(false); |
| | 221 | |
|
| 0 | 222 | | return; |
| | 223 | | } |
| | 224 | |
|
| 0 | 225 | | if (!parcelHighlightImage.gameObject.activeSelf) |
| 0 | 226 | | parcelHighlightImage.gameObject.SetActive(true); |
| | 227 | |
|
| 0 | 228 | | string previousText = highlightedParcelText.text; |
| 0 | 229 | | parcelHighlightImage.transform.position = worldCoordsOriginInMap + cursorMapCoords * parcelSizeInMap + new V |
| 0 | 230 | | highlightedParcelText.text = showCursorCoords ? $"{cursorMapCoords.x}, {cursorMapCoords.y}" : string.Empty; |
| | 231 | |
|
| 0 | 232 | | if (highlightedParcelText.text != previousText && !Input.GetMouseButton(0)) |
| | 233 | | { |
| 0 | 234 | | OnMovedParcelCursor?.Invoke(); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | // ---------------------------------------------------- |
| | 238 | | // TODO: Use sceneInfo to highlight whole scene parcels and populate scenes hover info on navmap once we can |
| | 239 | | // var sceneInfo = mapMetadata.GetSceneInfo(cursorMapCoords.x, cursorMapCoords.y); |
| 0 | 240 | | } |
| | 241 | |
|
| | 242 | | void UpdateParcelHold() |
| | 243 | | { |
| 0 | 244 | | if (cursorMapCoords == lastCursorMapCoords) |
| | 245 | | { |
| 0 | 246 | | if (parcelHoldCountdown <= 0f) |
| 0 | 247 | | return; |
| | 248 | |
|
| 0 | 249 | | parcelHoldCountdown -= Time.deltaTime; |
| | 250 | |
|
| 0 | 251 | | if (parcelHoldCountdown <= 0) |
| | 252 | | { |
| 0 | 253 | | parcelHoldCountdown = 0f; |
| 0 | 254 | | highlightedParcelText.text = string.Empty; |
| 0 | 255 | | OnParcelHold?.Invoke((int)cursorMapCoords.x, (int)cursorMapCoords.y); |
| | 256 | | } |
| 0 | 257 | | } |
| | 258 | | else |
| | 259 | | { |
| 0 | 260 | | parcelHoldCountdown = parcelHoldTimeInSeconds; |
| 0 | 261 | | OnParcelHoldCancel?.Invoke(); |
| | 262 | | } |
| 0 | 263 | | } |
| | 264 | |
|
| 10 | 265 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { validWorldRanges = c |
| | 266 | |
|
| | 267 | | bool CoordinatesAreInsideTheWorld(int xCoord, int yCoord) |
| | 268 | | { |
| 0 | 269 | | foreach (WorldRange worldRange in validWorldRanges) |
| | 270 | | { |
| 0 | 271 | | if (worldRange.Contains(xCoord, yCoord)) |
| | 272 | | { |
| 0 | 273 | | return true; |
| | 274 | | } |
| | 275 | | } |
| 0 | 276 | | return false; |
| 0 | 277 | | } |
| | 278 | |
|
| | 279 | | private void MapRenderer_OnSceneInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 280 | | { |
| 5 | 281 | | if (!sceneInfo.isPOI) |
| 4 | 282 | | return; |
| | 283 | |
|
| 1 | 284 | | if (scenesOfInterest.Contains(sceneInfo)) |
| 0 | 285 | | return; |
| | 286 | |
|
| 1 | 287 | | scenesOfInterest.Add(sceneInfo); |
| | 288 | |
|
| 1 | 289 | | GameObject go = Object.Instantiate(scenesOfInterestIconPrefab.gameObject, overlayContainer.transform); |
| | 290 | |
|
| 1 | 291 | | Vector2 centerTile = Vector2.zero; |
| | 292 | |
|
| 10 | 293 | | foreach (var parcel in sceneInfo.parcels) |
| | 294 | | { |
| 4 | 295 | | centerTile += parcel; |
| | 296 | | } |
| | 297 | |
|
| 1 | 298 | | centerTile /= (float)sceneInfo.parcels.Count; |
| | 299 | |
|
| 1 | 300 | | (go.transform as RectTransform).anchoredPosition = MapUtils.GetTileToLocalPosition(centerTile.x, centerTile. |
| | 301 | |
|
| 1 | 302 | | MapSceneIcon icon = go.GetComponent<MapSceneIcon>(); |
| | 303 | |
|
| 1 | 304 | | if (icon.title != null) |
| 1 | 305 | | icon.title.text = sceneInfo.name; |
| | 306 | |
|
| 1 | 307 | | scenesOfInterestMarkers.Add(sceneInfo, go); |
| 1 | 308 | | } |
| | 309 | |
|
| | 310 | | private void MapRenderer_OnUserInfoUpdated(MinimapMetadata.MinimapUserInfo userInfo) |
| | 311 | | { |
| 5 | 312 | | if (!usersInfoMarkers.TryGetValue(userInfo.userId, out PoolableObject marker)) |
| | 313 | | { |
| 4 | 314 | | marker = usersInfoPool.Get(); |
| 4 | 315 | | marker.gameObject.name = $"UserIcon-{userInfo.userName}"; |
| 4 | 316 | | marker.gameObject.transform.SetParent(overlayContainer.transform, true); |
| 4 | 317 | | marker.gameObject.transform.localScale = Vector3.one; |
| 4 | 318 | | usersInfoMarkers.Add(userInfo.userId, marker); |
| | 319 | | } |
| | 320 | |
|
| 5 | 321 | | ConfigureUserIcon(marker.gameObject, userInfo.worldPosition); |
| 5 | 322 | | } |
| | 323 | |
|
| | 324 | | private void MapRenderer_OnUserInfoRemoved(string userId) |
| | 325 | | { |
| 3 | 326 | | if (!usersInfoMarkers.TryGetValue(userId, out PoolableObject go)) |
| | 327 | | { |
| 0 | 328 | | return; |
| | 329 | | } |
| | 330 | |
|
| 3 | 331 | | usersInfoPool.Release(go); |
| 3 | 332 | | usersInfoMarkers.Remove(userId); |
| 3 | 333 | | } |
| | 334 | |
|
| | 335 | | private void ConfigureUserIcon(GameObject iconGO, Vector3 pos) |
| | 336 | | { |
| 5 | 337 | | var gridPosition = Utils.WorldToGridPositionUnclamped(pos); |
| 5 | 338 | | iconGO.transform.localPosition = MapUtils.GetTileToLocalPosition(gridPosition.x, gridPosition.y); |
| 5 | 339 | | } |
| | 340 | |
|
| | 341 | | private void OnCharacterMove(Vector3 current, Vector3 previous) |
| | 342 | | { |
| 886 | 343 | | current.y = 0; |
| 886 | 344 | | previous.y = 0; |
| | 345 | |
|
| 886 | 346 | | if (Vector3.Distance(current, previous) < 0.1f) |
| 773 | 347 | | return; |
| | 348 | |
|
| 113 | 349 | | UpdateRendering(Utils.WorldToGridPositionUnclamped(current)); |
| 113 | 350 | | } |
| | 351 | |
|
| 50 | 352 | | private void OnCharacterRotate(Vector3 current, Vector3 previous) { UpdateRendering(Utils.WorldToGridPositionUnc |
| | 353 | |
|
| | 354 | | public void OnCharacterSetPosition(Vector2Int newCoords, Vector2Int oldCoords) |
| | 355 | | { |
| 0 | 356 | | if (oldCoords == newCoords) |
| 0 | 357 | | return; |
| | 358 | |
|
| 0 | 359 | | UpdateRendering(new Vector2((float)newCoords.x, (float)newCoords.y)); |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | public void UpdateRendering(Vector2 newCoords) |
| | 363 | | { |
| 138 | 364 | | UpdateBackgroundLayer(newCoords); |
| 138 | 365 | | UpdateSelectionLayer(); |
| 138 | 366 | | UpdateOverlayLayer(); |
| 138 | 367 | | } |
| | 368 | |
|
| 276 | 369 | | void UpdateBackgroundLayer(Vector2 newCoords) { atlas.CenterToTile(newCoords); } |
| | 370 | |
|
| | 371 | | void UpdateSelectionLayer() |
| | 372 | | { |
| | 373 | | //TODO(Brian): Build and place here the scene highlight if applicable. |
| 0 | 374 | | } |
| | 375 | |
|
| | 376 | | void UpdateOverlayLayer() |
| | 377 | | { |
| | 378 | | //NOTE(Brian): Player icon |
| 138 | 379 | | Vector3 f = CommonScriptableObjects.cameraForward.Get(); |
| 138 | 380 | | Quaternion playerAngle = Quaternion.Euler(0, 0, Mathf.Atan2(-f.x, f.z) * Mathf.Rad2Deg); |
| | 381 | |
|
| 138 | 382 | | var gridPosition = this.playerGridPosition; |
| 138 | 383 | | playerPositionIcon.transform.localPosition = MapUtils.GetTileToLocalPosition(gridPosition.x, gridPosition.y) |
| 138 | 384 | | playerPositionIcon.transform.rotation = playerAngle; |
| 138 | 385 | | } |
| | 386 | |
|
| 0 | 387 | | public Vector3 GetViewportCenter() { return atlas.viewport.TransformPoint(atlas.viewport.rect.center); } |
| | 388 | |
|
| | 389 | | // Called by the parcelhighlight image button |
| | 390 | | public void ClickMousePositionParcel() |
| | 391 | | { |
| 0 | 392 | | highlightedParcelText.text = string.Empty; |
| 0 | 393 | | OnParcelClicked?.Invoke((int)cursorMapCoords.x, (int)cursorMapCoords.y); |
| 0 | 394 | | } |
| | 395 | | } |
| | 396 | | } |