| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Rendering; |
| | 5 | | using UnityEngine; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace DCL.Controllers |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// This class is the domain-specific glue for BlockerInstanceHandler. |
| | 12 | | /// <br/><br/> |
| | 13 | | /// Responsibilities:<br/> |
| | 14 | | /// - Spawning blockers depending on scene state<br/> |
| | 15 | | /// - Moving blockers when the world is repositioned<br/> |
| | 16 | | /// - Handling lifecycle of BlockerInstanceHandler<br/> |
| | 17 | | /// </summary> |
| | 18 | | public class WorldBlockersController : IWorldBlockersController |
| | 19 | | { |
| 634 | 20 | | public bool enabled = true; |
| | 21 | |
|
| | 22 | | Transform blockersParent; |
| | 23 | |
|
| | 24 | | ISceneHandler sceneHandler; |
| | 25 | | IBlockerInstanceHandler blockerInstanceHandler; |
| | 26 | |
|
| 634 | 27 | | HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>(); |
| 634 | 28 | | HashSet<Vector2Int> blockersToAdd = new HashSet<Vector2Int>(); |
| | 29 | |
|
| 1 | 30 | | static Vector2Int[] aroundOffsets = |
| | 31 | | { |
| | 32 | | new Vector2Int(1, 0), |
| | 33 | | new Vector2Int(-1, 0), |
| | 34 | | new Vector2Int(0, 1), |
| | 35 | | new Vector2Int(0, -1), |
| | 36 | | new Vector2Int(1, 1), |
| | 37 | | new Vector2Int(-1, -1), |
| | 38 | | new Vector2Int(1, -1), |
| | 39 | | new Vector2Int(-1, 1) |
| | 40 | | }; |
| | 41 | |
|
| | 42 | | void OnRendererStateChange(bool newValue, bool oldValue) |
| | 43 | | { |
| 4096 | 44 | | if (newValue && DataStore.i.debugConfig.isDebugMode.Get()) |
| 0 | 45 | | SetEnabled(false); |
| 4096 | 46 | | } |
| | 47 | |
|
| 634 | 48 | | public WorldBlockersController(IBlockerInstanceHandler blockerInstanceHandler = null, ISceneHandler sceneHandler |
| | 49 | | { |
| 634 | 50 | | this.sceneHandler = sceneHandler; |
| 634 | 51 | | this.blockerInstanceHandler = blockerInstanceHandler; |
| 634 | 52 | | } |
| | 53 | |
|
| | 54 | | public void SetupWorldBlockers() |
| | 55 | | { |
| | 56 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 755 | 57 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 58 | | return; |
| | 59 | | #endif |
| | 60 | |
|
| 755 | 61 | | if (!enabled || sceneHandler == null) |
| 8 | 62 | | return; |
| | 63 | |
|
| 747 | 64 | | SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords()); |
| 747 | 65 | | } |
| | 66 | |
|
| | 67 | | public void SetEnabled(bool targetValue) |
| | 68 | | { |
| | 69 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 8 | 70 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 71 | | return; |
| | 72 | | #endif |
| | 73 | |
|
| 8 | 74 | | enabled = targetValue; |
| | 75 | |
|
| 8 | 76 | | if (!enabled) |
| 6 | 77 | | blockerInstanceHandler.DestroyAllBlockers(); |
| 8 | 78 | | } |
| | 79 | |
|
| | 80 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 81 | | { |
| 2 | 82 | | var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position |
| 2 | 83 | | blockersParent.position = newPosition; |
| 2 | 84 | | } |
| | 85 | |
|
| | 86 | | public void Dispose() |
| | 87 | | { |
| | 88 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 635 | 89 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 90 | | return; |
| | 91 | | #endif |
| | 92 | |
|
| 635 | 93 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 635 | 94 | | blockerInstanceHandler.DestroyAllBlockers(); |
| | 95 | |
|
| 635 | 96 | | if (blockersParent != null) |
| 630 | 97 | | Object.Destroy(blockersParent.gameObject); |
| | 98 | |
|
| 635 | 99 | | enabled = false; |
| 635 | 100 | | } |
| | 101 | |
|
| | 102 | | public void Initialize() |
| | 103 | | { |
| 630 | 104 | | enabled = true; |
| 630 | 105 | | blockersParent = new GameObject("WorldBlockers").transform; |
| 630 | 106 | | blockersParent.position = Vector3.zero; |
| | 107 | |
|
| 630 | 108 | | if ( blockerInstanceHandler == null ) |
| | 109 | | { |
| 630 | 110 | | var blockerAnimationHandler = new BlockerAnimationHandler(); |
| 630 | 111 | | blockerInstanceHandler = new BlockerInstanceHandler(blockerAnimationHandler); |
| | 112 | | } |
| | 113 | |
|
| 630 | 114 | | if ( this.sceneHandler == null ) |
| 630 | 115 | | this.sceneHandler = DCL.Environment.i.world.state; |
| | 116 | |
|
| 630 | 117 | | blockerInstanceHandler.SetParent(blockersParent); |
| | 118 | |
|
| 630 | 119 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 630 | 120 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 121 | |
|
| 630 | 122 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 630 | 123 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 630 | 124 | | } |
| | 125 | |
|
| | 126 | | internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords) |
| | 127 | | { |
| 747 | 128 | | if (allLoadedParcelCoords.Count == 0) |
| 740 | 129 | | return; |
| | 130 | |
|
| 7 | 131 | | blockersToRemove.Clear(); |
| 7 | 132 | | blockersToAdd.Clear(); |
| | 133 | |
|
| 7 | 134 | | var blockers = blockerInstanceHandler.GetBlockers(); |
| | 135 | |
|
| | 136 | | // Detect blockers to be removed |
| 38 | 137 | | foreach (var item in blockers) |
| | 138 | | { |
| 12 | 139 | | if (allLoadedParcelCoords.Contains(item.Key)) |
| | 140 | | { |
| 2 | 141 | | blockersToRemove.Add(item.Key); |
| 2 | 142 | | } |
| | 143 | | else |
| | 144 | | { |
| 10 | 145 | | bool foundAroundLoadedScenes = false; |
| 70 | 146 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 147 | | { |
| 35 | 148 | | Vector2Int offset = aroundOffsets[i]; |
| 35 | 149 | | Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y); |
| | 150 | |
|
| 35 | 151 | | if (allLoadedParcelCoords.Contains(checkedPosition)) |
| | 152 | | { |
| 10 | 153 | | foundAroundLoadedScenes = true; |
| 10 | 154 | | break; |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| 10 | 158 | | if (!foundAroundLoadedScenes) |
| 0 | 159 | | blockersToRemove.Add(item.Key); |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| | 163 | | // Detect missing blockers to be added |
| 7 | 164 | | using (var it = allLoadedParcelCoords.GetEnumerator()) |
| | 165 | | { |
| 27 | 166 | | while (it.MoveNext()) |
| | 167 | | { |
| 20 | 168 | | Vector2Int pos = it.Current; |
| | 169 | |
|
| 360 | 170 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 171 | | { |
| 160 | 172 | | Vector2Int offset = aroundOffsets[i]; |
| 160 | 173 | | Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y); |
| | 174 | |
|
| 160 | 175 | | if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition)) |
| | 176 | | { |
| 96 | 177 | | blockersToAdd.Add(checkedPosition); |
| | 178 | | } |
| | 179 | | } |
| | 180 | | } |
| 7 | 181 | | } |
| | 182 | |
|
| | 183 | | // Remove extra blockers |
| 18 | 184 | | foreach (var coords in blockersToRemove) |
| | 185 | | { |
| 2 | 186 | | blockerInstanceHandler.HideBlocker(coords, false); |
| | 187 | | } |
| | 188 | |
|
| | 189 | | // Add missing blockers |
| 154 | 190 | | foreach (var coords in blockersToAdd) |
| | 191 | | { |
| 70 | 192 | | blockerInstanceHandler.ShowBlocker(coords); |
| | 193 | | } |
| 7 | 194 | | } |
| | 195 | | } |
| | 196 | | } |