Coroutine updates WIP

This commit is contained in:
Michal Pikulski
2025-09-18 14:29:10 +02:00
parent 3f52de9e1e
commit c5ce2ae1af
3 changed files with 249 additions and 40 deletions

View File

@@ -32,7 +32,7 @@ namespace Minigames.DivingForPictures
[Header("Spawn Position")]
[Tooltip("How far below screen to spawn obstacles")]
[SerializeField] private float spawnDistanceBelowScreen = 2f;
[Tooltip("Horizontal spawn range (distance from center)")]
[SerializeField] private float spawnRangeX = 8f;
@@ -71,6 +71,7 @@ namespace Minigames.DivingForPictures
private ObstaclePool _obstaclePool;
private Camera _mainCamera;
private float _screenBottom;
private float _spawnRangeX;
private Coroutine _spawnCoroutine;
private readonly List<GameObject> _activeObstacles = new List<GameObject>();
@@ -174,7 +175,7 @@ namespace Minigames.DivingForPictures
}
/// <summary>
/// Calculate screen bounds in world space
/// Calculate screen bounds in world space dynamically
/// </summary>
private void CalculateScreenBounds()
{
@@ -188,8 +189,19 @@ namespace Minigames.DivingForPictures
}
}
// Calculate screen bottom (Y spawn position will be 2 units below this)
Vector3 bottomWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 0f, _mainCamera.nearClipPlane));
_screenBottom = bottomWorldPoint.y;
// Calculate screen width in world units
Vector3 leftWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0f, 0.5f, _mainCamera.nearClipPlane));
Vector3 rightWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(1f, 0.5f, _mainCamera.nearClipPlane));
float screenWidth = rightWorldPoint.x - leftWorldPoint.x;
// Calculate spawn range based on 80% of screen width (40% on each side from center)
_spawnRangeX = (screenWidth * 0.8f) / 2f;
Debug.Log($"[ObstacleSpawner] Screen calculated - Width: {screenWidth:F2}, Bottom: {_screenBottom:F2}, Spawn Range X: ±{_spawnRangeX:F2}");
}
/// <summary>
@@ -273,9 +285,11 @@ namespace Minigames.DivingForPictures
/// </summary>
private Vector3 GetRandomSpawnPosition()
{
float randomX = Random.Range(-spawnRangeX, spawnRangeX);
float spawnY = _screenBottom - spawnDistanceBelowScreen;
// Use dynamically calculated spawn range (80% of screen width)
float randomX = Random.Range(-_spawnRangeX, _spawnRangeX);
// Spawn 2 units below screen bottom
float spawnY = _screenBottom - 2f;
return new Vector3(randomX, spawnY, 0f);
}
@@ -416,6 +430,14 @@ namespace Minigames.DivingForPictures
maxMoveSpeed = max;
}
/// <summary>
/// Public method to recalculate screen bounds (useful if camera changes)
/// </summary>
public void RecalculateScreenBounds()
{
CalculateScreenBounds();
}
/// <summary>
/// Gets the count of currently active obstacles
/// </summary>
@@ -424,15 +446,19 @@ namespace Minigames.DivingForPictures
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
// Draw spawn area
Gizmos.color = Color.yellow;
Vector3 center = new Vector3(0f, _screenBottom - spawnDistanceBelowScreen, 0f);
Vector3 size = new Vector3(spawnRangeX * 2f, 1f, 1f);
Gizmos.DrawWireCube(center, size);
// Draw collision radius at spawn point
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(center, spawnCollisionRadius);
// Only draw if screen bounds have been calculated
if (_spawnRangeX > 0f)
{
// Draw spawn area using dynamic calculations
Gizmos.color = Color.yellow;
Vector3 center = new Vector3(0f, _screenBottom - 2f, 0f);
Vector3 size = new Vector3(_spawnRangeX * 2f, 1f, 1f);
Gizmos.DrawWireCube(center, size);
// Draw collision radius at spawn point
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(center, spawnCollisionRadius);
}
}
#endif
}