Update the orientation enforcer to periodically check the orientation in diving minigamw
This commit is contained in:
@@ -3,6 +3,8 @@ using UnityEngine.SceneManagement;
|
||||
using System;
|
||||
using Input;
|
||||
using Settings;
|
||||
using System.Collections;
|
||||
using Minigames.DivingForPictures;
|
||||
|
||||
namespace Utility
|
||||
{
|
||||
@@ -34,10 +36,12 @@ namespace Utility
|
||||
|
||||
public event Action OnOrientationCorrect;
|
||||
|
||||
private GameObject promptInstance;
|
||||
private ScreenOrientationRequirement requiredOrientation;
|
||||
private bool orientationCorrect;
|
||||
private Coroutine orientationCheckCoroutine;
|
||||
private GameObject _promptInstance;
|
||||
private ScreenOrientationRequirement _requiredOrientation;
|
||||
private bool _orientationCorrect;
|
||||
private Coroutine _orientationCheckCoroutine;
|
||||
private bool _isDivingMinigame = false;
|
||||
private Coroutine _continuousOrientationCheckCoroutine;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@@ -57,40 +61,13 @@ namespace Utility
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// Clean up any previous prompt/coroutine
|
||||
// CleanupPromptAndCoroutine();
|
||||
if (IsMainMenuScene(scene))
|
||||
return;
|
||||
requiredOrientation = orientationConfig != null ? orientationConfig.GetRequirementForScene(scene.name) : ScreenOrientationRequirement.Portrait;
|
||||
orientationCorrect = IsOrientationCorrect();
|
||||
if (!orientationCorrect)
|
||||
{
|
||||
InputManager.Instance.SetInputMode(InputMode.UI);
|
||||
ShowPrompt();
|
||||
orientationCheckCoroutine = StartCoroutine(OrientationCheckRoutine());
|
||||
}
|
||||
else
|
||||
{
|
||||
orientationCorrect = true;
|
||||
OnOrientationCorrect?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMainMenuScene(Scene scene)
|
||||
{
|
||||
// Adjust this logic if you have a different main menu scene name
|
||||
return scene.name == "Main Menu" || scene.name == "MainMenu";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the current device orientation matches the required orientation for the scene
|
||||
/// </summary>
|
||||
/// <returns>True if the orientation is correct, false otherwise</returns>
|
||||
public bool IsOrientationCorrect()
|
||||
{
|
||||
switch (requiredOrientation)
|
||||
switch (_requiredOrientation)
|
||||
{
|
||||
case ScreenOrientationRequirement.Portrait:
|
||||
return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown;
|
||||
@@ -101,36 +78,126 @@ namespace Utility
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the current scene is the diving minigame scene
|
||||
/// </summary>
|
||||
/// <returns>True if the scene is "DivingForPictures", false otherwise</returns>
|
||||
public bool IsDivingMinigameScene(Scene scene)
|
||||
{
|
||||
return scene.name == "DivingForPictures";
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// Clean up any previous prompt/coroutine
|
||||
CleanupPromptAndCoroutine();
|
||||
|
||||
if (IsMainMenuScene(scene))
|
||||
return;
|
||||
|
||||
_isDivingMinigame = IsDivingMinigameScene(scene);
|
||||
|
||||
_requiredOrientation = orientationConfig != null ? orientationConfig.GetRequirementForScene(scene.name) : ScreenOrientationRequirement.Portrait;
|
||||
_orientationCorrect = IsOrientationCorrect();
|
||||
|
||||
if (!_orientationCorrect)
|
||||
{
|
||||
InputManager.Instance.SetInputMode(InputMode.UI);
|
||||
ShowPrompt();
|
||||
// If this is the diving minigame, start continuous orientation checking
|
||||
if (_isDivingMinigame)
|
||||
{
|
||||
_continuousOrientationCheckCoroutine = StartCoroutine(ContinuousOrientationCheckRoutine());
|
||||
}
|
||||
else
|
||||
{
|
||||
_orientationCheckCoroutine = StartCoroutine(OrientationCheckRoutine());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_orientationCorrect = true;
|
||||
OnOrientationCorrect?.Invoke();
|
||||
|
||||
// If this is the diving minigame, start continuous orientation checking
|
||||
if (_isDivingMinigame)
|
||||
{
|
||||
_continuousOrientationCheckCoroutine = StartCoroutine(ContinuousOrientationCheckRoutine());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator OrientationCheckRoutine()
|
||||
{
|
||||
while (!IsOrientationCorrect())
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
orientationCorrect = true;
|
||||
_orientationCorrect = true;
|
||||
OnOrientationCorrect?.Invoke();
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator ContinuousOrientationCheckRoutine()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Wait for a short interval before checking again
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
|
||||
// Check if orientation is now incorrect
|
||||
if (!IsOrientationCorrect())
|
||||
{
|
||||
// Pause the game using DivingGameManager
|
||||
if (DivingGameManager.Instance != null)
|
||||
{
|
||||
DivingGameManager.Instance.Pause();
|
||||
}
|
||||
|
||||
// Show the orientation prompt
|
||||
ShowPrompt();
|
||||
|
||||
// Wait until orientation is correct again
|
||||
while (!IsOrientationCorrect())
|
||||
{
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
}
|
||||
|
||||
// Hide the prompt when orientation is correct again
|
||||
if (_promptInstance != null)
|
||||
{
|
||||
Destroy(_promptInstance);
|
||||
_promptInstance = null;
|
||||
}
|
||||
|
||||
// Resume the game using DivingGameManager
|
||||
if (DivingGameManager.Instance != null)
|
||||
{
|
||||
DivingGameManager.Instance.DoResume();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowPrompt()
|
||||
{
|
||||
if (orientationPromptPrefab != null && promptInstance == null)
|
||||
if (orientationPromptPrefab != null && _promptInstance == null)
|
||||
{
|
||||
promptInstance = Instantiate(orientationPromptPrefab);
|
||||
DontDestroyOnLoad(promptInstance);
|
||||
_promptInstance = Instantiate(orientationPromptPrefab);
|
||||
DontDestroyOnLoad(_promptInstance);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleOrientationCorrect()
|
||||
{
|
||||
if (promptInstance != null)
|
||||
if (_promptInstance != null)
|
||||
{
|
||||
Destroy(promptInstance);
|
||||
promptInstance = null;
|
||||
Destroy(_promptInstance);
|
||||
_promptInstance = null;
|
||||
}
|
||||
if (orientationCheckCoroutine != null)
|
||||
if (_orientationCheckCoroutine != null)
|
||||
{
|
||||
StopCoroutine(orientationCheckCoroutine);
|
||||
orientationCheckCoroutine = null;
|
||||
StopCoroutine(_orientationCheckCoroutine);
|
||||
_orientationCheckCoroutine = null;
|
||||
}
|
||||
|
||||
InputManager.Instance.SetInputMode(InputMode.Game);
|
||||
@@ -138,22 +205,36 @@ namespace Utility
|
||||
|
||||
private void CleanupPromptAndCoroutine()
|
||||
{
|
||||
if (promptInstance != null)
|
||||
if (_promptInstance != null)
|
||||
{
|
||||
Destroy(promptInstance);
|
||||
promptInstance = null;
|
||||
Destroy(_promptInstance);
|
||||
_promptInstance = null;
|
||||
}
|
||||
if (orientationCheckCoroutine != null)
|
||||
|
||||
if (_orientationCheckCoroutine != null)
|
||||
{
|
||||
StopCoroutine(orientationCheckCoroutine);
|
||||
orientationCheckCoroutine = null;
|
||||
StopCoroutine(_orientationCheckCoroutine);
|
||||
_orientationCheckCoroutine = null;
|
||||
}
|
||||
|
||||
if (_continuousOrientationCheckCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_continuousOrientationCheckCoroutine);
|
||||
_continuousOrientationCheckCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMainMenuScene(Scene scene)
|
||||
{
|
||||
// Adjust this logic if you have a different main menu scene name
|
||||
return scene.name == "Main Menu" || scene.name == "MainMenu";
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
OnOrientationCorrect -= HandleOrientationCorrect;
|
||||
CleanupPromptAndCoroutine();
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
|
||||
Reference in New Issue
Block a user