From f1f4234c2ad5e332acaf23b4498ed07a53a8669a Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Tue, 30 Sep 2025 13:13:37 +0200 Subject: [PATCH] Wait for correct screen orientation --- Assets/Prefabs/UI/PauseMenu.prefab | 4 +- .../DivingForPictures/DivingGameManager.cs | 56 +++++++++++++++++-- .../Obstacles/ObstacleSpawner.cs | 29 ++++++++++ .../Player/PlayerController.cs | 49 +++++++++++++++- .../Tiles/TrenchTileSpawner.cs | 31 ++++++++++ Assets/Scripts/UI/PauseMenu.cs | 9 +-- .../Utility/SceneOrientationEnforcer.cs | 10 +++- 7 files changed, 175 insertions(+), 13 deletions(-) diff --git a/Assets/Prefabs/UI/PauseMenu.prefab b/Assets/Prefabs/UI/PauseMenu.prefab index 3e265793..159ae74a 100644 --- a/Assets/Prefabs/UI/PauseMenu.prefab +++ b/Assets/Prefabs/UI/PauseMenu.prefab @@ -1732,14 +1732,14 @@ MonoBehaviour: - m_Target: {fileID: 7928661763020144797} m_TargetAssemblyTypeName: UI.PauseMenu, AppleHillsScripts m_MethodName: HidePauseMenu - m_Mode: 1 + m_Mode: 6 m_Arguments: m_ObjectArgument: {fileID: 0} m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_IntArgument: 0 m_FloatArgument: 0 m_StringArgument: - m_BoolArgument: 0 + m_BoolArgument: 1 m_CallState: 2 --- !u!1 &5544888797992325527 GameObject: diff --git a/Assets/Scripts/Minigames/DivingForPictures/DivingGameManager.cs b/Assets/Scripts/Minigames/DivingForPictures/DivingGameManager.cs index 9e7dce64..fcddf796 100644 --- a/Assets/Scripts/Minigames/DivingForPictures/DivingGameManager.cs +++ b/Assets/Scripts/Minigames/DivingForPictures/DivingGameManager.cs @@ -5,6 +5,7 @@ using System.Collections; using UnityEngine.Events; using UnityEngine.Playables; using AppleHills.Core.Settings; +using Utility; namespace Minigames.DivingForPictures { @@ -56,9 +57,15 @@ namespace Minigames.DivingForPictures private bool isGameOver = false; private bool _isSurfacing = false; + // Initialization state + private bool _isGameInitialized = false; + // Used to track if we're currently surfacing public bool IsSurfacing => _isSurfacing; + // Event for game components to subscribe to + public event Action OnGameInitialized; + private void Awake() { // Get settings from GameManager @@ -74,6 +81,42 @@ namespace Minigames.DivingForPictures private void Start() { + // Subscribe to SceneOrientationEnforcer's event + if (SceneOrientationEnforcer.Instance != null) + { + SceneOrientationEnforcer.Instance.OnOrientationCorrect += InitializeGame; + + // If orientation is already correct, initialize right away + // This prevents issues if the orientation was already correct before subscription + if (SceneOrientationEnforcer.Instance.IsOrientationCorrect()) + { + InitializeGame(); + } + } + else + { + Debug.LogWarning("[DivingGameManager] SceneOrientationEnforcer not found. Initializing game immediately."); + InitializeGame(); + } + + // Subscribe to player damage events (this doesn't depend on initialization) + PlayerCollisionBehavior.OnDamageTaken += OnPlayerDamageTaken; + + // Validate rope references (this doesn't depend on initialization) + ValidateRopeReferences(); + } + + /// + /// Initializes the game components once the orientation is correct. + /// This is called by SceneOrientationEnforcer when the device is properly oriented. + /// + public void InitializeGame() + { + // Prevent double initialization + if (_isGameInitialized) return; + + Debug.Log("[DivingGameManager] Initializing game"); + // Subscribe to tile spawned event TrenchTileSpawner tileSpawner = FindFirstObjectByType(); if (tileSpawner != null) @@ -85,17 +128,22 @@ namespace Minigames.DivingForPictures Debug.LogWarning("No TrenchTileSpawner found in scene. Monster spawning won't work."); } - // Subscribe to player damage events - PlayerCollisionBehavior.OnDamageTaken += OnPlayerDamageTaken; + // Mark as initialized + _isGameInitialized = true; - // Validate rope references - ValidateRopeReferences(); + // Notify all listeners that the game is initialized + OnGameInitialized?.Invoke(); } private void OnDestroy() { // Unsubscribe from events when the manager is destroyed PlayerCollisionBehavior.OnDamageTaken -= OnPlayerDamageTaken; + + if (SceneOrientationEnforcer.Instance != null) + { + SceneOrientationEnforcer.Instance.OnOrientationCorrect -= InitializeGame; + } } private void Update() diff --git a/Assets/Scripts/Minigames/DivingForPictures/Obstacles/ObstacleSpawner.cs b/Assets/Scripts/Minigames/DivingForPictures/Obstacles/ObstacleSpawner.cs index 311c2e94..d30c766f 100644 --- a/Assets/Scripts/Minigames/DivingForPictures/Obstacles/ObstacleSpawner.cs +++ b/Assets/Scripts/Minigames/DivingForPictures/Obstacles/ObstacleSpawner.cs @@ -74,9 +74,38 @@ namespace Minigames.DivingForPictures } private void Start() + { + + + // Find DivingGameManager and subscribe to its initialization event + DivingGameManager gameManager = FindFirstObjectByType(); + if (gameManager != null) + { + gameManager.OnGameInitialized += Initialize; + + // If game is already initialized, initialize immediately + if (gameManager.GetType().GetField("_isGameInitialized", + System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Instance)?.GetValue(gameManager) is bool isInitialized && isInitialized) + { + Initialize(); + } + } + else + { + Debug.LogWarning("[ObstacleSpawner] DivingGameManager not found. Initializing immediately."); + Initialize(); + } + } + + /// + /// Initializes the obstacle spawner when triggered by DivingGameManager + /// + private void Initialize() { CalculateScreenBounds(); StartSpawning(); + Debug.Log("[ObstacleSpawner] Initialized"); } private void OnDestroy() diff --git a/Assets/Scripts/Minigames/DivingForPictures/Player/PlayerController.cs b/Assets/Scripts/Minigames/DivingForPictures/Player/PlayerController.cs index 5a974a54..4a9c61b4 100644 --- a/Assets/Scripts/Minigames/DivingForPictures/Player/PlayerController.cs +++ b/Assets/Scripts/Minigames/DivingForPictures/Player/PlayerController.cs @@ -20,6 +20,9 @@ namespace Minigames.DivingForPictures // Tap impulse system variables private float _tapImpulseStrength = 0f; private float _tapDirection = 0f; + + // Initialization flag + private bool _isInitialized = false; void Awake() { @@ -35,11 +38,53 @@ namespace Minigames.DivingForPictures void Start() { - // Register as default consumer for input - InputManager.Instance?.SetDefaultConsumer(this); // Initialize target to current position _targetFingerX = transform.position.x; _isTouchActive = false; + + // Find DivingGameManager and subscribe to its initialization event + DivingGameManager gameManager = FindFirstObjectByType(); + if (gameManager != null) + { + gameManager.OnGameInitialized += Initialize; + + // If game is already initialized, initialize immediately + if (gameManager.GetType().GetField("_isGameInitialized", + System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Instance)?.GetValue(gameManager) is bool isInitialized && isInitialized) + { + Initialize(); + } + } + else + { + Debug.LogWarning("[PlayerController] DivingGameManager not found. Initializing immediately."); + Initialize(); + } + } + + /// + /// Initializes the player controller when triggered by DivingGameManager + /// + private void Initialize() + { + if (_isInitialized) return; + + // Register as default consumer for input + InputManager.Instance?.SetDefaultConsumer(this); + + _isInitialized = true; + Debug.Log("[PlayerController] Initialized"); + } + + private void OnDestroy() + { + // Unsubscribe from events to prevent memory leaks + DivingGameManager gameManager = FindFirstObjectByType(); + if (gameManager != null) + { + gameManager.OnGameInitialized -= Initialize; + } } /// diff --git a/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs b/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs index 50f93f69..0df5cad4 100644 --- a/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs +++ b/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs @@ -107,13 +107,44 @@ namespace Minigames.DivingForPictures } private void Start() + { + + + // Find DivingGameManager and subscribe to its initialization event + DivingGameManager gameManager = FindFirstObjectByType(); + if (gameManager != null) + { + gameManager.OnGameInitialized += Initialize; + + // If game is already initialized, initialize immediately + if (gameManager.GetType().GetField("_isGameInitialized", + System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Instance)?.GetValue(gameManager) is bool isInitialized && isInitialized) + { + Initialize(); + } + } + else + { + Debug.LogWarning("[TrenchTileSpawner] DivingGameManager not found. Initializing immediately."); + Initialize(); + } + } + + /// + /// Initializes the tile spawner when triggered by DivingGameManager + /// + private void Initialize() { CalculateScreenBounds(); + // Spawn initial tiles to fill the screen SpawnInitialTiles(); // Initialize velocity and start the velocity calculation coroutine _currentVelocity = _baseMoveSpeed * Time.fixedDeltaTime; StartCoroutine(VelocityCalculationRoutine()); + + Debug.Log("[TrenchTileSpawner] Initialized"); } private void Update() diff --git a/Assets/Scripts/UI/PauseMenu.cs b/Assets/Scripts/UI/PauseMenu.cs index 7b1f0efc..0cf08dde 100644 --- a/Assets/Scripts/UI/PauseMenu.cs +++ b/Assets/Scripts/UI/PauseMenu.cs @@ -20,7 +20,7 @@ namespace UI SetPauseMenuByLevel(SceneManager.GetActiveScene().name); // Initialize pause menu state - HidePauseMenu(); + HidePauseMenu(false); } private void OnDestroy() @@ -45,7 +45,7 @@ namespace UI gameObject.SetActive(!isMainMenu); if(!isMainMenu) - HidePauseMenu(); // Ensure menu is hidden when switching to a game level + HidePauseMenu(false); // Ensure menu is hidden when switching to a game level Debug.Log($"[PauseMenu] Setting pause menu active: {!isMainMenu} for scene: {levelName}"); } @@ -68,7 +68,7 @@ namespace UI /// /// Hides the pause menu and shows the pause button. Sets input mode to Game. /// - public void HidePauseMenu() + public void HidePauseMenu(bool resetInput = true) { if (pauseMenuPanel != null) pauseMenuPanel.SetActive(false); @@ -77,7 +77,8 @@ namespace UI pauseButton.SetActive(true); // Change input mode back to Game when menu is closed - InputManager.Instance.SetInputMode(InputMode.Game); + if(resetInput) + InputManager.Instance.SetInputMode(InputMode.Game); } /// diff --git a/Assets/Scripts/Utility/SceneOrientationEnforcer.cs b/Assets/Scripts/Utility/SceneOrientationEnforcer.cs index 01f80a9a..b9d63426 100644 --- a/Assets/Scripts/Utility/SceneOrientationEnforcer.cs +++ b/Assets/Scripts/Utility/SceneOrientationEnforcer.cs @@ -1,6 +1,7 @@ using UnityEngine; using UnityEngine.SceneManagement; using System; +using Input; using Settings; namespace Utility @@ -66,6 +67,7 @@ namespace Utility orientationCorrect = IsOrientationCorrect(); if (!orientationCorrect) { + InputManager.Instance.SetInputMode(InputMode.UI); ShowPrompt(); orientationCheckCoroutine = StartCoroutine(OrientationCheckRoutine()); } @@ -77,7 +79,11 @@ namespace Utility return scene.name == "Main Menu" || scene.name == "MainMenu"; } - private bool IsOrientationCorrect() + /// + /// Checks if the current device orientation matches the required orientation for the scene + /// + /// True if the orientation is correct, false otherwise + public bool IsOrientationCorrect() { switch (requiredOrientation) { @@ -121,6 +127,8 @@ namespace Utility StopCoroutine(orientationCheckCoroutine); orientationCheckCoroutine = null; } + + InputManager.Instance.SetInputMode(InputMode.Game); } private void CleanupPromptAndCoroutine()