Wait for correct screen orientation

This commit is contained in:
Michal Pikulski
2025-09-30 13:13:37 +02:00
parent c640be7d9c
commit f1f4234c2a
7 changed files with 175 additions and 13 deletions

View File

@@ -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();
}
/// <summary>
/// Initializes the game components once the orientation is correct.
/// This is called by SceneOrientationEnforcer when the device is properly oriented.
/// </summary>
public void InitializeGame()
{
// Prevent double initialization
if (_isGameInitialized) return;
Debug.Log("[DivingGameManager] Initializing game");
// Subscribe to tile spawned event
TrenchTileSpawner tileSpawner = FindFirstObjectByType<TrenchTileSpawner>();
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()

View File

@@ -74,9 +74,38 @@ namespace Minigames.DivingForPictures
}
private void Start()
{
// Find DivingGameManager and subscribe to its initialization event
DivingGameManager gameManager = FindFirstObjectByType<DivingGameManager>();
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();
}
}
/// <summary>
/// Initializes the obstacle spawner when triggered by DivingGameManager
/// </summary>
private void Initialize()
{
CalculateScreenBounds();
StartSpawning();
Debug.Log("[ObstacleSpawner] Initialized");
}
private void OnDestroy()

View File

@@ -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<DivingGameManager>();
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();
}
}
/// <summary>
/// Initializes the player controller when triggered by DivingGameManager
/// </summary>
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<DivingGameManager>();
if (gameManager != null)
{
gameManager.OnGameInitialized -= Initialize;
}
}
/// <summary>

View File

@@ -107,13 +107,44 @@ namespace Minigames.DivingForPictures
}
private void Start()
{
// Find DivingGameManager and subscribe to its initialization event
DivingGameManager gameManager = FindFirstObjectByType<DivingGameManager>();
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();
}
}
/// <summary>
/// Initializes the tile spawner when triggered by DivingGameManager
/// </summary>
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()