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