| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public interface IBuilderInWorldLoadingView |
| | 8 | | { |
| | 9 | | bool isActive { get; } |
| | 10 | | GameObject gameObject { get; } |
| | 11 | | void Show(); |
| | 12 | | void Hide(bool forceHide = false, Action onHideAction = null); |
| | 13 | | void StartTipsCarousel(); |
| | 14 | | void StopTipsCarousel(); |
| | 15 | | void SetPercentage(float newValue); |
| | 16 | | void Dispose(); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class BuilderInWorldLoadingView : MonoBehaviour, IBuilderInWorldLoadingView |
| | 20 | | { |
| | 21 | | private const string VIEW_PATH = "BuilderInWorldLoadingView"; |
| | 22 | |
|
| | 23 | | [Header("Loading Tips Config")] |
| | 24 | | [SerializeField] internal BuilderInWorldLoadingTip loadingTipPrefab; |
| | 25 | |
|
| | 26 | | [SerializeField] internal RectTransform loadingTipsContainer; |
| | 27 | | [SerializeField] internal ScrollRect loadingTipsScroll; |
| | 28 | | [SerializeField] internal List<BuilderInWorldLoadingTipModel> loadingTips; |
| 17 | 29 | | [SerializeField] internal float timeBetweenTips = 3f; |
| | 30 | | [SerializeField] internal AnimationCurve animationTipsCurve; |
| 17 | 31 | | [SerializeField] internal float animationTipsTransitionTime = 1f; |
| | 32 | |
|
| | 33 | | [Header("Other Config")] |
| 17 | 34 | | [SerializeField] internal float minVisibilityTime = 1.5f; |
| | 35 | |
|
| | 36 | | [SerializeField] internal LoadingBar loadingBar; |
| | 37 | |
|
| 0 | 38 | | public bool isActive => gameObject.activeSelf; |
| | 39 | |
|
| | 40 | | internal Coroutine tipsCoroutine; |
| | 41 | | internal Coroutine hideCoroutine; |
| | 42 | | internal float showTime = 0f; |
| | 43 | | internal int currentTipIndex = 0; |
| | 44 | | internal float currentFinalNormalizedPos; |
| | 45 | |
|
| | 46 | | internal static BuilderInWorldLoadingView Create() |
| | 47 | | { |
| 2 | 48 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<BuilderInWorldLoadingView>(); |
| 2 | 49 | | view.gameObject.name = "_BuilderInWorldLoadingView"; |
| | 50 | |
|
| 2 | 51 | | return view; |
| | 52 | | } |
| | 53 | |
|
| 32 | 54 | | private void Awake() { CreateLoadingTips(); } |
| | 55 | |
|
| | 56 | | private void CreateLoadingTips() |
| | 57 | | { |
| 16 | 58 | | RectTransform tipsContainerRectTransform = loadingTipsContainer; |
| 16 | 59 | | float tipsContainerHorizontalOffset = 0; |
| | 60 | |
|
| 96 | 61 | | for (int i = 0; i < loadingTips.Count; i++) |
| | 62 | | { |
| 32 | 63 | | BuilderInWorldLoadingTip newTip = Instantiate(loadingTipPrefab, loadingTipsContainer); |
| 32 | 64 | | newTip.Configure(loadingTips[i]); |
| | 65 | |
|
| 32 | 66 | | if (i > 0) |
| | 67 | | { |
| 16 | 68 | | tipsContainerHorizontalOffset += ((RectTransform)newTip.transform).rect.size.x; |
| | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 16 | 72 | | tipsContainerRectTransform.offsetMax = new Vector2(tipsContainerHorizontalOffset, tipsContainerRectTransform.off |
| 16 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Show() |
| | 76 | | { |
| 1 | 77 | | if (gameObject != null) |
| 1 | 78 | | gameObject.SetActive(true); |
| | 79 | |
|
| 1 | 80 | | showTime = Time.realtimeSinceStartup; |
| | 81 | |
|
| 1 | 82 | | if (loadingTips.Count > 0) |
| | 83 | | { |
| 1 | 84 | | StartTipsCarousel(); |
| | 85 | | } |
| | 86 | |
|
| 1 | 87 | | AudioScriptableObjects.builderEnter.Play(); |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | public void Hide(bool forceHide = false, Action onHideAction = null) |
| | 91 | | { |
| 1 | 92 | | if (hideCoroutine != null) |
| 0 | 93 | | CoroutineStarter.Stop(hideCoroutine); |
| | 94 | |
|
| 1 | 95 | | hideCoroutine = CoroutineStarter.Start(TryToHideCoroutine(forceHide, onHideAction)); |
| 1 | 96 | | } |
| | 97 | |
|
| | 98 | | public void Dispose() |
| | 99 | | { |
| 16 | 100 | | StopTipsCarousel(); |
| | 101 | |
|
| 16 | 102 | | if (hideCoroutine != null) |
| 0 | 103 | | CoroutineStarter.Stop(hideCoroutine); |
| | 104 | |
|
| 16 | 105 | | if (tipsCoroutine != null) |
| 0 | 106 | | CoroutineStarter.Stop(tipsCoroutine); |
| 16 | 107 | | } |
| | 108 | |
|
| | 109 | | public void StartTipsCarousel() |
| | 110 | | { |
| 1 | 111 | | StopTipsCarousel(); |
| 1 | 112 | | tipsCoroutine = CoroutineStarter.Start(RunTipsCarouselCoroutine()); |
| 1 | 113 | | } |
| | 114 | |
|
| | 115 | | public void StopTipsCarousel() |
| | 116 | | { |
| 18 | 117 | | if (tipsCoroutine == null) |
| 17 | 118 | | return; |
| | 119 | |
|
| 1 | 120 | | CoroutineStarter.Stop(tipsCoroutine); |
| 1 | 121 | | tipsCoroutine = null; |
| | 122 | |
|
| 1 | 123 | | ForceToEndCurrentTipsAnimation(); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | internal IEnumerator TryToHideCoroutine(bool forceHide, Action onHideAction) |
| | 127 | | { |
| 1 | 128 | | while (!forceHide && (Time.realtimeSinceStartup - showTime) < minVisibilityTime) |
| | 129 | | { |
| 0 | 130 | | yield return null; |
| | 131 | | } |
| | 132 | |
|
| 1 | 133 | | StopTipsCarousel(); |
| | 134 | |
|
| 1 | 135 | | if (gameObject != null) |
| 1 | 136 | | gameObject.SetActive(false); |
| | 137 | |
|
| 1 | 138 | | onHideAction?.Invoke(); |
| | 139 | |
|
| 1 | 140 | | AudioScriptableObjects.builderReady.Play(); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | internal IEnumerator RunTipsCarouselCoroutine() |
| | 144 | | { |
| 1 | 145 | | currentTipIndex = 0; |
| | 146 | |
|
| 0 | 147 | | while (true) |
| | 148 | | { |
| 1 | 149 | | yield return new WaitForSeconds(timeBetweenTips); |
| 0 | 150 | | yield return RunTipsAnimationCoroutine(); |
| 0 | 151 | | IncrementTipIndex(); |
| | 152 | | } |
| | 153 | | } |
| | 154 | |
|
| | 155 | | private void IncrementTipIndex() |
| | 156 | | { |
| 1 | 157 | | currentTipIndex++; |
| 1 | 158 | | if (currentTipIndex >= loadingTips.Count - 1) |
| | 159 | | { |
| 1 | 160 | | currentTipIndex = 0; |
| 1 | 161 | | loadingTipsScroll.horizontalNormalizedPosition = 0f; |
| | 162 | |
|
| | 163 | | // Moving the last tip game object to the first position in the hierarchy, we make the carousel cyclical. |
| 1 | 164 | | loadingTipsContainer.GetChild(loadingTipsContainer.childCount - 1).SetAsFirstSibling(); |
| | 165 | | } |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | internal IEnumerator RunTipsAnimationCoroutine() |
| | 169 | | { |
| 0 | 170 | | float currentAnimationTime = 0f; |
| 0 | 171 | | float initialNormalizedPos = loadingTipsScroll.horizontalNormalizedPosition; |
| 0 | 172 | | currentFinalNormalizedPos = initialNormalizedPos + (1f / (loadingTips.Count - 1)); |
| | 173 | |
|
| 0 | 174 | | while (currentAnimationTime <= animationTipsTransitionTime) |
| | 175 | | { |
| 0 | 176 | | loadingTipsScroll.horizontalNormalizedPosition = Mathf.Lerp( |
| | 177 | | initialNormalizedPos, |
| | 178 | | currentFinalNormalizedPos, |
| | 179 | | animationTipsCurve.Evaluate(currentAnimationTime / animationTipsTransitionTime)); |
| | 180 | |
|
| 0 | 181 | | currentAnimationTime += Time.deltaTime; |
| | 182 | |
|
| 0 | 183 | | yield return null; |
| | 184 | | } |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | internal void ForceToEndCurrentTipsAnimation() |
| | 188 | | { |
| 1 | 189 | | loadingTipsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos; |
| 1 | 190 | | IncrementTipIndex(); |
| 1 | 191 | | } |
| | 192 | |
|
| 0 | 193 | | public void SetPercentage(float newValue) { loadingBar.SetPercentage(newValue); } |
| | 194 | | } |