| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using Random = UnityEngine.Random; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Receive a list of hot scenes and handle what markers to show, what markers to hide and places them on the map |
| | 10 | | /// </summary> |
| | 11 | | internal class MarkersHandler : IDisposable |
| | 12 | | { |
| | 13 | | internal readonly List<UserPositionMarker> availableMarkers; |
| | 14 | | internal readonly List<UserPositionMarker> usedMarkers; |
| | 15 | | readonly Func<float, float, Vector3> coordToMapPosition; |
| | 16 | |
|
| | 17 | | readonly ExclusionArea exclusionArea; |
| | 18 | | readonly ScenesFilter scenesFilter; |
| | 19 | |
|
| | 20 | | int maxMarkers; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Constructor |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="markerPrefab">prefab for markers</param> |
| | 26 | | /// <param name="overlayContainer">parent for markers</param> |
| | 27 | | /// <param name="maxMarkers">max amount of markers (pool)</param> |
| | 28 | | /// <param name="coordToMapPosFunc">function to transform coords to map position</param> |
| 11 | 29 | | public MarkersHandler(UserMarkerObject markerPrefab, Transform overlayContainer, int maxMarkers, Func<float, flo |
| | 30 | | { |
| 11 | 31 | | this.maxMarkers = maxMarkers; |
| 11 | 32 | | this.coordToMapPosition = coordToMapPosFunc; |
| | 33 | |
|
| 11 | 34 | | exclusionArea = new ExclusionArea(); |
| 11 | 35 | | scenesFilter = new ScenesFilter(); |
| | 36 | |
|
| 11 | 37 | | availableMarkers = new List<UserPositionMarker>(maxMarkers); |
| 11 | 38 | | usedMarkers = new List<UserPositionMarker>(maxMarkers); |
| | 39 | |
|
| 2032 | 40 | | for (int i = 0; i < maxMarkers; i++) |
| | 41 | | { |
| 1005 | 42 | | var marker = new UserPositionMarker(GameObject.Instantiate(markerPrefab, overlayContainer)); |
| 1005 | 43 | | availableMarkers.Add(marker); |
| 1005 | 44 | | marker.SetActive(false); |
| | 45 | | } |
| 11 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Set exclusion area. Markers inside this area will be hidden, to avoid overlapping with markers set with comm |
| | 50 | | /// After set it will iterate through current markers to hide or show them respectively. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="center">center of the exclusion area</param> |
| | 53 | | /// <param name="area">size of the exclusion area</param> |
| | 54 | | public void SetExclusionArea(Vector2Int center, int area) |
| | 55 | | { |
| 11 | 56 | | exclusionArea.position = center; |
| 11 | 57 | | exclusionArea.area = area; |
| 11 | 58 | | ApplyExclusionArea(); |
| 11 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Set scenes to set markers to. Scenes will be filtered and it coordinates will be extracted. |
| | 63 | | /// Then markers will be set and will be shown or hidden according to the current exclusion area. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="hotScenes">list of populated scenes</param> |
| | 66 | | public void SetMarkers(List<HotScenesController.HotSceneInfo> hotScenes) |
| | 67 | | { |
| 2 | 68 | | var parcelList = scenesFilter.Filter(hotScenes, maxMarkers); |
| 2 | 69 | | ResfreshMarkersPoolLists(parcelList.Count); |
| 14 | 70 | | for (int i = 0; i < parcelList.Count && i < usedMarkers.Count; i++) |
| | 71 | | { |
| 5 | 72 | | SetMarker(usedMarkers[i], parcelList[i]); |
| | 73 | | } |
| 2 | 74 | | } |
| | 75 | |
|
| | 76 | | public void Dispose() |
| | 77 | | { |
| 13 | 78 | | int markersCount = availableMarkers.Count; |
| 2032 | 79 | | for (int i = 0; i < markersCount; i++) |
| | 80 | | { |
| 1003 | 81 | | availableMarkers[i].Dispose(); |
| | 82 | | } |
| | 83 | |
|
| 30 | 84 | | for (int i = 0; i < usedMarkers.Count; i++) |
| | 85 | | { |
| 2 | 86 | | usedMarkers[i].Dispose(); |
| | 87 | | } |
| | 88 | |
|
| 13 | 89 | | availableMarkers.Clear(); |
| 13 | 90 | | usedMarkers.Clear(); |
| 13 | 91 | | } |
| | 92 | |
|
| | 93 | | private void SetMarker(UserPositionMarker marker, ParcelData parcelData) |
| | 94 | | { |
| 5 | 95 | | marker.name = $"UsersPositionMarker({parcelData.coords.x},{parcelData.coords.y})"; |
| | 96 | |
|
| 5 | 97 | | marker.localPosition = coordToMapPosition( |
| | 98 | | parcelData.coords.x + Random.Range(-0.5f, 0.5f), |
| | 99 | | parcelData.coords.y + Random.Range(-0.5f, 0.5f)); |
| | 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 | | { |
| 11 | 110 | | if (usedMarkers.Count == 0) |
| | 111 | | { |
| 10 | 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 | | } |
| 0 | 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 | | } |