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