| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace DCL.Controllers |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// This class is the domain-specific glue for BlockerInstanceHandler. |
| | 11 | | /// <br/><br/> |
| | 12 | | /// Responsibilities:<br/> |
| | 13 | | /// - Spawning blockers depending on scene state<br/> |
| | 14 | | /// - Moving blockers when the world is repositioned<br/> |
| | 15 | | /// - Handling lifecycle of BlockerInstanceHandler<br/> |
| | 16 | | /// </summary> |
| | 17 | | public class WorldBlockersController : IWorldBlockersController |
| | 18 | | { |
| 429 | 19 | | public bool enabled = true; |
| | 20 | |
|
| | 21 | | Transform blockersParent; |
| | 22 | |
|
| | 23 | | ISceneHandler sceneHandler; |
| | 24 | | IBlockerInstanceHandler blockerInstanceHandler; |
| | 25 | |
|
| 429 | 26 | | HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>(); |
| 429 | 27 | | HashSet<Vector2Int> blockersToAdd = new HashSet<Vector2Int>(); |
| | 28 | |
|
| 1 | 29 | | static Vector2Int[] aroundOffsets = |
| | 30 | | { |
| | 31 | | new Vector2Int(1, 0), |
| | 32 | | new Vector2Int(-1, 0), |
| | 33 | | new Vector2Int(0, 1), |
| | 34 | | new Vector2Int(0, -1), |
| | 35 | | new Vector2Int(1, 1), |
| | 36 | | new Vector2Int(-1, -1), |
| | 37 | | new Vector2Int(1, -1), |
| | 38 | | new Vector2Int(-1, 1) |
| | 39 | | }; |
| | 40 | |
|
| 13 | 41 | | private BaseVariable<int> worldBlockersLimit => DataStore.i.worldBlockers.worldBlockerLimit; |
| 859 | 42 | | private BaseVariable<bool> worldBlockersEnabled => DataStore.i.worldBlockers.worldBlockerEnabled; |
| | 43 | |
|
| | 44 | | void OnRendererStateChange(bool newValue, bool oldValue) |
| | 45 | | { |
| 540 | 46 | | blockerInstanceHandler.SetCollision(newValue); |
| | 47 | |
|
| 540 | 48 | | if (newValue && DataStore.i.debugConfig.isDebugMode.Get()) |
| 0 | 49 | | SetEnabled(false); |
| 540 | 50 | | } |
| | 51 | |
|
| 429 | 52 | | public WorldBlockersController(IBlockerInstanceHandler blockerInstanceHandler = null, |
| | 53 | | ISceneHandler sceneHandler = null) |
| | 54 | | { |
| 429 | 55 | | this.sceneHandler = sceneHandler; |
| 429 | 56 | | this.blockerInstanceHandler = blockerInstanceHandler; |
| 429 | 57 | | worldBlockersEnabled.OnChange += OnWorldsBlockerEnabledChange; |
| 429 | 58 | | } |
| | 59 | |
|
| | 60 | | public void SetupWorldBlockers() |
| | 61 | | { |
| | 62 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 500 | 63 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 64 | | return; |
| | 65 | | #endif |
| | 66 | |
|
| 500 | 67 | | if (!enabled || sceneHandler == null) |
| 1 | 68 | | return; |
| | 69 | |
|
| 499 | 70 | | SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords()); |
| 499 | 71 | | } |
| | 72 | |
|
| | 73 | | public void SetEnabled(bool targetValue) |
| | 74 | | { |
| | 75 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 2 | 76 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 77 | | return; |
| | 78 | | #endif |
| | 79 | |
|
| 2 | 80 | | enabled = targetValue; |
| | 81 | |
|
| 2 | 82 | | if (!enabled) |
| 1 | 83 | | blockerInstanceHandler.DestroyAllBlockers(); |
| 2 | 84 | | } |
| | 85 | |
|
| | 86 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 87 | | { |
| 2 | 88 | | var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position |
| 2 | 89 | | blockersParent.position = newPosition; |
| 2 | 90 | | } |
| | 91 | |
|
| | 92 | | public void Dispose() |
| | 93 | | { |
| | 94 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 430 | 95 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 96 | | return; |
| | 97 | | #endif |
| | 98 | |
|
| 430 | 99 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 430 | 100 | | worldBlockersEnabled.OnChange -= OnWorldsBlockerEnabledChange; |
| 430 | 101 | | blockerInstanceHandler?.DestroyAllBlockers(); |
| | 102 | |
|
| 430 | 103 | | if (blockersParent != null) |
| 425 | 104 | | Object.Destroy(blockersParent.gameObject); |
| | 105 | |
|
| 430 | 106 | | enabled = false; |
| 430 | 107 | | } |
| | 108 | |
|
| | 109 | | public void Initialize() |
| | 110 | | { |
| 425 | 111 | | enabled = true; |
| 425 | 112 | | blockersParent = new GameObject("WorldBlockers").transform; |
| 425 | 113 | | blockersParent.position = Vector3.zero; |
| | 114 | |
|
| 425 | 115 | | if (blockerInstanceHandler == null) |
| | 116 | | { |
| 425 | 117 | | var blockerAnimationHandler = new BlockerAnimationHandler(); |
| 425 | 118 | | blockerInstanceHandler = new BlockerInstanceHandler(blockerAnimationHandler); |
| | 119 | | } |
| | 120 | |
|
| 425 | 121 | | if (this.sceneHandler == null) |
| 425 | 122 | | this.sceneHandler = DCL.Environment.i.world.state; |
| | 123 | |
|
| 425 | 124 | | blockerInstanceHandler.SetParent(blockersParent); |
| | 125 | |
|
| 425 | 126 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 425 | 127 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 128 | |
|
| 425 | 129 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 425 | 130 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 425 | 131 | | } |
| | 132 | |
|
| | 133 | | internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords) |
| | 134 | | { |
| 499 | 135 | | if (allLoadedParcelCoords.Count == 0) |
| 486 | 136 | | return; |
| | 137 | |
|
| 13 | 138 | | blockersToRemove.Clear(); |
| 13 | 139 | | blockersToAdd.Clear(); |
| | 140 | |
|
| 13 | 141 | | var blockers = blockerInstanceHandler.GetBlockers(); |
| | 142 | |
|
| | 143 | | // Detect blockers to be removed |
| 50 | 144 | | foreach (var item in blockers) |
| | 145 | | { |
| 12 | 146 | | if (allLoadedParcelCoords.Contains(item.Key)) |
| | 147 | | { |
| 2 | 148 | | blockersToRemove.Add(item.Key); |
| | 149 | | } |
| | 150 | | else |
| | 151 | | { |
| 10 | 152 | | bool foundAroundLoadedScenes = false; |
| 70 | 153 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 154 | | { |
| 35 | 155 | | Vector2Int offset = aroundOffsets[i]; |
| 35 | 156 | | Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y); |
| | 157 | |
|
| 35 | 158 | | if (allLoadedParcelCoords.Contains(checkedPosition)) |
| | 159 | | { |
| 10 | 160 | | foundAroundLoadedScenes = true; |
| 10 | 161 | | break; |
| | 162 | | } |
| | 163 | | } |
| | 164 | |
|
| 10 | 165 | | if (!foundAroundLoadedScenes) |
| 0 | 166 | | blockersToRemove.Add(item.Key); |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 13 | 170 | | blockersToAdd = LookForLimits(dontAddABlockerHere: allLoadedParcelCoords, blockers, 0); |
| | 171 | |
|
| | 172 | | // Remove extra blockers |
| 30 | 173 | | foreach (var coords in blockersToRemove) |
| 2 | 174 | | blockerInstanceHandler.HideBlocker(coords, false); |
| | 175 | |
|
| | 176 | | // Add missing blockers |
| 134 | 177 | | foreach (var coords in blockersToAdd) |
| 54 | 178 | | blockerInstanceHandler.ShowBlocker(coords, false, CommonScriptableObjects.rendererState.Get()); |
| 13 | 179 | | } |
| | 180 | |
|
| | 181 | | private HashSet<Vector2Int> LookForLimits(HashSet<Vector2Int> dontAddABlockerHere, Dictionary<Vector2Int, IPoola |
| | 182 | | { |
| 13 | 183 | | HashSet<Vector2Int> blockersCandidate = new HashSet<Vector2Int>(); |
| | 184 | |
|
| | 185 | | // Detect missing blockers to be added |
| 13 | 186 | | using (var it = dontAddABlockerHere.GetEnumerator()) |
| | 187 | | { |
| 39 | 188 | | while (it.MoveNext()) |
| | 189 | | { |
| 26 | 190 | | Vector2Int pos = it.Current; |
| | 191 | |
|
| 468 | 192 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 193 | | { |
| 208 | 194 | | Vector2Int offset = aroundOffsets[i]; |
| 208 | 195 | | int xCandidate = pos.x + offset.x; |
| 208 | 196 | | int yCandidate = pos.y + offset.y; |
| 208 | 197 | | Vector2Int checkedPosition = new Vector2Int(xCandidate, yCandidate); |
| | 198 | |
|
| 208 | 199 | | if (!dontAddABlockerHere.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition)) |
| | 200 | | { |
| | 201 | | // We add a blocker here because it is either part of a World, or because it contains a scen |
| 144 | 202 | | if (DataStore.i.common.isWorld.Get() || IsSceneKnown (checkedPosition)) |
| 80 | 203 | | blockersCandidate.Add(checkedPosition); |
| | 204 | | } |
| | 205 | | } |
| | 206 | | } |
| 13 | 207 | | } |
| | 208 | |
|
| 13 | 209 | | if (currentLimitIterationEvaluation == worldBlockersLimit.Get()) |
| 13 | 210 | | return blockersCandidate; |
| | 211 | | else |
| | 212 | | { |
| 0 | 213 | | blockersCandidate.UnionWith(dontAddABlockerHere); |
| 0 | 214 | | return LookForLimits(blockersCandidate, blockers, currentLimitIterationEvaluation + 1); |
| | 215 | | } |
| | 216 | | } |
| | 217 | |
|
| | 218 | | private void OnWorldsBlockerEnabledChange(bool newState, bool _) |
| | 219 | | { |
| 0 | 220 | | SetEnabled(newState); |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | private bool IsSceneKnown(Vector2Int parcel) |
| | 224 | | { |
| | 225 | | // Note: This returns false when the set of coordinates is about a parcel |
| | 226 | | // where the kernel didn't provide yet any information to the renderer |
| 144 | 227 | | return sceneHandler.GetScene(parcel) != null; |
| | 228 | | } |
| | 229 | | } |
| | 230 | | } |