| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using KernelConfigurationTypes; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class MapRenderer : MonoBehaviour |
| | 13 | | { |
| | 14 | | const int LEFT_BORDER_PARCELS = 25; |
| | 15 | | const int RIGHT_BORDER_PARCELS = 31; |
| | 16 | | const int TOP_BORDER_PARCELS = 31; |
| | 17 | | const int BOTTOM_BORDER_PARCELS = 25; |
| | 18 | | const int WORLDMAP_WIDTH_IN_PARCELS = 300; |
| | 19 | | const string MINIMAP_USER_ICONS_POOL_NAME = "MinimapUserIconsPool"; |
| | 20 | | const int MINIMAP_USER_ICONS_MAX_PREWARM = 30; |
| | 21 | | private const int MAX_CURSOR_PARCEL_DISTANCE = 40; |
| | 22 | | private const int MAX_SCENE_CHARACTER_TITLE = 29; |
| | 23 | | private const string EMPTY_PARCEL_NAME = "Empty parcel"; |
| | 24 | |
|
| | 25 | | public static System.Action<int, int> OnParcelClicked; |
| | 26 | | public static System.Action OnCursorFarFromParcel; |
| | 27 | |
|
| 0 | 28 | | [SerializeField] private float parcelHightlightScale = 1.25f; |
| | 29 | | [SerializeField] private Button ParcelHighlightButton; |
| | 30 | | [SerializeField] private MapParcelHighlight highlight; |
| | 31 | | [SerializeField] private Image parcelHighlightImage; |
| | 32 | | [SerializeField] private Image parcelHighlighImagePrefab; |
| | 33 | | [SerializeField] private Image parcelHighlighWithContentImagePrefab; |
| | 34 | | [SerializeField] private Image selectParcelHighlighImagePrefab; |
| | 35 | |
|
| | 36 | | [HideInInspector] public Vector2Int cursorMapCoords; |
| 0 | 37 | | [HideInInspector] public bool showCursorCoords = true; |
| | 38 | | public MapAtlas atlas; |
| | 39 | | public TextMeshProUGUI highlightedParcelText; |
| | 40 | | public Transform overlayContainer; |
| | 41 | | public Transform overlayContainerPlayers; |
| | 42 | | public Transform globalUserMarkerContainer; |
| | 43 | | public RectTransform playerPositionIcon; |
| | 44 | |
|
| 0 | 45 | | public float scaleFactor = 1f; |
| | 46 | |
|
| | 47 | | // Used as a reference of the coordinates origin in-map and as a parcel width/height reference |
| | 48 | | public RectTransform centeredReferenceParcel; |
| | 49 | |
|
| | 50 | | public MapSceneIcon scenesOfInterestIconPrefab; |
| | 51 | | public GameObject userIconPrefab; |
| | 52 | | public GameObject homePointIconPrefab; |
| | 53 | | public UserMarkerObject globalUserMarkerPrefab; |
| 0 | 54 | | private Dictionary<Vector2Int, Image> highlightedLands = new Dictionary<Vector2Int, Image>(); |
| 0 | 55 | | private BaseVariable<Vector2Int> homePointCoordinates = DataStore.i.HUDs.homePoint; |
| | 56 | | private RectTransform homePointIcon; |
| | 57 | |
|
| | 58 | | private bool isInitialized = false; |
| | 59 | |
|
| | 60 | | private Vector2Int lastClickedCursorMapCoords; |
| 0 | 61 | | private Vector3 lastPlayerPosition = new Vector3(float.NegativeInfinity, 0, float.NegativeInfinity); |
| | 62 | | private Vector2Int lastSelectedLand; |
| | 63 | | private int NAVMAP_CHUNK_LAYER; |
| 0 | 64 | | private bool otherPlayersIconsEnabled = true; |
| 0 | 65 | | private List<Vector2Int> ownedEmptyLands = new List<Vector2Int>(); |
| 0 | 66 | | private List<Vector2Int> ownedLandsWithContent = new List<Vector2Int>(); |
| | 67 | |
|
| | 68 | | private bool parcelHighlightEnabledValue = false; |
| 0 | 69 | | private BaseVariable<Vector3> playerWorldPosition = DataStore.i.player.playerWorldPosition; |
| | 70 | |
|
| 0 | 71 | | private HashSet<MinimapMetadata.MinimapSceneInfo> scenesOfInterest = new HashSet<MinimapMetadata.MinimapSceneInf |
| 0 | 72 | | private Dictionary<MinimapMetadata.MinimapSceneInfo, GameObject> scenesOfInterestMarkers = new Dictionary<Minima |
| 0 | 73 | | private PointerEventData uiRaycastPointerEventData = new PointerEventData(EventSystem.current); |
| 0 | 74 | | private List<RaycastResult> uiRaycastResults = new List<RaycastResult>(); |
| 0 | 75 | | private Dictionary<string, PoolableObject> usersInfoMarkers = new Dictionary<string, PoolableObject>(); |
| | 76 | | private Pool usersInfoPool; |
| | 77 | |
|
| 0 | 78 | | List<WorldRange> validWorldRanges = new List<WorldRange> |
| | 79 | | { |
| | 80 | | new WorldRange(-150, -150, 150, 150) // default range |
| | 81 | | }; |
| | 82 | |
|
| | 83 | | [Obsolete] |
| 363 | 84 | | public static MapRenderer i { get; private set; } |
| | 85 | |
|
| 0 | 86 | | private Vector3Variable playerRotation => CommonScriptableObjects.cameraForward; |
| 0 | 87 | | public Vector3 playerGridPosition => Utils.WorldToGridPositionUnclamped(playerWorldPosition.Get()); |
| | 88 | |
|
| 0 | 89 | | public MapGlobalUsersPositionMarkerController usersPositionMarkerController { private set; get; } |
| | 90 | |
|
| | 91 | | public bool parcelHighlightEnabled |
| | 92 | | { |
| | 93 | | set |
| | 94 | | { |
| 0 | 95 | | parcelHighlightEnabledValue = value; |
| 0 | 96 | | parcelHighlightImage.gameObject.SetActive(parcelHighlightEnabledValue); |
| 0 | 97 | | MapVisibilityChanged?.Invoke(value); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | get |
| | 101 | | { |
| 0 | 102 | | return parcelHighlightEnabledValue; |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 107 | |
|
| | 108 | | private void Awake() |
| | 109 | | { |
| 0 | 110 | | i = this; |
| 0 | 111 | | Initialize(); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | void Update() |
| | 115 | | { |
| 0 | 116 | | if ((playerWorldPosition.Get() - lastPlayerPosition).sqrMagnitude >= 0.1f * 0.1f) |
| | 117 | | { |
| 0 | 118 | | lastPlayerPosition = playerWorldPosition.Get(); |
| 0 | 119 | | UpdateRendering(Utils.WorldToGridPositionUnclamped(lastPlayerPosition)); |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | if (!parcelHighlightEnabled) |
| 0 | 123 | | return; |
| | 124 | |
|
| 0 | 125 | | UpdateCursorMapCoords(); |
| | 126 | |
|
| 0 | 127 | | UpdateParcelHighlight(); |
| | 128 | |
|
| 0 | 129 | | UpdateParcelHold(); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public void OnDestroy() |
| | 133 | | { |
| 0 | 134 | | Cleanup(); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | [HideInInspector] |
| | 138 | | public event System.Action<float, float> OnMovedParcelCursor; |
| | 139 | | public event Action<bool> MapVisibilityChanged; |
| | 140 | |
|
| | 141 | | public void Initialize() |
| | 142 | | { |
| 0 | 143 | | if (isInitialized) |
| 0 | 144 | | return; |
| | 145 | |
|
| 0 | 146 | | isInitialized = true; |
| | 147 | |
|
| 0 | 148 | | InitializeHomePointIcon(); |
| 0 | 149 | | EnsurePools(); |
| 0 | 150 | | atlas.InitializeChunks(); |
| 0 | 151 | | NAVMAP_CHUNK_LAYER = LayerMask.NameToLayer("NavmapChunk"); |
| | 152 | |
|
| 0 | 153 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated += MapRenderer_OnSceneInfoUpdated; |
| 0 | 154 | | otherPlayers.OnAdded += OnOtherPlayersAdded; |
| 0 | 155 | | otherPlayers.OnRemoved += OnOtherPlayerRemoved; |
| 0 | 156 | | homePointCoordinates.OnChange += MoveHomePointIcon; |
| 0 | 157 | | MoveHomePointIcon(homePointCoordinates.Get(), new Vector2Int()); |
| | 158 | |
|
| 0 | 159 | | ParcelHighlightButton.onClick.AddListener(ClickMousePositionParcel); |
| | 160 | |
|
| 0 | 161 | | playerRotation.OnChange += OnCharacterRotate; |
| | 162 | |
|
| 0 | 163 | | highlight.SetScale(parcelHightlightScale); |
| | 164 | |
|
| 0 | 165 | | usersPositionMarkerController = new MapGlobalUsersPositionMarkerController(globalUserMarkerPrefab, |
| | 166 | | globalUserMarkerContainer, |
| | 167 | | MapUtils.CoordsToPosition); |
| | 168 | |
|
| 0 | 169 | | usersPositionMarkerController.SetUpdateMode(MapGlobalUsersPositionMarkerController.UpdateMode.BACKGROUND); |
| | 170 | |
|
| 0 | 171 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | private void MoveHomePointIcon(Vector2Int current, Vector2Int previous) |
| | 175 | | { |
| 0 | 176 | | homePointIcon.anchoredPosition = MapUtils.CoordsToPosition(new Vector3(current.x, current.y, 0)); |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | private void InitializeHomePointIcon() |
| | 180 | | { |
| 0 | 181 | | homePointIcon = GameObject.Instantiate(homePointIconPrefab).GetComponent<RectTransform>(); |
| 0 | 182 | | homePointIcon.gameObject.transform.SetParent(overlayContainer.transform, false); |
| 0 | 183 | | homePointIcon.anchoredPosition = new Vector2(0, 0); |
| 0 | 184 | | homePointIcon.transform.localPosition = new Vector3(homePointIcon.transform.localPosition.x, homePointIcon.t |
| 0 | 185 | | homePointIcon.localScale = new Vector3(2, 2, 2); |
| 0 | 186 | | homePointIcon.transform.SetAsFirstSibling(); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | private void EnsurePools() |
| | 190 | | { |
| 0 | 191 | | usersInfoPool = PoolManager.i.GetPool(MINIMAP_USER_ICONS_POOL_NAME); |
| | 192 | |
|
| 0 | 193 | | if (usersInfoPool == null) |
| | 194 | | { |
| 0 | 195 | | usersInfoPool = PoolManager.i.AddPool( |
| | 196 | | MINIMAP_USER_ICONS_POOL_NAME, |
| | 197 | | Instantiate(userIconPrefab.gameObject, overlayContainerPlayers.transform), |
| | 198 | | maxPrewarmCount: MINIMAP_USER_ICONS_MAX_PREWARM, |
| | 199 | | isPersistent: true); |
| | 200 | |
|
| 0 | 201 | | if (!Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 0 | 202 | | usersInfoPool.ForcePrewarm(); |
| | 203 | | } |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | public void SetParcelHighlightActive(bool isAtive) => |
| 0 | 207 | | parcelHighlightImage.enabled = isAtive; |
| | 208 | |
|
| | 209 | | public Vector3 GetParcelHighlightTransform() => |
| 0 | 210 | | parcelHighlightImage.transform.position; |
| | 211 | |
|
| | 212 | | public void SetOtherPlayersIconActive(bool isActive) |
| | 213 | | { |
| 0 | 214 | | otherPlayersIconsEnabled = isActive; |
| | 215 | |
|
| 0 | 216 | | foreach (PoolableObject poolableObject in usersInfoMarkers.Values) { poolableObject.gameObject.SetActive(isA |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public void SetPlayerIconActive(bool isActive) => |
| 0 | 220 | | playerPositionIcon.gameObject.SetActive(isActive); |
| | 221 | |
|
| | 222 | | public void SetHighlighSize(Vector2Int size) |
| | 223 | | { |
| 0 | 224 | | highlight.ChangeHighlighSize(size); |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | public void SetHighlightStyle(MapParcelHighlight.HighlighStyle style) |
| | 228 | | { |
| 0 | 229 | | highlight.SetStyle(style); |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | public void Cleanup() |
| | 233 | | { |
| 0 | 234 | | if (atlas != null) |
| 0 | 235 | | atlas.Cleanup(); |
| | 236 | |
|
| 0 | 237 | | foreach (var kvp in scenesOfInterestMarkers) |
| | 238 | | { |
| 0 | 239 | | if (kvp.Value != null) |
| 0 | 240 | | Destroy(kvp.Value); |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | CleanLandsHighlights(); |
| 0 | 244 | | ClearLandHighlightsInfo(); |
| | 245 | |
|
| 0 | 246 | | scenesOfInterestMarkers.Clear(); |
| | 247 | |
|
| 0 | 248 | | playerRotation.OnChange -= OnCharacterRotate; |
| 0 | 249 | | MinimapMetadata.GetMetadata().OnSceneInfoUpdated -= MapRenderer_OnSceneInfoUpdated; |
| 0 | 250 | | otherPlayers.OnAdded -= OnOtherPlayersAdded; |
| 0 | 251 | | otherPlayers.OnRemoved -= OnOtherPlayerRemoved; |
| 0 | 252 | | homePointCoordinates.OnChange -= MoveHomePointIcon; |
| | 253 | |
|
| 0 | 254 | | ParcelHighlightButton.onClick.RemoveListener(ClickMousePositionParcel); |
| | 255 | |
|
| 0 | 256 | | usersPositionMarkerController?.Dispose(); |
| | 257 | |
|
| 0 | 258 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 259 | |
|
| 0 | 260 | | isInitialized = false; |
| 0 | 261 | | } |
| | 262 | |
|
| | 263 | | public void CleanLandsHighlights() |
| | 264 | | { |
| 0 | 265 | | foreach (KeyValuePair<Vector2Int, Image> kvp in highlightedLands) { Destroy(kvp.Value.gameObject); } |
| | 266 | |
|
| 0 | 267 | | highlightedLands.Clear(); //To Clear out the dictionary |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | public void ClearLandHighlightsInfo() |
| | 271 | | { |
| 0 | 272 | | ownedLandsWithContent.Clear(); //To Clear out the content lands |
| 0 | 273 | | ownedEmptyLands.Clear(); //To Clear out the empty content |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | public void SelectLand(Vector2Int coordsToSelect, Vector2Int size) |
| | 277 | | { |
| 0 | 278 | | if (highlightedLands.ContainsKey(lastSelectedLand)) |
| | 279 | | { |
| 0 | 280 | | Destroy(highlightedLands[lastSelectedLand].gameObject); |
| 0 | 281 | | highlightedLands.Remove(lastSelectedLand); |
| | 282 | | } |
| | 283 | |
|
| 0 | 284 | | HighlightLands(ownedEmptyLands, ownedLandsWithContent); |
| | 285 | |
|
| 0 | 286 | | if (highlightedLands.ContainsKey(coordsToSelect)) |
| | 287 | | { |
| 0 | 288 | | Destroy(highlightedLands[coordsToSelect].gameObject); |
| 0 | 289 | | highlightedLands.Remove(coordsToSelect); |
| | 290 | | } |
| | 291 | |
|
| 0 | 292 | | CreateHighlightParcel(selectParcelHighlighImagePrefab, coordsToSelect, size); |
| 0 | 293 | | lastSelectedLand = coordsToSelect; |
| 0 | 294 | | } |
| | 295 | |
|
| | 296 | | public void HighlightLands(List<Vector2Int> landsToHighlight, List<Vector2Int> landsToHighlightWithContent) |
| | 297 | | { |
| 0 | 298 | | CleanLandsHighlights(); |
| | 299 | |
|
| 0 | 300 | | foreach (Vector2Int coords in landsToHighlight) |
| | 301 | | { |
| 0 | 302 | | if (highlightedLands.ContainsKey(coords)) |
| | 303 | | continue; |
| | 304 | |
|
| 0 | 305 | | CreateHighlightParcel(parcelHighlighImagePrefab, coords, Vector2Int.one); |
| | 306 | | } |
| | 307 | |
|
| 0 | 308 | | foreach (Vector2Int coords in landsToHighlightWithContent) |
| | 309 | | { |
| 0 | 310 | | if (highlightedLands.ContainsKey(coords)) |
| | 311 | | continue; |
| | 312 | |
|
| 0 | 313 | | if (!ownedLandsWithContent.Contains(coords)) |
| 0 | 314 | | ownedLandsWithContent.Add(coords); |
| | 315 | |
|
| 0 | 316 | | CreateHighlightParcel(parcelHighlighWithContentImagePrefab, coords, Vector2Int.one); |
| | 317 | | } |
| | 318 | |
|
| 0 | 319 | | ownedEmptyLands = landsToHighlight; |
| 0 | 320 | | ownedLandsWithContent = landsToHighlightWithContent; |
| 0 | 321 | | } |
| | 322 | |
|
| | 323 | | private void CreateHighlightParcel(Image prefab, Vector2Int coords, Vector2Int size) |
| | 324 | | { |
| 0 | 325 | | var highlightItem = Instantiate(prefab, overlayContainer, true).GetComponent<Image>(); |
| 0 | 326 | | highlightItem.rectTransform.localScale = new Vector3(parcelHightlightScale * size.x, parcelHightlightScale * |
| 0 | 327 | | highlightItem.rectTransform.SetAsLastSibling(); |
| 0 | 328 | | highlightItem.rectTransform.anchoredPosition = MapUtils.CoordsToPosition(coords); |
| 0 | 329 | | highlightedLands.Add(coords, highlightItem); |
| 0 | 330 | | } |
| | 331 | |
|
| | 332 | | void UpdateCursorMapCoords() |
| | 333 | | { |
| 0 | 334 | | if (!IsCursorOverMapChunk()) |
| 0 | 335 | | return; |
| | 336 | |
|
| | 337 | | const int OFFSET = -60; //Map is a bit off centered, we need to adjust it a little. |
| 0 | 338 | | RectTransformUtility.ScreenPointToLocalPointInRectangle(atlas.chunksParent, Input.mousePosition, DataStore.i |
| 0 | 339 | | mapPoint -= Vector2.one * OFFSET; |
| 0 | 340 | | mapPoint -= (atlas.chunksParent.sizeDelta / 2f); |
| 0 | 341 | | cursorMapCoords = Vector2Int.RoundToInt(mapPoint / MapUtils.PARCEL_SIZE); |
| 0 | 342 | | } |
| | 343 | |
|
| | 344 | | bool IsCursorOverMapChunk() |
| | 345 | | { |
| 0 | 346 | | uiRaycastPointerEventData.position = Input.mousePosition; |
| 0 | 347 | | EventSystem.current.RaycastAll(uiRaycastPointerEventData, uiRaycastResults); |
| | 348 | |
|
| 0 | 349 | | return uiRaycastResults.Count > 0 && uiRaycastResults[0].gameObject.layer == NAVMAP_CHUNK_LAYER; |
| | 350 | | } |
| | 351 | |
|
| | 352 | | void UpdateParcelHighlight() |
| | 353 | | { |
| 0 | 354 | | if (!CoordinatesAreInsideTheWorld((int)cursorMapCoords.x, (int)cursorMapCoords.y)) |
| | 355 | | { |
| 0 | 356 | | if (parcelHighlightImage.gameObject.activeSelf) |
| 0 | 357 | | parcelHighlightImage.gameObject.SetActive(false); |
| | 358 | |
|
| 0 | 359 | | return; |
| | 360 | | } |
| | 361 | |
|
| 0 | 362 | | if (!parcelHighlightImage.gameObject.activeSelf) |
| 0 | 363 | | parcelHighlightImage.gameObject.SetActive(true); |
| | 364 | |
|
| 0 | 365 | | string previousText = highlightedParcelText.text; |
| 0 | 366 | | parcelHighlightImage.rectTransform.SetAsLastSibling(); |
| 0 | 367 | | parcelHighlightImage.rectTransform.anchoredPosition = MapUtils.CoordsToPosition(cursorMapCoords); |
| 0 | 368 | | highlightedParcelText.text = showCursorCoords ? $"{cursorMapCoords.x}, {cursorMapCoords.y}" : string.Empty; |
| | 369 | |
|
| 0 | 370 | | if (highlightedParcelText.text != previousText && !Input.GetMouseButton(0)) { OnMovedParcelCursor?.Invoke(cu |
| | 371 | |
|
| | 372 | | // ---------------------------------------------------- |
| | 373 | | // TODO: Use sceneInfo to highlight whole scene parcels and populate scenes hover info on navmap once we can |
| | 374 | | // var sceneInfo = mapMetadata.GetSceneInfo(cursorMapCoords.x, cursorMapCoords.y); |
| 0 | 375 | | } |
| | 376 | |
|
| | 377 | | void UpdateParcelHold() |
| | 378 | | { |
| 0 | 379 | | if (Vector2.Distance(lastClickedCursorMapCoords, cursorMapCoords) > MAX_CURSOR_PARCEL_DISTANCE / (scaleFacto |
| 0 | 380 | | } |
| | 381 | |
|
| | 382 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) |
| | 383 | | { |
| 0 | 384 | | validWorldRanges = current.validWorldRanges; |
| 0 | 385 | | } |
| | 386 | |
|
| | 387 | | bool CoordinatesAreInsideTheWorld(int xCoord, int yCoord) |
| | 388 | | { |
| 0 | 389 | | foreach (WorldRange worldRange in validWorldRanges) |
| | 390 | | { |
| 0 | 391 | | if (worldRange.Contains(xCoord, yCoord)) { return true; } |
| | 392 | | } |
| | 393 | |
|
| 0 | 394 | | return false; |
| 0 | 395 | | } |
| | 396 | |
|
| | 397 | | private void MapRenderer_OnSceneInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 398 | | { |
| 0 | 399 | | if (!sceneInfo.isPOI) |
| 0 | 400 | | return; |
| | 401 | |
|
| 0 | 402 | | if (scenesOfInterest.Contains(sceneInfo)) |
| 0 | 403 | | return; |
| | 404 | |
|
| 0 | 405 | | if (IsEmptyParcel(sceneInfo)) |
| 0 | 406 | | return; |
| | 407 | |
|
| 0 | 408 | | scenesOfInterest.Add(sceneInfo); |
| | 409 | |
|
| 0 | 410 | | GameObject go = Instantiate(scenesOfInterestIconPrefab.gameObject, overlayContainer.transform); |
| | 411 | |
|
| 0 | 412 | | Vector2 centerTile = Vector2.zero; |
| | 413 | |
|
| 0 | 414 | | foreach (var parcel in sceneInfo.parcels) { centerTile += parcel; } |
| | 415 | |
|
| 0 | 416 | | centerTile /= (float)sceneInfo.parcels.Count; |
| 0 | 417 | | float distance = float.PositiveInfinity; |
| 0 | 418 | | Vector2 centerParcel = Vector2.zero; |
| | 419 | |
|
| 0 | 420 | | foreach (var parcel in sceneInfo.parcels) |
| | 421 | | { |
| 0 | 422 | | if (Vector2.Distance(centerTile, parcel) < distance) |
| | 423 | | { |
| 0 | 424 | | distance = Vector2.Distance(centerParcel, parcel); |
| 0 | 425 | | centerParcel = parcel; |
| | 426 | | } |
| | 427 | | } |
| | 428 | |
|
| 0 | 429 | | (go.transform as RectTransform).anchoredPosition = MapUtils.CoordsToPosition(centerParcel); |
| | 430 | |
|
| 0 | 431 | | MapSceneIcon icon = go.GetComponent<MapSceneIcon>(); |
| | 432 | |
|
| 0 | 433 | | if (icon.title != null) |
| 0 | 434 | | icon.title.text = sceneInfo.name.Length > MAX_SCENE_CHARACTER_TITLE ? sceneInfo.name.Substring(0, MAX_SC |
| | 435 | |
|
| 0 | 436 | | scenesOfInterestMarkers.Add(sceneInfo, go); |
| 0 | 437 | | } |
| | 438 | |
|
| | 439 | | public void SetPointOfInterestActive(bool areActive) |
| | 440 | | { |
| 0 | 441 | | foreach (GameObject pointOfInterestGameObject in scenesOfInterestMarkers.Values) { pointOfInterestGameObject |
| 0 | 442 | | } |
| | 443 | |
|
| | 444 | | private bool IsEmptyParcel(MinimapMetadata.MinimapSceneInfo sceneInfo) |
| | 445 | | { |
| 0 | 446 | | return (sceneInfo.name != null && sceneInfo.name.Equals(EMPTY_PARCEL_NAME)); |
| | 447 | | } |
| | 448 | |
|
| | 449 | | private void OnOtherPlayersAdded(string userId, Player player) |
| | 450 | | { |
| 0 | 451 | | var poolable = usersInfoPool.Get(); |
| 0 | 452 | | var marker = poolable.gameObject.GetComponent<MapUserIcon>(); |
| 0 | 453 | | marker.gameObject.name = $"UserIcon-{player.name}"; |
| 0 | 454 | | marker.gameObject.transform.SetParent(overlayContainerPlayers.transform, true); |
| 0 | 455 | | marker.Populate(player); |
| 0 | 456 | | marker.gameObject.SetActive(otherPlayersIconsEnabled); |
| 0 | 457 | | marker.transform.localScale = Vector3.one; |
| 0 | 458 | | usersInfoMarkers.Add(userId, poolable); |
| 0 | 459 | | } |
| | 460 | |
|
| | 461 | | private void OnOtherPlayerRemoved(string userId, Player player) |
| | 462 | | { |
| 0 | 463 | | if (!usersInfoMarkers.TryGetValue(userId, out PoolableObject go)) { return; } |
| | 464 | |
|
| 0 | 465 | | usersInfoPool.Release(go); |
| 0 | 466 | | usersInfoMarkers.Remove(userId); |
| 0 | 467 | | } |
| | 468 | |
|
| | 469 | | private void ConfigureUserIcon(GameObject iconGO, Vector3 pos) |
| | 470 | | { |
| 0 | 471 | | var gridPosition = Utils.WorldToGridPositionUnclamped(pos); |
| 0 | 472 | | iconGO.transform.localPosition = MapUtils.CoordsToPosition(Vector2Int.RoundToInt(gridPosition)); |
| 0 | 473 | | } |
| | 474 | |
|
| | 475 | | private void OnCharacterRotate(Vector3 current, Vector3 previous) |
| | 476 | | { |
| 0 | 477 | | UpdateRendering(Utils.WorldToGridPositionUnclamped(playerWorldPosition.Get())); |
| 0 | 478 | | } |
| | 479 | |
|
| | 480 | | public void OnCharacterSetPosition(Vector2Int newCoords, Vector2Int oldCoords) |
| | 481 | | { |
| 0 | 482 | | if (oldCoords == newCoords) |
| 0 | 483 | | return; |
| | 484 | |
|
| 0 | 485 | | UpdateRendering(new Vector2((float)newCoords.x, (float)newCoords.y)); |
| 0 | 486 | | } |
| | 487 | |
|
| | 488 | | public void UpdateRendering(Vector2 newCoords) |
| | 489 | | { |
| 0 | 490 | | UpdateBackgroundLayer(newCoords); |
| 0 | 491 | | UpdateSelectionLayer(); |
| 0 | 492 | | UpdateOverlayLayer(); |
| 0 | 493 | | } |
| | 494 | |
|
| | 495 | | void UpdateBackgroundLayer(Vector2 newCoords) |
| | 496 | | { |
| 0 | 497 | | atlas.CenterToTile(newCoords); |
| 0 | 498 | | } |
| | 499 | |
|
| | 500 | | void UpdateSelectionLayer() |
| | 501 | | { |
| | 502 | | //TODO(Brian): Build and place here the scene highlight if applicable. |
| 0 | 503 | | } |
| | 504 | |
|
| | 505 | | void UpdateOverlayLayer() |
| | 506 | | { |
| | 507 | | //NOTE(Brian): Player icon |
| 0 | 508 | | Vector3 f = CommonScriptableObjects.cameraForward.Get(); |
| 0 | 509 | | Quaternion playerAngle = Quaternion.Euler(0, 0, Mathf.Atan2(-f.x, f.z) * Mathf.Rad2Deg); |
| | 510 | |
|
| 0 | 511 | | var gridPosition = playerGridPosition; |
| 0 | 512 | | playerPositionIcon.anchoredPosition = MapUtils.CoordsToPositionWithOffset(gridPosition); |
| 0 | 513 | | playerPositionIcon.rotation = playerAngle; |
| 0 | 514 | | } |
| | 515 | |
|
| | 516 | | // Called by the parcelhighlight image button |
| | 517 | | public void ClickMousePositionParcel() |
| | 518 | | { |
| 0 | 519 | | highlightedParcelText.text = string.Empty; |
| 0 | 520 | | lastClickedCursorMapCoords = cursorMapCoords; |
| 0 | 521 | | OnParcelClicked?.Invoke(cursorMapCoords.x, cursorMapCoords.y); |
| 0 | 522 | | } |
| | 523 | | } |
| | 524 | | } |