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 System;
|
||||||
using Input;
|
using Input;
|
||||||
using Settings;
|
using Settings;
|
||||||
|
using System.Collections;
|
||||||
|
using Minigames.DivingForPictures;
|
||||||
|
|
||||||
namespace Utility
|
namespace Utility
|
||||||
{
|
{
|
||||||
@@ -34,10 +36,12 @@ namespace Utility
|
|||||||
|
|
||||||
public event Action OnOrientationCorrect;
|
public event Action OnOrientationCorrect;
|
||||||
|
|
||||||
private GameObject promptInstance;
|
private GameObject _promptInstance;
|
||||||
private ScreenOrientationRequirement requiredOrientation;
|
private ScreenOrientationRequirement _requiredOrientation;
|
||||||
private bool orientationCorrect;
|
private bool _orientationCorrect;
|
||||||
private Coroutine orientationCheckCoroutine;
|
private Coroutine _orientationCheckCoroutine;
|
||||||
|
private bool _isDivingMinigame = false;
|
||||||
|
private Coroutine _continuousOrientationCheckCoroutine;
|
||||||
|
|
||||||
void Awake()
|
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>
|
/// <summary>
|
||||||
/// Checks if the current device orientation matches the required orientation for the scene
|
/// Checks if the current device orientation matches the required orientation for the scene
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>True if the orientation is correct, false otherwise</returns>
|
/// <returns>True if the orientation is correct, false otherwise</returns>
|
||||||
public bool IsOrientationCorrect()
|
public bool IsOrientationCorrect()
|
||||||
{
|
{
|
||||||
switch (requiredOrientation)
|
switch (_requiredOrientation)
|
||||||
{
|
{
|
||||||
case ScreenOrientationRequirement.Portrait:
|
case ScreenOrientationRequirement.Portrait:
|
||||||
return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown;
|
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()
|
private System.Collections.IEnumerator OrientationCheckRoutine()
|
||||||
{
|
{
|
||||||
while (!IsOrientationCorrect())
|
while (!IsOrientationCorrect())
|
||||||
{
|
{
|
||||||
yield return new WaitForSeconds(0.5f);
|
yield return new WaitForSeconds(0.5f);
|
||||||
}
|
}
|
||||||
orientationCorrect = true;
|
_orientationCorrect = true;
|
||||||
OnOrientationCorrect?.Invoke();
|
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()
|
private void ShowPrompt()
|
||||||
{
|
{
|
||||||
if (orientationPromptPrefab != null && promptInstance == null)
|
if (orientationPromptPrefab != null && _promptInstance == null)
|
||||||
{
|
{
|
||||||
promptInstance = Instantiate(orientationPromptPrefab);
|
_promptInstance = Instantiate(orientationPromptPrefab);
|
||||||
DontDestroyOnLoad(promptInstance);
|
DontDestroyOnLoad(_promptInstance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleOrientationCorrect()
|
private void HandleOrientationCorrect()
|
||||||
{
|
{
|
||||||
if (promptInstance != null)
|
if (_promptInstance != null)
|
||||||
{
|
{
|
||||||
Destroy(promptInstance);
|
Destroy(_promptInstance);
|
||||||
promptInstance = null;
|
_promptInstance = null;
|
||||||
}
|
}
|
||||||
if (orientationCheckCoroutine != null)
|
if (_orientationCheckCoroutine != null)
|
||||||
{
|
{
|
||||||
StopCoroutine(orientationCheckCoroutine);
|
StopCoroutine(_orientationCheckCoroutine);
|
||||||
orientationCheckCoroutine = null;
|
_orientationCheckCoroutine = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
InputManager.Instance.SetInputMode(InputMode.Game);
|
InputManager.Instance.SetInputMode(InputMode.Game);
|
||||||
@@ -138,22 +205,36 @@ namespace Utility
|
|||||||
|
|
||||||
private void CleanupPromptAndCoroutine()
|
private void CleanupPromptAndCoroutine()
|
||||||
{
|
{
|
||||||
if (promptInstance != null)
|
if (_promptInstance != null)
|
||||||
{
|
{
|
||||||
Destroy(promptInstance);
|
Destroy(_promptInstance);
|
||||||
promptInstance = null;
|
_promptInstance = null;
|
||||||
}
|
}
|
||||||
if (orientationCheckCoroutine != null)
|
|
||||||
|
if (_orientationCheckCoroutine != null)
|
||||||
{
|
{
|
||||||
StopCoroutine(orientationCheckCoroutine);
|
StopCoroutine(_orientationCheckCoroutine);
|
||||||
orientationCheckCoroutine = null;
|
_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()
|
void OnDestroy()
|
||||||
{
|
{
|
||||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||||
OnOrientationCorrect -= HandleOrientationCorrect;
|
OnOrientationCorrect -= HandleOrientationCorrect;
|
||||||
|
CleanupPromptAndCoroutine();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnApplicationQuit()
|
void OnApplicationQuit()
|
||||||
|
|||||||
Reference in New Issue
Block a user