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