| | 1 | | using System; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using DCL.Rendering; |
| | 7 | | using UnityEngine; |
| | 8 | | using Object = UnityEngine.Object; |
| | 9 | |
|
| | 10 | | namespace DCL.Controllers |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// This class is the loading blockers composite and instancing handler. |
| | 14 | | /// <br/> |
| | 15 | | /// Responsibilities include:<br/> |
| | 16 | | /// - Handling hide/show of blockers through its IBlockerAnimationHandler<br/> |
| | 17 | | /// - Keeping track of all blockers<br/> |
| | 18 | | /// </summary> |
| | 19 | | public class BlockerInstanceHandler : IBlockerInstanceHandler |
| | 20 | | { |
| | 21 | | static GameObject blockerPrefab; |
| | 22 | | private bool blockerPrefabDirty; |
| | 23 | |
|
| | 24 | | const string PARCEL_BLOCKER_POOL_NAME = "ParcelBlocker"; |
| | 25 | |
|
| | 26 | | Vector3 auxPosVec = new Vector3(); |
| | 27 | | Vector3 auxScaleVec = new Vector3(); |
| | 28 | |
|
| 675 | 29 | | Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>(); |
| | 30 | |
|
| | 31 | | private IBlockerAnimationHandler animationHandler; |
| | 32 | | private ICullingController cullingController; |
| | 33 | | private Transform parent; |
| | 34 | |
|
| | 35 | | public void Initialize(IBlockerAnimationHandler animationHandler, ICullingController cullingController) |
| | 36 | | { |
| 0 | 37 | | this.cullingController = cullingController; |
| 0 | 38 | | this.animationHandler = animationHandler; |
| 0 | 39 | | } |
| | 40 | |
|
| 675 | 41 | | public BlockerInstanceHandler() |
| | 42 | | { |
| 675 | 43 | | RenderProfileManifest.i.OnChangeProfile += OnChangeProfile; |
| 675 | 44 | | OnChangeProfile(RenderProfileManifest.i.currentProfile); |
| 675 | 45 | | } |
| | 46 | |
|
| | 47 | | private void OnChangeProfile(RenderProfileWorld profile) |
| | 48 | | { |
| 8591 | 49 | | if (profile == null) |
| 0 | 50 | | return; |
| | 51 | |
|
| 8591 | 52 | | blockerPrefabDirty = true; |
| 8591 | 53 | | blockerPrefab = profile.loadingBlockerPrefab; |
| 8591 | 54 | | } |
| | 55 | |
|
| | 56 | | public void ShowBlocker(Vector2Int pos, bool instant = false) |
| | 57 | | { |
| 62 | 58 | | if (blockerPrefabDirty) |
| | 59 | | { |
| 11 | 60 | | blockerPrefabDirty = false; |
| 11 | 61 | | EnsureBlockerPool(); |
| | 62 | | } |
| | 63 | |
|
| 62 | 64 | | float centerOffset = ParcelSettings.PARCEL_SIZE / 2; |
| 62 | 65 | | PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME); |
| 62 | 66 | | GameObject blockerGo = blockerPoolable.gameObject; |
| 62 | 67 | | BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>(); |
| | 68 | |
|
| 62 | 69 | | Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y)); |
| | 70 | |
|
| 62 | 71 | | auxPosVec.x = blockerPos.x + centerOffset; |
| 62 | 72 | | auxPosVec.z = blockerPos.z + centerOffset; |
| 62 | 73 | | auxPosVec.y = 8; |
| | 74 | |
|
| 62 | 75 | | Transform blockerTransform = blockerGo.transform; |
| 62 | 76 | | blockerTransform.SetParent(parent, false); |
| 62 | 77 | | blockerTransform.position = auxPosVec; |
| 62 | 78 | | blockerTransform.localScale = Vector3.one * 16; |
| | 79 | |
|
| 62 | 80 | | blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y); |
| 62 | 81 | | blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f); |
| | 82 | |
|
| | 83 | | #if UNITY_EDITOR |
| 62 | 84 | | blockerGo.name = "BLOCKER " + pos; |
| | 85 | | #endif |
| | 86 | |
|
| 62 | 87 | | blockers.Add(pos, blockerPoolable); |
| | 88 | |
|
| 62 | 89 | | if (!instant) |
| 55 | 90 | | animationHandler.FadeIn(blockerGo); |
| | 91 | |
|
| 62 | 92 | | cullingController?.MarkDirty(); |
| 24 | 93 | | } |
| | 94 | |
|
| | 95 | | private void EnsureBlockerPool() |
| | 96 | | { |
| | 97 | | // We need to manually create the Pool for empty game objects if it doesn't exist |
| 11 | 98 | | if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME)) |
| | 99 | | { |
| 7 | 100 | | PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME); |
| | 101 | | } |
| | 102 | |
|
| 11 | 103 | | GameObject go = Object.Instantiate(blockerPrefab); |
| 11 | 104 | | Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go); |
| 11 | 105 | | pool.persistent = true; |
| 11 | 106 | | pool.ForcePrewarm(); |
| 11 | 107 | | } |
| | 108 | |
|
| 1358 | 109 | | public void SetParent(Transform parent) { this.parent = parent; } |
| | 110 | |
|
| | 111 | | public void HideBlocker(Vector2Int coords, bool instant = false) |
| | 112 | | { |
| 4 | 113 | | if (instant) |
| | 114 | | { |
| 1 | 115 | | ReleaseBlocker(coords); |
| 1 | 116 | | return; |
| | 117 | | } |
| | 118 | |
|
| 3 | 119 | | animationHandler.FadeOut( |
| | 120 | | blockers[coords].gameObject, |
| 3 | 121 | | () => ReleaseBlocker(coords) |
| | 122 | | ); |
| 3 | 123 | | } |
| | 124 | |
|
| | 125 | | private void ReleaseBlocker(Vector2Int coords) |
| | 126 | | { |
| 51 | 127 | | if (!blockers.ContainsKey(coords)) |
| 0 | 128 | | return; |
| | 129 | |
|
| 51 | 130 | | blockers[coords].Release(); |
| 51 | 131 | | blockers.Remove(coords); |
| 51 | 132 | | } |
| | 133 | |
|
| 16 | 134 | | public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject |
| | 135 | |
|
| | 136 | | public void DestroyAllBlockers() |
| | 137 | | { |
| 713 | 138 | | var keys = blockers.Keys.ToArray(); |
| | 139 | |
|
| 1520 | 140 | | for (var i = 0; i < keys.Length; i++) |
| | 141 | | { |
| 47 | 142 | | ReleaseBlocker(keys[i]); |
| | 143 | | } |
| | 144 | |
|
| 713 | 145 | | blockers.Clear(); |
| 713 | 146 | | } |
| | 147 | | } |
| | 148 | | } |