| | 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 | | { |
| 669 | 18 | | public bool enabled = true; |
| | 19 | |
|
| | 20 | | Transform blockersParent; |
| | 21 | |
|
| | 22 | | ISceneHandler sceneHandler; |
| | 23 | | IBlockerInstanceHandler blockerInstanceHandler; |
| | 24 | |
|
| 669 | 25 | | HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>(); |
| 669 | 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 | | { |
| 670 | 42 | | this.blockerInstanceHandler = blockerInstanceHandler; |
| 670 | 43 | | this.sceneHandler = sceneHandler; |
| | 44 | |
|
| 670 | 45 | | blockerInstanceHandler.SetParent(blockersParent); |
| | 46 | |
|
| 670 | 47 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 670 | 48 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 49 | |
|
| 670 | 50 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 670 | 51 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 670 | 52 | | } |
| | 53 | |
|
| | 54 | | void OnRendererStateChange(bool newValue, bool oldValue) |
| | 55 | | { |
| 89952 | 56 | | if (newValue && DataStore.i.debugConfig.isDebugMode.Get()) |
| 0 | 57 | | SetEnabled(false); |
| 89952 | 58 | | } |
| | 59 | |
|
| 669 | 60 | | public WorldBlockersController() |
| | 61 | | { |
| 669 | 62 | | blockersParent = new GameObject("WorldBlockers").transform; |
| 669 | 63 | | blockersParent.position = Vector3.zero; |
| 669 | 64 | | } |
| | 65 | |
|
| | 66 | | public static WorldBlockersController CreateWithDefaultDependencies(ISceneHandler sceneHandler, ICullingControll |
| | 67 | | { |
| 0 | 68 | | var worldBlockersController = new WorldBlockersController(); |
| 0 | 69 | | worldBlockersController.InitializeWithDefaultDependencies(sceneHandler, cullingController); |
| 0 | 70 | | return worldBlockersController; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public void InitializeWithDefaultDependencies(ISceneHandler sceneHandler, ICullingController cullingController) |
| | 74 | | { |
| 666 | 75 | | var blockerAnimationHandler = new BlockerAnimationHandler(); |
| 666 | 76 | | var blockerInstanceHandler = new BlockerInstanceHandler(); |
| | 77 | |
|
| 666 | 78 | | blockerInstanceHandler.Initialize( |
| | 79 | | blockerAnimationHandler, |
| | 80 | | cullingController |
| | 81 | | ); |
| | 82 | |
|
| 666 | 83 | | Initialize( |
| | 84 | | sceneHandler, |
| | 85 | | blockerInstanceHandler); |
| 666 | 86 | | } |
| | 87 | |
|
| | 88 | | public void SetupWorldBlockers() |
| | 89 | | { |
| 1376 | 90 | | if (!enabled || sceneHandler == null) |
| 55 | 91 | | return; |
| | 92 | |
|
| 1321 | 93 | | SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords()); |
| 1321 | 94 | | } |
| | 95 | |
|
| | 96 | | public void SetEnabled(bool targetValue) |
| | 97 | | { |
| 28 | 98 | | enabled = targetValue; |
| | 99 | |
|
| 28 | 100 | | if (!enabled) |
| 23 | 101 | | blockerInstanceHandler.DestroyAllBlockers(); |
| 28 | 102 | | } |
| | 103 | |
|
| | 104 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 105 | | { |
| 5 | 106 | | var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position |
| 5 | 107 | | blockersParent.position = newPosition; |
| 5 | 108 | | } |
| | 109 | |
|
| | 110 | | public void Dispose() |
| | 111 | | { |
| 692 | 112 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 692 | 113 | | blockerInstanceHandler.DestroyAllBlockers(); |
| | 114 | |
|
| 692 | 115 | | if (blockersParent != null) |
| 671 | 116 | | Object.Destroy(blockersParent.gameObject); |
| 692 | 117 | | } |
| | 118 | |
|
| | 119 | | internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords) |
| | 120 | | { |
| 1321 | 121 | | if (allLoadedParcelCoords.Count == 0) |
| 1313 | 122 | | return; |
| | 123 | |
|
| 8 | 124 | | blockersToRemove.Clear(); |
| 8 | 125 | | blockersToAdd.Clear(); |
| | 126 | |
|
| 8 | 127 | | var blockers = blockerInstanceHandler.GetBlockers(); |
| | 128 | |
|
| | 129 | | // Detect blockers to be removed |
| 40 | 130 | | foreach (var item in blockers) |
| | 131 | | { |
| 12 | 132 | | if (allLoadedParcelCoords.Contains(item.Key)) |
| | 133 | | { |
| 2 | 134 | | blockersToRemove.Add(item.Key); |
| 2 | 135 | | } |
| | 136 | | else |
| | 137 | | { |
| 10 | 138 | | bool foundAroundLoadedScenes = false; |
| 70 | 139 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 140 | | { |
| 35 | 141 | | Vector2Int offset = aroundOffsets[i]; |
| 35 | 142 | | Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y); |
| | 143 | |
|
| 35 | 144 | | if (allLoadedParcelCoords.Contains(checkedPosition)) |
| | 145 | | { |
| 10 | 146 | | foundAroundLoadedScenes = true; |
| 10 | 147 | | break; |
| | 148 | | } |
| | 149 | | } |
| | 150 | |
|
| 10 | 151 | | if (!foundAroundLoadedScenes) |
| 0 | 152 | | blockersToRemove.Add(item.Key); |
| | 153 | | } |
| | 154 | | } |
| | 155 | |
|
| | 156 | | // Detect missing blockers to be added |
| 8 | 157 | | using (var it = allLoadedParcelCoords.GetEnumerator()) |
| | 158 | | { |
| 29 | 159 | | while (it.MoveNext()) |
| | 160 | | { |
| 21 | 161 | | Vector2Int pos = it.Current; |
| | 162 | |
|
| 378 | 163 | | for (int i = 0; i < aroundOffsets.Length; i++) |
| | 164 | | { |
| 168 | 165 | | Vector2Int offset = aroundOffsets[i]; |
| 168 | 166 | | Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y); |
| | 167 | |
|
| 168 | 168 | | if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition)) |
| | 169 | | { |
| 104 | 170 | | blockersToAdd.Add(checkedPosition); |
| | 171 | | } |
| | 172 | | } |
| | 173 | | } |
| 8 | 174 | | } |
| | 175 | |
|
| | 176 | | // Remove extra blockers |
| 20 | 177 | | foreach (var coords in blockersToRemove) |
| | 178 | | { |
| 2 | 179 | | blockerInstanceHandler.HideBlocker(coords, false); |
| | 180 | | } |
| | 181 | |
|
| | 182 | | // Add missing blockers |
| 172 | 183 | | foreach (var coords in blockersToAdd) |
| | 184 | | { |
| 78 | 185 | | blockerInstanceHandler.ShowBlocker(coords); |
| | 186 | | } |
| 8 | 187 | | } |
| | 188 | | } |
| | 189 | | } |