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