| | 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 | |
|
| | 23 | | const string PARCEL_BLOCKER_POOL_NAME = "ParcelBlocker"; |
| | 24 | |
|
| | 25 | | Vector3 auxPosVec = new Vector3(); |
| | 26 | | Vector3 auxScaleVec = new Vector3(); |
| | 27 | |
|
| 638 | 28 | | Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>(); |
| | 29 | |
|
| | 30 | | private IBlockerAnimationHandler animationHandler; |
| | 31 | | private Transform parent; |
| | 32 | |
|
| 638 | 33 | | public BlockerInstanceHandler(IBlockerAnimationHandler animationHandler) |
| | 34 | | { |
| 638 | 35 | | this.animationHandler = animationHandler; |
| 638 | 36 | | blockerPrefab = Resources.Load<GameObject>("LoadingBlocker_Green"); |
| 638 | 37 | | EnsureBlockerPool(); |
| | 38 | | //TODO We lost the purple blockers when implementing the procedural skybox |
| | 39 | | // We could set a color in their shader dynamically, |
| | 40 | | // similar to how we set toon shader material in the Editor to disable the skybox |
| 638 | 41 | | } |
| | 42 | |
|
| | 43 | | public void ShowBlocker(Vector2Int pos, bool instant = false, bool colliderEnabled = true) |
| | 44 | | { |
| 54 | 45 | | float centerOffset = ParcelSettings.PARCEL_SIZE / 2; |
| 54 | 46 | | PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME); |
| 54 | 47 | | GameObject blockerGo = blockerPoolable.gameObject; |
| 54 | 48 | | BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>(); |
| | 49 | |
|
| 54 | 50 | | Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y)); |
| | 51 | |
|
| 54 | 52 | | auxPosVec.x = blockerPos.x + centerOffset; |
| 54 | 53 | | auxPosVec.z = blockerPos.z + centerOffset; |
| 54 | 54 | | auxPosVec.y = 8; |
| | 55 | |
|
| 54 | 56 | | Transform blockerTransform = blockerGo.transform; |
| 54 | 57 | | blockerTransform.SetParent(parent, false); |
| 54 | 58 | | blockerTransform.position = auxPosVec; |
| 54 | 59 | | blockerTransform.localScale = Vector3.one * 16; |
| | 60 | |
|
| 54 | 61 | | blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y); |
| 54 | 62 | | blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f); |
| 54 | 63 | | blockerCollider.enabled = colliderEnabled; |
| | 64 | |
|
| | 65 | | #if UNITY_EDITOR |
| 54 | 66 | | blockerGo.name = "BLOCKER " + pos; |
| | 67 | | #endif |
| | 68 | |
|
| 54 | 69 | | blockers.Add(pos, blockerPoolable); |
| | 70 | |
|
| 54 | 71 | | if (!instant) |
| 47 | 72 | | animationHandler.FadeIn(blockerGo); |
| | 73 | |
|
| 54 | 74 | | DCL.Environment.i.platform.cullingController?.MarkDirty(); |
| 16 | 75 | | } |
| | 76 | |
|
| | 77 | | private void EnsureBlockerPool() |
| | 78 | | { |
| | 79 | | // We need to manually create the Pool for empty game objects if it doesn't exist |
| 638 | 80 | | if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME)) |
| | 81 | | { |
| 57 | 82 | | PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME); |
| | 83 | | } |
| | 84 | |
|
| 638 | 85 | | GameObject go = Object.Instantiate(blockerPrefab); |
| 638 | 86 | | Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go); |
| 638 | 87 | | pool.persistent = true; |
| 638 | 88 | | pool.ForcePrewarm(); |
| 638 | 89 | | } |
| | 90 | |
|
| 1278 | 91 | | public void SetParent(Transform parent) { this.parent = parent; } |
| | 92 | |
|
| | 93 | | public void HideBlocker(Vector2Int coords, bool instant = false) |
| | 94 | | { |
| 4 | 95 | | if (instant) |
| | 96 | | { |
| 1 | 97 | | ReleaseBlocker(coords); |
| 1 | 98 | | return; |
| | 99 | | } |
| | 100 | |
|
| 3 | 101 | | animationHandler.FadeOut( |
| | 102 | | blockers[coords].gameObject, |
| 3 | 103 | | () => ReleaseBlocker(coords) |
| | 104 | | ); |
| 3 | 105 | | } |
| | 106 | |
|
| | 107 | | private void ReleaseBlocker(Vector2Int coords) |
| | 108 | | { |
| 51 | 109 | | if (!blockers.ContainsKey(coords)) |
| 0 | 110 | | return; |
| | 111 | |
|
| 51 | 112 | | blockers[coords].Release(); |
| 51 | 113 | | blockers.Remove(coords); |
| 51 | 114 | | } |
| | 115 | |
|
| 15 | 116 | | public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject |
| | 117 | |
|
| | 118 | | public void SetCollision(bool newState) |
| | 119 | | { |
| 8204 | 120 | | foreach (var keyValuePair in blockers) |
| | 121 | | { |
| 0 | 122 | | keyValuePair.Value.gameObject.GetComponent<Collider>().enabled = newState; |
| | 123 | | } |
| 4102 | 124 | | } |
| | 125 | |
|
| | 126 | | public void DestroyAllBlockers() |
| | 127 | | { |
| 638 | 128 | | var keys = blockers.Keys.ToArray(); |
| | 129 | |
|
| 1370 | 130 | | for (var i = 0; i < keys.Length; i++) |
| | 131 | | { |
| 47 | 132 | | ReleaseBlocker(keys[i]); |
| | 133 | | } |
| | 134 | |
|
| 638 | 135 | | blockers.Clear(); |
| 638 | 136 | | } |
| | 137 | | } |
| | 138 | | } |