| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.NotificationModel; |
| | 5 | | using Type = DCL.NotificationModel.Type; |
| | 6 | |
|
| | 7 | | namespace DCL.LoadingScreen |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Controls the state of the loading screen. It's responsibility is to update the view depending on the SceneContro |
| | 11 | | /// Creates and provides the controllers associated to the LoadingScreen: TipsController and PercentageController |
| | 12 | | /// </summary> |
| | 13 | | public class LoadingScreenController : IDisposable |
| | 14 | | { |
| | 15 | | private readonly ILoadingScreenView view; |
| | 16 | | private readonly ISceneController sceneController; |
| | 17 | | private readonly DataStore_Player playerDataStore; |
| | 18 | | private readonly DataStore_Common commonDataStore; |
| | 19 | | private readonly DataStore_LoadingScreen loadingScreenDataStore; |
| | 20 | | private readonly DataStore_Realm realmDataStore; |
| | 21 | | private readonly IWorldState worldState; |
| | 22 | | private readonly NotificationsController notificationsController; |
| | 23 | |
|
| | 24 | | private Vector2Int currentDestination; |
| | 25 | | private string currentRealm; |
| | 26 | | private bool currentRealmIsWorld; |
| | 27 | | private readonly LoadingScreenTipsController tipsController; |
| | 28 | | private readonly LoadingScreenPercentageController percentageController; |
| | 29 | |
|
| 0 | 30 | | public LoadingScreenController(ILoadingScreenView view, ISceneController sceneController, IWorldState worldState |
| | 31 | | DataStore_Player playerDataStore, DataStore_Common commonDataStore, DataStore_LoadingScreen loadingScreenDat |
| | 32 | | { |
| 0 | 33 | | this.view = view; |
| 0 | 34 | | this.sceneController = sceneController; |
| 0 | 35 | | this.playerDataStore = playerDataStore; |
| 0 | 36 | | this.commonDataStore = commonDataStore; |
| 0 | 37 | | this.worldState = worldState; |
| 0 | 38 | | this.loadingScreenDataStore = loadingScreenDataStore; |
| 0 | 39 | | this.realmDataStore = realmDataStore; |
| 0 | 40 | | this.notificationsController = notificationsController; |
| | 41 | |
|
| 0 | 42 | | tipsController = new LoadingScreenTipsController(view.GetTipsView()); |
| 0 | 43 | | percentageController = new LoadingScreenPercentageController(sceneController, view.GetPercentageView(), comm |
| | 44 | |
|
| 0 | 45 | | this.playerDataStore.lastTeleportPosition.OnChange += TeleportRequested; |
| 0 | 46 | | this.commonDataStore.isSignUpFlow.OnChange += OnSignupFlow; |
| 0 | 47 | | this.sceneController.OnReadyScene += ReadyScene; |
| 0 | 48 | | view.OnFadeInFinish += FadeInFinished; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Dispose() |
| | 52 | | { |
| 0 | 53 | | view.Dispose(); |
| 0 | 54 | | percentageController.Dispose(); |
| 0 | 55 | | playerDataStore.lastTeleportPosition.OnChange -= TeleportRequested; |
| 0 | 56 | | commonDataStore.isSignUpFlow.OnChange -= OnSignupFlow; |
| 0 | 57 | | sceneController.OnReadyScene -= ReadyScene; |
| 0 | 58 | | view.OnFadeInFinish -= FadeInFinished; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void FadeInFinished(ShowHideAnimator obj) |
| | 62 | | { |
| 0 | 63 | | loadingScreenDataStore.decoupledLoadingHUD.visible.Set(true); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void ReadyScene(int obj) |
| | 67 | | { |
| | 68 | | //We have to check that the latest scene loaded is the one from our current destination |
| 0 | 69 | | if (worldState.GetSceneNumberByCoords(currentDestination).Equals(obj)) |
| | 70 | | { |
| | 71 | | //We have to check if the player is loaded |
| 0 | 72 | | if(commonDataStore.isPlayerRendererLoaded.Get()) |
| 0 | 73 | | FadeOutView(); |
| | 74 | | else |
| | 75 | | { |
| 0 | 76 | | percentageController.SetAvatarLoadingMessage(); |
| 0 | 77 | | commonDataStore.isPlayerRendererLoaded.OnChange += PlayerLoaded; |
| | 78 | | } |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | //We have to add one more check not to show the loadingScreen unless the player is loaded |
| | 83 | | private void PlayerLoaded(bool loaded, bool _) |
| | 84 | | { |
| 0 | 85 | | if(loaded) |
| 0 | 86 | | FadeOutView(); |
| | 87 | |
|
| 0 | 88 | | commonDataStore.isPlayerRendererLoaded.OnChange -= PlayerLoaded; |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnSignupFlow(bool current, bool previous) |
| | 92 | | { |
| 0 | 93 | | if (current) |
| 0 | 94 | | FadeOutView(); |
| | 95 | | else |
| 0 | 96 | | view.FadeIn(false, false); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private void TeleportRequested(Vector3 current, Vector3 previous) |
| | 100 | | { |
| 0 | 101 | | Vector2Int currentDestinationCandidate = Utils.WorldToGridPosition(current); |
| | 102 | |
|
| 0 | 103 | | if (IsNewRealm() || IsSceneLoaded(currentDestinationCandidate)) |
| | 104 | | { |
| 0 | 105 | | currentDestination = currentDestinationCandidate; |
| | 106 | |
|
| | 107 | | //On a teleport, to copy previos behaviour, we disable tips entirely and show the teleporting screen |
| | 108 | | //This is probably going to change with the integration of WORLDS loading screen |
| | 109 | | //Temporarily removing tips until V2 |
| | 110 | | //tipsController.StopTips(); |
| 0 | 111 | | percentageController.StartLoading(currentDestination); |
| | 112 | |
|
| 0 | 113 | | view.FadeIn(false, true); |
| | 114 | | } |
| | 115 | | else |
| | 116 | | { |
| | 117 | | //We are going to check if the scene has timeout using the POSITION_SETTLED event. |
| 0 | 118 | | CheckSceneTimeout(currentDestinationCandidate); |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | //The realm gets changed before the scenes starts to unload. So, if we try to teleport to a world scene in which |
| | 123 | | //we wont see the loading screen. Same happens when leaving a world. Thats why we need to keep track of the late |
| | 124 | | private bool IsNewRealm() |
| | 125 | | { |
| | 126 | | bool realmChangeRequiresLoadingScreen; |
| | 127 | |
|
| 0 | 128 | | if (commonDataStore.isWorld.Get()) |
| 0 | 129 | | realmChangeRequiresLoadingScreen = string.IsNullOrEmpty(currentRealm) || !currentRealm.Equals(realmDataS |
| | 130 | | else |
| 0 | 131 | | realmChangeRequiresLoadingScreen = currentRealmIsWorld; |
| | 132 | |
|
| 0 | 133 | | currentRealm = realmDataStore.playerRealmAboutConfiguration.Get().RealmName; |
| 0 | 134 | | currentRealmIsWorld = commonDataStore.isWorld.Get(); |
| 0 | 135 | | return realmChangeRequiresLoadingScreen; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | //If the destination scene is not loaded, we show the teleport screen. THis is called in the POSITION_UNSETTLED |
| | 139 | | //On the other hand, the POSITION_SETTLED event is called; but since the scene will already be loaded, the loadi |
| | 140 | | private bool IsSceneLoaded(Vector2Int currentDestinationCandidate) => |
| 0 | 141 | | worldState.GetSceneNumberByCoords(currentDestinationCandidate).Equals(-1); |
| | 142 | |
|
| | 143 | | private void CheckSceneTimeout(Vector2Int currentDestinationCandidate) |
| | 144 | | { |
| | 145 | | //If we are settling on the destination position, but loading is not complete, this means that kernel is cal |
| | 146 | | //For now, we hide the loading screen and add a notification |
| 0 | 147 | | if (currentDestinationCandidate.Equals(currentDestination) && |
| | 148 | | worldState.GetScene(worldState.GetSceneNumberByCoords(currentDestination))?.loadingProgress < 100) |
| | 149 | | { |
| 0 | 150 | | notificationsController.ShowNotification(new Model |
| | 151 | | { |
| | 152 | | message = "Loading scene timeout", |
| | 153 | | type = Type.GENERIC, |
| | 154 | | timer = 10f, |
| | 155 | | destroyOnFinish = true |
| | 156 | | }); |
| 0 | 157 | | FadeOutView(); |
| | 158 | | } |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | private void FadeOutView() |
| | 162 | | { |
| 0 | 163 | | view.FadeOut(); |
| 0 | 164 | | loadingScreenDataStore.decoupledLoadingHUD.visible.Set(false); |
| 0 | 165 | | } |
| | 166 | | } |
| | 167 | | } |