| | 1 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using Random = UnityEngine.Random; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Receive a list of hot scenes and handle what markers to show, what markers to hide and places them on the map |
| | 11 | | /// </summary> |
| | 12 | | internal class MarkersHandler : IDisposable |
| | 13 | | { |
| | 14 | | internal readonly List<UserPositionMarker> availableMarkers; |
| | 15 | | internal readonly List<UserPositionMarker> usedMarkers; |
| | 16 | | readonly Func<Vector2Int, Vector2> coordToMapPosition; |
| | 17 | |
|
| | 18 | | readonly ExclusionArea exclusionArea; |
| | 19 | | readonly ScenesFilter scenesFilter; |
| | 20 | |
|
| | 21 | | int maxMarkers; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Constructor |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="markerPrefab">prefab for markers</param> |
| | 27 | | /// <param name="overlayContainer">parent for markers</param> |
| | 28 | | /// <param name="maxMarkers">max amount of markers (pool)</param> |
| | 29 | | /// <param name="coordToMapPosFunc">function to transform coords to map position</param> |
| 1 | 30 | | public MarkersHandler(UserMarkerObject markerPrefab, Transform overlayContainer, int maxMarkers, Func<Vector2Int |
| | 31 | | { |
| 1 | 32 | | this.maxMarkers = maxMarkers; |
| 1 | 33 | | this.coordToMapPosition = coordToMapPosFunc; |
| | 34 | |
|
| 1 | 35 | | exclusionArea = new ExclusionArea(); |
| 1 | 36 | | scenesFilter = new ScenesFilter(); |
| | 37 | |
|
| 1 | 38 | | availableMarkers = new List<UserPositionMarker>(maxMarkers); |
| 1 | 39 | | usedMarkers = new List<UserPositionMarker>(maxMarkers); |
| | 40 | |
|
| 12 | 41 | | for (int i = 0; i < maxMarkers; i++) |
| | 42 | | { |
| 5 | 43 | | var marker = new UserPositionMarker(GameObject.Instantiate(markerPrefab, overlayContainer)); |
| 5 | 44 | | availableMarkers.Add(marker); |
| 5 | 45 | | marker.SetActive(false); |
| | 46 | | } |
| 1 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Set exclusion area. Markers inside this area will be hidden, to avoid overlapping with markers set with comm |
| | 51 | | /// After set it will iterate through current markers to hide or show them respectively. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="center">center of the exclusion area</param> |
| | 54 | | /// <param name="area">size of the exclusion area</param> |
| | 55 | | public void SetExclusionArea(Vector2Int center, int area) |
| | 56 | | { |
| 2 | 57 | | exclusionArea.position = center; |
| 2 | 58 | | exclusionArea.area = area; |
| 2 | 59 | | ApplyExclusionArea(); |
| 2 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Set scenes to set markers to. Scenes will be filtered and it coordinates will be extracted. |
| | 64 | | /// Then markers will be set and will be shown or hidden according to the current exclusion area. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="hotScenes">list of populated scenes</param> |
| | 67 | | public void SetMarkers(List<IHotScenesController.HotSceneInfo> hotScenes) |
| | 68 | | { |
| 2 | 69 | | var parcelList = scenesFilter.Filter(hotScenes, maxMarkers); |
| 2 | 70 | | ResfreshMarkersPoolLists(parcelList.Count); |
| 14 | 71 | | for (int i = 0; i < parcelList.Count && i < usedMarkers.Count; i++) |
| | 72 | | { |
| 5 | 73 | | SetMarker(usedMarkers[i], parcelList[i]); |
| | 74 | | } |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Dispose() |
| | 78 | | { |
| 1 | 79 | | int markersCount = availableMarkers.Count; |
| 8 | 80 | | for (int i = 0; i < markersCount; i++) |
| | 81 | | { |
| 3 | 82 | | availableMarkers[i].Dispose(); |
| | 83 | | } |
| | 84 | |
|
| 6 | 85 | | for (int i = 0; i < usedMarkers.Count; i++) |
| | 86 | | { |
| 2 | 87 | | usedMarkers[i].Dispose(); |
| | 88 | | } |
| | 89 | |
|
| 1 | 90 | | availableMarkers.Clear(); |
| 1 | 91 | | usedMarkers.Clear(); |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | private void SetMarker(UserPositionMarker marker, ParcelData parcelData) |
| | 95 | | { |
| 5 | 96 | | marker.name = $"UsersPositionMarker({parcelData.coords.x},{parcelData.coords.y})"; |
| | 97 | |
|
| 5 | 98 | | var coords = new Vector2(parcelData.coords.x + Random.Range(-0.5f, 0.5f), parcelData.coords.y + Random.Range |
| 5 | 99 | | marker.localPosition = coordToMapPosition(Vector2Int.RoundToInt(coords)); |
| | 100 | |
|
| 5 | 101 | | marker.coords = parcelData.coords; |
| 5 | 102 | | marker.realmServer = parcelData.realmServer; |
| 5 | 103 | | marker.realmLayer = parcelData.realmLayer; |
| | 104 | |
|
| 5 | 105 | | marker.SetActive(!exclusionArea.Contains(parcelData.coords)); |
| 5 | 106 | | } |
| | 107 | |
|
| | 108 | | private void ApplyExclusionArea() |
| | 109 | | { |
| 2 | 110 | | if (usedMarkers.Count == 0) |
| | 111 | | { |
| 1 | 112 | | return; |
| | 113 | | } |
| | 114 | |
|
| 1 | 115 | | using (var iterator = usedMarkers.GetEnumerator()) |
| | 116 | | { |
| 4 | 117 | | while (iterator.MoveNext()) |
| | 118 | | { |
| 3 | 119 | | iterator.Current.SetActive(!exclusionArea.Contains(iterator.Current.coords)); |
| | 120 | | } |
| 1 | 121 | | } |
| 1 | 122 | | } |
| | 123 | |
|
| | 124 | | private void ResfreshMarkersPoolLists(int amountToBeUsed) |
| | 125 | | { |
| 2 | 126 | | if (amountToBeUsed == usedMarkers.Count) |
| 0 | 127 | | return; |
| | 128 | |
|
| 2 | 129 | | if (amountToBeUsed > usedMarkers.Count) |
| | 130 | | { |
| 1 | 131 | | int addAmount = amountToBeUsed - usedMarkers.Count; |
| 8 | 132 | | for (int i = 0; i < addAmount && i < availableMarkers.Count; i++) |
| | 133 | | { |
| 3 | 134 | | var marker = availableMarkers[i]; |
| 3 | 135 | | availableMarkers.RemoveAt(i); |
| 3 | 136 | | usedMarkers.Add(marker); |
| | 137 | | } |
| | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 1 | 141 | | int removeAmount = usedMarkers.Count - amountToBeUsed; |
| 4 | 142 | | for (int i = 0; i < removeAmount && i < usedMarkers.Count; i++) |
| | 143 | | { |
| 1 | 144 | | var marker = usedMarkers[i]; |
| 1 | 145 | | usedMarkers.RemoveAt(i); |
| 1 | 146 | | marker.SetActive(false); |
| 1 | 147 | | availableMarkers.Add(marker); |
| | 148 | | } |
| | 149 | | } |
| 2 | 150 | | } |
| | 151 | | } |
| | 152 | | } |