Create a diving minigame MVP (#6)
- Obstacles - Tiles - Object pooling - Monster spawns - Scoring - Minigame End Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Co-authored-by: AlexanderT <alexander@foolhardyhorizons.com> Reviewed-on: #6
This commit is contained in:
@@ -44,8 +44,12 @@ namespace Minigames.DivingForPictures
|
||||
private Collider2D _collider;
|
||||
private Camera _mainCamera;
|
||||
private float _screenTop;
|
||||
private float _screenBottom; // Added to track bottom of screen
|
||||
private Coroutine _movementCoroutine;
|
||||
private Coroutine _offScreenCheckCoroutine;
|
||||
private bool _isSurfacing = false; // Flag to track surfacing state
|
||||
private float _velocityFactor = 1.0f; // Current velocity factor from game manager
|
||||
private float _baseMoveSpeed; // Original move speed before velocity factor is applied
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -62,6 +66,7 @@ namespace Minigames.DivingForPictures
|
||||
}
|
||||
|
||||
_mainCamera = Camera.main;
|
||||
_baseMoveSpeed = moveSpeed; // Store original speed
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -105,6 +110,30 @@ namespace Minigames.DivingForPictures
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the velocity factor changes from the DivingGameManager via ObstacleSpawner
|
||||
/// </summary>
|
||||
public void OnVelocityFactorChanged(float velocityFactor)
|
||||
{
|
||||
_velocityFactor = velocityFactor;
|
||||
|
||||
// Update actual move speed based on velocity factor and base speed
|
||||
// We use Abs for magnitude and Sign for direction
|
||||
moveSpeed = _baseMoveSpeed * Mathf.Abs(_velocityFactor);
|
||||
|
||||
// Restart movement with new speed if needed
|
||||
if (enableMovement && gameObject.activeInHierarchy)
|
||||
{
|
||||
if (_movementCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_movementCoroutine);
|
||||
}
|
||||
_movementCoroutine = StartCoroutine(MovementCoroutine());
|
||||
}
|
||||
|
||||
Debug.Log($"[FloatingObstacle] {gameObject.name} velocity factor updated to {_velocityFactor:F2}, speed: {moveSpeed:F2}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine that handles obstacle movement
|
||||
/// </summary>
|
||||
@@ -112,8 +141,12 @@ namespace Minigames.DivingForPictures
|
||||
{
|
||||
while (enabled && gameObject.activeInHierarchy)
|
||||
{
|
||||
// Move the obstacle upward
|
||||
transform.position += Vector3.up * (moveSpeed * Time.deltaTime);
|
||||
// Use velocity factor sign to determine direction
|
||||
Vector3 direction = Vector3.up * Mathf.Sign(_velocityFactor);
|
||||
float speed = moveSpeed * Time.deltaTime;
|
||||
|
||||
// Apply movement in correct direction
|
||||
transform.position += direction * speed;
|
||||
|
||||
// Wait for next frame
|
||||
yield return null;
|
||||
@@ -165,6 +198,9 @@ namespace Minigames.DivingForPictures
|
||||
Vector3 topWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 1f, _mainCamera.transform.position.z));
|
||||
_screenTop = topWorldPoint.y;
|
||||
|
||||
Vector3 bottomWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 0f, _mainCamera.transform.position.z));
|
||||
_screenBottom = bottomWorldPoint.y;
|
||||
|
||||
// Check if obstacle is significantly above screen top (obstacles move upward)
|
||||
// Use a larger buffer to ensure obstacles are truly off-screen before returning to pool
|
||||
if (transform.position.y > _screenTop + 5f)
|
||||
@@ -172,6 +208,11 @@ namespace Minigames.DivingForPictures
|
||||
Debug.Log($"[FloatingObstacle] {gameObject.name} off-screen at Y:{transform.position.y:F2}, screen top:{_screenTop:F2}");
|
||||
ReturnToPool();
|
||||
}
|
||||
else if (transform.position.y < _screenBottom - 5f) // Added check for bottom screen edge
|
||||
{
|
||||
Debug.Log($"[FloatingObstacle] {gameObject.name} below screen at Y:{transform.position.y:F2}, screen bottom:{_screenBottom:F2}");
|
||||
ReturnToPool();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -279,5 +320,20 @@ namespace Minigames.DivingForPictures
|
||||
StartObstacleBehavior();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets surfacing mode, which reverses obstacle movement direction
|
||||
/// </summary>
|
||||
public void StartSurfacing()
|
||||
{
|
||||
if (_isSurfacing) return; // Already surfacing
|
||||
|
||||
_isSurfacing = true;
|
||||
|
||||
// Reverse movement speed (already handled by ObstacleSpawner, but this ensures consistency)
|
||||
moveSpeed *= -1;
|
||||
|
||||
Debug.Log($"[FloatingObstacle] {gameObject.name} started surfacing with speed: {moveSpeed}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user