Skip to content

Commit

Permalink
🔀 Merge develop into master (pull request #7)
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
matheusamazonas authored Sep 18, 2023
2 parents 63a91d1 + b001a2a commit aa16abe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 27 deletions.
6 changes: 6 additions & 0 deletions Assets/Scenes/Startup.unity
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ MonoBehaviour:
_scenePath: Assets/Scenes/Match.unity
_transition: {fileID: 686083401}
_inputManager: {fileID: 1379932018}
_transitionDuration: 0.5
--- !u!4 &437329616
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -353,6 +354,11 @@ PrefabInstance:
propertyPath: m_Name
value: Transition Canvas
objectReference: {fileID: 0}
- target: {fileID: 7593389556534962451, guid: 8c44d27ccf79c4f37a77e8dc1c3e8e2e,
type: 3}
propertyPath: m_Enabled
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7593389556534962460, guid: 8c44d27ccf79c4f37a77e8dc1c3e8e2e,
type: 3}
propertyPath: m_Pivot.x
Expand Down
82 changes: 63 additions & 19 deletions Assets/Scripts/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ private enum GamePart
[SerializeField] private SceneReference _matchScene;
[SerializeField] private CanvasFader _transition;
[SerializeField] private InputManager _inputManager;
/// <summary>
/// The duration of &lt;see cref="UI.Screen"/&gt; transitions in the UI.
/// </summary>
[SerializeField, Range(0.1f, 5f)] private float _transitionDuration;

#endregion

Expand All @@ -47,11 +51,6 @@ private enum GamePart
private MenuManager _menuManager;
private MatchManager _matchManager;

/// <summary>
/// The duration of <see cref="UI.Screen"/> transitions in the UI.
/// </summary>
private const float TransitionDuration = 1f;

/// <summary>
/// The currently loaded scene.
/// </summary>
Expand Down Expand Up @@ -82,7 +81,8 @@ private async void Start()
{
try
{
await LoadMenuAsync();
Input.backButtonLeavesApp = true;
await LoadMenuAsync(false);
_inputManager.OnReturn += HandleReturn;
}
catch (OperationCanceledException)
Expand All @@ -96,6 +96,11 @@ private void OnDestroy()
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_inputManager.OnReturn -= HandleReturn;
if (_menuManager)
{
_menuManager.OnReturnToMainMenu -= HandleReturnToMainMenu;
_menuManager.OnEnterMenu -= HandleEnterSubmenu;
}
}

#endregion
Expand Down Expand Up @@ -125,9 +130,12 @@ private async void HandleReturn()
await _menuManager.ReturnAsync(token);
break;
case GamePart.Match:
var matchEnd = _matchManager.StopMatchAsync(TransitionDuration * 0.9f, token);
var loadMenu = LoadMenuAsync();
var matchEnd = _matchManager.StopMatchAsync(_transitionDuration * 0.9f, token);
var loadMenu = LoadMenuAsync(true);
await UniTask.WhenAll(matchEnd, loadMenu);
// Wait for the loading to set this to true, otherwise the event system might pick up the
// back button press right away (within the same frame), effectively quitting the application.
Input.backButtonLeavesApp = true;
break;
default:
throw new NotImplementedException($"Game part not implemented: {_part}");
Expand Down Expand Up @@ -175,19 +183,42 @@ private async void HandleRestartMatch()
}
}

/// <summary>
/// Handles the event of entering s submenu.
/// </summary>
private void HandleEnterSubmenu()
{
Input.backButtonLeavesApp = false;
}

/// <summary>
/// Handles the event of going back to the main menu, from a submenu.
/// </summary>
private void HandleReturnToMainMenu()
{
Input.backButtonLeavesApp = true;
}

#endregion

#region Private

/// <summary>
/// <summary>
/// Loads the menu scene.
/// </summary>
/// <param name="transition">Whether the transition animation should be played when loading the menu. </param>
/// <returns>A task to be awaited which represents the loading.</returns>
private async UniTask LoadMenuAsync()
private async UniTask LoadMenuAsync(bool transition)
{
if (transition)
await StartTransitionAsync();
_menuManager = await LoadManagedSceneAsync<MenuManager>(_menuScene);
_menuManager.OnStartMatch += HandleStartMatch;
_menuManager.OnReturnToMainMenu += HandleReturnToMainMenu;
_menuManager.OnEnterMenu += HandleEnterSubmenu;
_part = GamePart.Menu;
if (transition)
await EndTransitionAsync();
}

/// <summary>
Expand All @@ -196,26 +227,43 @@ private async UniTask LoadMenuAsync()
/// <param name="settings">The settings of the match to be started.</param>
private async UniTask StartMatch(MatchSettings settings)
{
await StartTransitionAsync();
_matchManager = await LoadManagedSceneAsync<MatchManager>(_matchScene);
_matchManager.OnLeaveRequest += HandleReturn;
_matchManager.OnRestartRequest += HandleRestartMatch;
_part = GamePart.Match;
await EndTransitionAsync();
await _matchManager.StartMatchAsync(settings, _cancellationTokenSource.Token);
}

/// <summary>
/// Starts a scene transition.
/// </summary>
private async UniTask StartTransitionAsync()
{
_loading = true;
await _transition.FadeInAsync(_transitionDuration, _cancellationTokenSource.Token);
}

/// <summary>
/// End a scene transition.
/// </summary>
private async UniTask EndTransitionAsync()
{
await _transition.FadeOutAsync(_transitionDuration, _cancellationTokenSource.Token);
_loading = false;
}

/// <summary>
/// Loads a scene that contains a manager asynchronously.
/// </summary>
/// <param name="scene">The scene to be loaded.</param>
/// <typeparam name="T">The type of the manager to be fetched in the scene.</typeparam>
/// <returns>A task to be awaited which represents the loading. Its value is the scene's manager. </returns>
/// <exception cref="Exception">Thrown if the given <paramref name="scene"/> does not contain a manager of type
/// <exception cref="Exception">Thrown if the given <paramref name="scene"/> wasn't loaded.
/// <typeparamref name="T"/>The type of the manager in the scene.</exception>
private async UniTask<T> LoadManagedSceneAsync<T>(SceneReference scene) where T : MonoBehaviour
{
_loading = true;
var token = _cancellationTokenSource.Token;
await _transition.FadeInAsync(TransitionDuration, token);
if (_scene != null) // In some cases (e.g. leading menu), there is nothing to unload.
await SceneManager.UnloadSceneAsync(_scene.Value);
await SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);
Expand All @@ -224,11 +272,7 @@ private async UniTask<T> LoadManagedSceneAsync<T>(SceneReference scene) where T
throw new Exception($"Managed scene wasn't loaded ({typeof(T)}).");

SceneManager.SetActiveScene(_scene.Value);
var manager = FindAnyObjectByType<T>();
await _transition.FadeOutAsync(TransitionDuration, token);
_loading = false;

return manager;
return FindAnyObjectByType<T>();
}

#endregion
Expand Down
25 changes: 19 additions & 6 deletions Assets/Scripts/Menu/MenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ internal class MenuManager : MonoBehaviour
/// </summary>
internal event Action<MatchSettings> OnStartMatch;

/// <summary>
/// Invoked whenever the application leaves a submenu and goes back to the main menu.
/// </summary>
internal event Action OnEnterMenu;

/// <summary>
/// Invoked whenever the application goes to a submenu.
/// </summary>
internal event Action OnReturnToMainMenu;

#endregion

#region Serialized fields
Expand All @@ -38,6 +48,7 @@ internal class MenuManager : MonoBehaviour
#endregion

private readonly CancellationTokenSource _cancellationTokenSource = new();

#region Fields

private Screen _currentScreen;
Expand Down Expand Up @@ -78,6 +89,7 @@ private void OnDestroy()

private async void HandleSelectNewMatch()
{
OnEnterMenu?.Invoke();
try
{
await TransitionToAsync(_playScreen);
Expand All @@ -90,6 +102,7 @@ private async void HandleSelectNewMatch()

private async void HandleSelectSettings()
{
OnEnterMenu?.Invoke();
try
{
await TransitionToAsync(_settingsScreen);
Expand All @@ -102,6 +115,7 @@ private async void HandleSelectSettings()

private async void HandleSelectCredits()
{
OnEnterMenu?.Invoke();
try
{
await TransitionToAsync(_creditsScreen);
Expand All @@ -110,7 +124,6 @@ private async void HandleSelectCredits()
{
Debug.Log("Credits selection handling stopped because the operation was cancelled.");
}

}

private void StartMatch(MatchSettings settings)
Expand Down Expand Up @@ -147,14 +160,14 @@ internal async UniTask ReturnAsync(CancellationToken token)

private async UniTask ReturnMenuAsync(CancellationToken token)
{
if (_currentScreen == null)
return;

await _transition.FadeInAsync(_transitionDuration / 2f, token);

if (_currentScreen != null)
_currentScreen.Hide();

_currentScreen.Hide();
_currentScreen = null;

await _transition.FadeOutAsync(_transitionDuration / 2f, token);
OnReturnToMainMenu?.Invoke();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ PlayerSettings:
vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1
loadStoreDebugModeEnabled: 0
bundleVersion: 1.1.0
bundleVersion: 1.1.1
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down Expand Up @@ -163,7 +163,7 @@ PlayerSettings:
iPhone: 0
tvOS: 0
overrideDefaultApplicationIdentifier: 1
AndroidBundleVersionCode: 8
AndroidBundleVersionCode: 9
AndroidMinSdkVersion: 22
AndroidTargetSdkVersion: 33
AndroidPreferredInstallLocation: 1
Expand Down

0 comments on commit aa16abe

Please sign in to comment.