| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.NotificationModel; |
| | 5 | | using MainScripts.DCL.Controllers.ShaderPrewarm; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.LoadingScreen |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Controls the state of the loading screen. It's responsibility is to update the view depending on the SceneContro |
| | 14 | | /// Creates and provides the controllers associated to the LoadingScreen: TipsController and PercentageController |
| | 15 | | /// </summary> |
| | 16 | | public class LoadingScreenController : IDisposable |
| | 17 | | { |
| | 18 | | private readonly ILoadingScreenView view; |
| | 19 | | private readonly ISceneController sceneController; |
| | 20 | | private readonly DataStore_Player playerDataStore; |
| | 21 | | private readonly DataStore_Common commonDataStore; |
| | 22 | | private readonly DataStore_LoadingScreen loadingScreenDataStore; |
| | 23 | | private readonly DataStore_Realm realmDataStore; |
| | 24 | | private readonly IWorldState worldState; |
| | 25 | |
|
| | 26 | | private Vector2Int currentDestination; |
| | 27 | | private string currentRealm; |
| | 28 | | private bool currentRealmIsWorld; |
| | 29 | | private readonly LoadingScreenTipsController tipsController; |
| | 30 | | private readonly LoadingScreenPercentageController percentageController; |
| | 31 | | internal readonly LoadingScreenTimeoutController timeoutController; |
| | 32 | | private readonly NotificationsController notificationsController; |
| | 33 | | private bool onSignUpFlow; |
| | 34 | | internal bool showRandomPositionNotification; |
| | 35 | | private bool isFadingOut; |
| | 36 | | private readonly IShaderPrewarm shaderPrewarm; |
| | 37 | | private readonly CancellationTokenSource cancellationTokenSource; |
| | 38 | |
|
| 0 | 39 | | public LoadingScreenController(ILoadingScreenView view, ISceneController sceneController, IWorldState worldState |
| | 40 | | DataStore_Player playerDataStore, DataStore_Common commonDataStore, DataStore_LoadingScreen loadingScreenDat |
| | 41 | | { |
| 0 | 42 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 43 | |
|
| 0 | 44 | | this.shaderPrewarm = shaderPrewarm; |
| 0 | 45 | | this.view = view; |
| 0 | 46 | | this.sceneController = sceneController; |
| 0 | 47 | | this.playerDataStore = playerDataStore; |
| 0 | 48 | | this.commonDataStore = commonDataStore; |
| 0 | 49 | | this.worldState = worldState; |
| 0 | 50 | | this.loadingScreenDataStore = loadingScreenDataStore; |
| 0 | 51 | | this.realmDataStore = realmDataStore; |
| 0 | 52 | | this.notificationsController = notificationsController; |
| | 53 | |
|
| 0 | 54 | | tipsController = new LoadingScreenTipsController(view.GetTipsView()); |
| 0 | 55 | | percentageController = new LoadingScreenPercentageController(sceneController, view.GetPercentageView(), comm |
| 0 | 56 | | timeoutController = new LoadingScreenTimeoutController(view.GetTimeoutView(), worldState, this); |
| | 57 | |
|
| 0 | 58 | | this.playerDataStore.lastTeleportPosition.OnChange += TeleportRequested; |
| 0 | 59 | | this.commonDataStore.isSignUpFlow.OnChange += OnSignupFlow; |
| 0 | 60 | | this.sceneController.OnReadyScene += ReadyScene; |
| 0 | 61 | | view.OnFadeInFinish += FadeInFinished; |
| | 62 | |
|
| | 63 | | // The initial loading has still a destination to set. We are starting the timeout for the |
| | 64 | | // websocket initialization |
| 0 | 65 | | timeoutController.StartTimeout(new Vector2Int(-1, -1)); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Dispose() |
| | 69 | | { |
| 0 | 70 | | view.Dispose(); |
| 0 | 71 | | percentageController.Dispose(); |
| 0 | 72 | | timeoutController.Dispose(); |
| | 73 | |
|
| 0 | 74 | | playerDataStore.lastTeleportPosition.OnChange -= TeleportRequested; |
| 0 | 75 | | commonDataStore.isSignUpFlow.OnChange -= OnSignupFlow; |
| 0 | 76 | | sceneController.OnReadyScene -= ReadyScene; |
| 0 | 77 | | view.OnFadeInFinish -= FadeInFinished; |
| | 78 | |
|
| 0 | 79 | | cancellationTokenSource.Cancel(); |
| 0 | 80 | | cancellationTokenSource.Dispose(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void FadeInFinished(ShowHideAnimator obj) |
| | 84 | | { |
| 0 | 85 | | loadingScreenDataStore.decoupledLoadingHUD.visible.Set(true); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void ReadyScene(int obj) |
| | 89 | | { |
| | 90 | | //We have to check that the latest scene loaded is the one from our current destination |
| 0 | 91 | | if (worldState.GetSceneNumberByCoords(currentDestination).Equals(obj)) |
| 0 | 92 | | HandlePlayerLoading(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | //We have to add one more check not to show the loadingScreen unless the player is loaded |
| | 96 | | private void PlayerLoaded(bool loaded, bool _) |
| | 97 | | { |
| 0 | 98 | | if (loaded) |
| 0 | 99 | | FadeOutView(); |
| | 100 | |
|
| 0 | 101 | | commonDataStore.isPlayerRendererLoaded.OnChange -= PlayerLoaded; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void OnSignupFlow(bool current, bool previous) |
| | 105 | | { |
| 0 | 106 | | onSignUpFlow = current; |
| | 107 | |
|
| 0 | 108 | | if (realmDataStore.playerRealmAboutConfiguration.Get() != null) |
| | 109 | | { |
| 0 | 110 | | currentRealm = realmDataStore.playerRealmAboutConfiguration.Get().RealmName; |
| 0 | 111 | | currentRealmIsWorld = commonDataStore.isWorld.Get(); |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | if (current) |
| 0 | 115 | | FadeOutView(); |
| | 116 | | else |
| 0 | 117 | | view.FadeIn(false, false); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void TeleportRequested(Vector3 current, Vector3 previous) |
| | 121 | | { |
| 0 | 122 | | if (onSignUpFlow) return; |
| | 123 | |
|
| 0 | 124 | | Vector2Int currentDestinationCandidate = Utils.WorldToGridPosition(current); |
| | 125 | |
|
| 0 | 126 | | if (IsNewRealm() || IsNewScene(currentDestinationCandidate)) |
| | 127 | | { |
| 0 | 128 | | currentDestination = currentDestinationCandidate; |
| | 129 | |
|
| | 130 | | //On a teleport, to copy previos behaviour, we disable tips entirely and show the teleporting screen |
| | 131 | | //This is probably going to change with the integration of WORLDS loading screen |
| | 132 | | //Temporarily removing tips until V2 |
| | 133 | | //tipsController.StopTips(); |
| 0 | 134 | | percentageController.StartLoading(currentDestination); |
| 0 | 135 | | timeoutController.StartTimeout(currentDestination); |
| 0 | 136 | | view.FadeIn(false, true); |
| | 137 | | } |
| 0 | 138 | | else if (IsSceneLoaded(currentDestinationCandidate)) |
| 0 | 139 | | HandlePlayerLoading(); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | //The realm gets changed before the scenes starts to unload. So, if we try to teleport to a world scene in which |
| | 143 | | //we wont see the loading screen. Same happens when leaving a world. Thats why we need to keep track of the late |
| | 144 | | private bool IsNewRealm() |
| | 145 | | { |
| | 146 | | //Realm has not been set yet, so we are not in a new realm |
| 0 | 147 | | if (realmDataStore.playerRealmAboutConfiguration.Get() == null) |
| 0 | 148 | | return false; |
| | 149 | |
|
| | 150 | | bool realmChangeRequiresLoadingScreen; |
| | 151 | |
|
| 0 | 152 | | if (commonDataStore.isWorld.Get()) |
| 0 | 153 | | realmChangeRequiresLoadingScreen = string.IsNullOrEmpty(currentRealm) || !currentRealm.Equals(realmDataS |
| | 154 | | else |
| 0 | 155 | | realmChangeRequiresLoadingScreen = currentRealmIsWorld; |
| | 156 | |
|
| 0 | 157 | | currentRealm = realmDataStore.playerRealmAboutConfiguration.Get().RealmName; |
| 0 | 158 | | currentRealmIsWorld = commonDataStore.isWorld.Get(); |
| 0 | 159 | | return realmChangeRequiresLoadingScreen; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | //If the destination scene is not loaded, we show the teleport screen. THis is called in the POSITION_UNSETTLED |
| | 163 | | //On the other hand, the POSITION_SETTLED event is called; but since the scene will already be loaded, the loadi |
| | 164 | | private bool IsNewScene(Vector2Int currentDestinationCandidate) => |
| 0 | 165 | | worldState.GetSceneNumberByCoords(currentDestinationCandidate).Equals(-1); |
| | 166 | |
|
| | 167 | | private bool IsSceneLoaded(Vector2Int candidate) => |
| 0 | 168 | | worldState.GetScene(worldState.GetSceneNumberByCoords(candidate))?.loadingProgress >= 100; |
| | 169 | |
|
| | 170 | | private void HandlePlayerLoading() |
| | 171 | | { |
| | 172 | | //We have to check if the player is loaded |
| 0 | 173 | | if (commonDataStore.isPlayerRendererLoaded.Get()) |
| 0 | 174 | | FadeOutView(); |
| | 175 | | else |
| | 176 | | { |
| 0 | 177 | | if (!isFadingOut) |
| 0 | 178 | | percentageController.SetAvatarLoadingMessage(); |
| | 179 | |
|
| 0 | 180 | | commonDataStore.isPlayerRendererLoaded.OnChange += PlayerLoaded; |
| | 181 | | } |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | private void FadeOutView() |
| | 185 | | { |
| 0 | 186 | | if (isFadingOut) return; |
| 0 | 187 | | isFadingOut = true; |
| 0 | 188 | | FadeOutViewAsync(cancellationTokenSource.Token).Forget(); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | private async UniTask FadeOutViewAsync(CancellationToken cancellationToken) |
| | 192 | | { |
| 0 | 193 | | timeoutController.StopTimeout(); |
| | 194 | |
|
| 0 | 195 | | await shaderPrewarm.PrewarmAsync(OnShaderPrewarmProgress, cancellationToken); |
| | 196 | |
|
| 0 | 197 | | view.FadeOut(); |
| 0 | 198 | | loadingScreenDataStore.decoupledLoadingHUD.visible.Set(false); |
| | 199 | |
|
| 0 | 200 | | if (showRandomPositionNotification) |
| 0 | 201 | | ShowRandomPositionNotification(); |
| | 202 | |
|
| 0 | 203 | | isFadingOut = false; |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | private void OnShaderPrewarmProgress(float progress) |
| | 207 | | { |
| 0 | 208 | | percentageController.SetShaderCompilingMessage(progress); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | private void ShowRandomPositionNotification() |
| | 212 | | { |
| 0 | 213 | | notificationsController.ShowNotification(new Model |
| | 214 | | { |
| | 215 | | message = "There was an error while trying to load your home scene. If the problem persists contact supp |
| | 216 | | type = NotificationModel.Type.ERROR, |
| | 217 | | timer = 10f, |
| | 218 | | destroyOnFinish = true |
| | 219 | | }); |
| | 220 | |
|
| 0 | 221 | | showRandomPositionNotification = false; |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public void RandomPositionRequested() |
| | 225 | | { |
| 0 | 226 | | WebInterface.SendChatMessage(new ChatMessage |
| | 227 | | { |
| | 228 | | messageType = ChatMessage.Type.NONE, |
| | 229 | | recipient = string.Empty, |
| | 230 | | body = "/goto random", |
| | 231 | | }); |
| | 232 | |
|
| 0 | 233 | | showRandomPositionNotification = true; |
| 0 | 234 | | } |
| | 235 | | } |
| | 236 | | } |