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