Surfacing functions in all manager components

This commit is contained in:
2025-09-22 11:40:01 +02:00
parent bb3c91ec94
commit b0e44f156e
6 changed files with 226 additions and 16 deletions

View File

@@ -75,6 +75,7 @@ namespace Minigames.DivingForPictures
private Coroutine _spawnCoroutine;
private readonly List<GameObject> _activeObstacles = new List<GameObject>();
private int _obstacleCounter = 0; // Counter for unique obstacle naming
private bool _isSurfacing = false; // Flag to track surfacing state
private void Awake()
{
@@ -250,6 +251,13 @@ namespace Minigames.DivingForPictures
/// </summary>
private void TrySpawnObstacle()
{
// Don't spawn new obstacles when surfacing
if (_isSurfacing)
{
Debug.Log("[ObstacleSpawner] Skipping obstacle spawn - currently surfacing");
return;
}
Debug.Log($"[ObstacleSpawner] TrySpawnObstacle called at {Time.time:F2}");
if (obstaclePrefabs == null || obstaclePrefabs.Count == 0)
@@ -338,25 +346,53 @@ namespace Minigames.DivingForPictures
return;
}
Debug.Log($"[ObstacleSpawner] Got obstacle {obstacle.name} from pool, active state: {obstacle.activeInHierarchy}");
// FORCE ACTIVATION - bypass pool issues
if (!obstacle.activeInHierarchy)
{
Debug.LogWarning($"[ObstacleSpawner] Pool returned inactive object {obstacle.name}, force activating!");
obstacle.SetActive(true);
Debug.Log($"[ObstacleSpawner] After force activation, {obstacle.name} active state: {obstacle.activeInHierarchy}");
}
// Important: Set position/parent/rotation BEFORE activation to avoid visual glitches
obstacle.transform.position = position;
obstacle.transform.rotation = prefab.transform.rotation;
obstacle.transform.SetParent(transform);
Debug.Log($"[ObstacleSpawner] After positioning, obstacle {obstacle.name} active state: {obstacle.activeInHierarchy}");
Debug.Log($"[ObstacleSpawner] Got obstacle {obstacle.name} from pool, active state: {obstacle.activeInHierarchy}");
// ENHANCED FORCE ACTIVATION - more robust approach
if (!obstacle.activeInHierarchy)
{
Debug.LogWarning($"[ObstacleSpawner] Pool returned inactive object {obstacle.name}, force activating!");
// Configure obstacle BEFORE activation
ConfigureObstacle(obstacle, prefabIndex);
// Force activate the obstacle
obstacle.SetActive(true);
// Double-check activation status
if (!obstacle.activeInHierarchy)
{
Debug.LogError($"[ObstacleSpawner] CRITICAL ERROR: Failed to activate {obstacle.name} after multiple attempts!");
// Last resort: try to instantiate a new one instead
GameObject newObstacle = Instantiate(prefab, position, prefab.transform.rotation, transform);
if (newObstacle != null)
{
obstacle = newObstacle;
ConfigureObstacle(obstacle, prefabIndex);
}
}
Debug.Log($"[ObstacleSpawner] After force activation, {obstacle.name} active state: {obstacle.activeInHierarchy}");
}
else
{
// Still configure if already active
ConfigureObstacle(obstacle, prefabIndex);
}
}
else
{
Debug.Log($"[ObstacleSpawner] Instantiating new obstacle (pooling disabled)");
obstacle = Instantiate(prefab, position, prefab.transform.rotation, transform);
// Configure the newly instantiated obstacle
ConfigureObstacle(obstacle, prefabIndex);
}
// Assign unique name with counter
@@ -364,10 +400,6 @@ namespace Minigames.DivingForPictures
string oldName = obstacle.name;
obstacle.name = $"Obstacle{_obstacleCounter:D3}";
Debug.Log($"[ObstacleSpawner] Renamed obstacle from '{oldName}' to '{obstacle.name}', active state: {obstacle.activeInHierarchy}");
// Configure the obstacle
ConfigureObstacle(obstacle, prefabIndex);
Debug.Log($"[ObstacleSpawner] After configuration, obstacle {obstacle.name} active state: {obstacle.activeInHierarchy}");
// Track active obstacles
_activeObstacles.Add(obstacle);
@@ -459,6 +491,32 @@ namespace Minigames.DivingForPictures
CalculateScreenBounds();
}
/// <summary>
/// Start surfacing mode - reverse direction of existing obstacles and stop spawning new ones
/// </summary>
public void StartSurfacing()
{
if (_isSurfacing) return; // Already surfacing
_isSurfacing = true;
// Reverse direction of all existing obstacles
foreach (GameObject obstacle in _activeObstacles)
{
if (obstacle != null)
{
FloatingObstacle obstacleComponent = obstacle.GetComponent<FloatingObstacle>();
if (obstacleComponent != null)
{
// Call StartSurfacing on the obstacle component itself
obstacleComponent.StartSurfacing();
}
}
}
Debug.Log($"[ObstacleSpawner] Started surfacing mode. Reversed direction of {_activeObstacles.Count} active obstacles.");
}
/// <summary>
/// Gets the count of currently active obstacles
/// </summary>