Surfacing sequence 80% done

This commit is contained in:
2025-09-22 13:56:42 +02:00
parent ae6b995f45
commit 46950aa877
6 changed files with 1533 additions and 35 deletions

View File

@@ -1,7 +1,9 @@
using UnityEngine;
using System.Collections.Generic;
using System;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.Playables;
namespace Minigames.DivingForPictures
{
@@ -35,6 +37,16 @@ namespace Minigames.DivingForPictures
[Tooltip("Ropes that will break one by one as player takes damage")]
[SerializeField] private RopeBreaker[] playerRopes;
[Header("Surfacing Settings")]
[Tooltip("Duration in seconds for speed transition when surfacing")]
[SerializeField] private float speedTransitionDuration = 2.0f;
[Tooltip("Factor to multiply speed by when surfacing (usually 1.0 for same speed)")]
[SerializeField] private float surfacingSpeedFactor = 3.0f;
[Tooltip("How long to continue spawning tiles after surfacing begins (seconds)")]
[SerializeField] private float surfacingSpawnDelay = 5.0f;
[Tooltip("Reference to the PlayableDirector that will play the surfacing timeline")]
[SerializeField] private PlayableDirector surfacingTimeline;
// Private state variables
private int playerScore = 0;
private float currentSpawnProbability;
@@ -43,15 +55,10 @@ namespace Minigames.DivingForPictures
private List<Monster> activeMonsters = new List<Monster>();
// Velocity management
[Header("Surfacing Settings")]
[Tooltip("Duration in seconds for speed transition when surfacing")]
[SerializeField] private float speedTransitionDuration = 2.0f;
[Tooltip("Factor to multiply speed by when surfacing (usually 1.0 for same speed)")]
[SerializeField] private float surfacingSpeedFactor = 3.0f;
// Velocity state tracking
private float _currentVelocityFactor = 1.0f; // 1.0 = normal descent speed, -1.0 * surfacingSpeedFactor = full surfacing speed
private Coroutine _velocityTransitionCoroutine;
private Coroutine _surfacingSequenceCoroutine;
// Public properties
public int PlayerScore => playerScore;
@@ -360,6 +367,10 @@ namespace Minigames.DivingForPictures
OnVelocityFactorChanged -= tileSpawner.OnVelocityFactorChanged;
OnVelocityFactorChanged += tileSpawner.OnVelocityFactorChanged;
// Subscribe to the last tile event
tileSpawner.onLastTileLeft.RemoveListener(OnLastTileLeft);
tileSpawner.onLastTileLeft.AddListener(OnLastTileLeft);
// Tell spawner to reverse spawn/despawn logic
tileSpawner.StartSurfacing();
@@ -389,8 +400,50 @@ namespace Minigames.DivingForPictures
obstacleSpawner.OnVelocityFactorChanged(_currentVelocityFactor);
}
// Start the surfacing sequence coroutine
if (_surfacingSequenceCoroutine != null)
{
StopCoroutine(_surfacingSequenceCoroutine);
}
_surfacingSequenceCoroutine = StartCoroutine(SurfacingSequence());
Debug.Log($"[DivingGameManager] Started surfacing with target velocity factor: {targetVelocityFactor}");
}
/// <summary>
/// Coroutine to handle the surfacing sequence timing
/// </summary>
private IEnumerator SurfacingSequence()
{
// Wait for the configured delay
yield return new WaitForSeconds(surfacingSpawnDelay);
// Find tile spawner and tell it to stop spawning
TrenchTileSpawner tileSpawner = FindFirstObjectByType<TrenchTileSpawner>();
if (tileSpawner != null)
{
// Tell it to stop spawning new tiles
tileSpawner.StopSpawning();
Debug.Log("[DivingGameManager] Stopped spawning new tiles after delay");
}
}
/// <summary>
/// Called when the last tile leaves the screen
/// </summary>
private void OnLastTileLeft()
{
// Play the timeline
if (surfacingTimeline != null)
{
surfacingTimeline.Play();
Debug.Log("[DivingGameManager] Last tile left the screen, playing timeline");
}
else
{
Debug.LogWarning("[DivingGameManager] No surfacing timeline assigned!");
}
}
// Call this when the game ends
public void EndGame()

View File

@@ -61,6 +61,10 @@ namespace Minigames.DivingForPictures
// Direction state
private bool _isSurfacing = false;
private bool _stopSpawning = false;
// Event triggered when the last tile leaves the screen after stopping spawning
public UnityEvent onLastTileLeft = new UnityEvent();
// Velocity management
private float _baseMoveSpeed;
@@ -311,6 +315,14 @@ namespace Minigames.DivingForPictures
Debug.Log("[TrenchTileSpawner] Started surfacing - reversed array order");
}
/// <summary>
/// Stops spawning new tiles
/// </summary>
public void StopSpawning()
{
_stopSpawning = true;
}
/// <summary>
/// Handles the movement of all active tiles based on the current velocity
/// </summary>
@@ -388,7 +400,15 @@ namespace Minigames.DivingForPictures
/// </summary>
private void HandleTileSpawning()
{
if (_activeTiles.Count == 0) return;
if (_activeTiles.Count == 0)
{
// If we have no active tiles and spawning is stopped, trigger the event
if (_stopSpawning)
{
onLastTileLeft.Invoke();
}
return;
}
GameObject bottomTile = _activeTiles[^1];
if (bottomTile == null)
@@ -397,8 +417,36 @@ namespace Minigames.DivingForPictures
return;
}
// Get the tile height once to use in all calculations
float tileHeight = GetTileHeight(bottomTile);
// If we're in stop spawning mode, don't spawn new tiles
if (_stopSpawning)
{
// Check if this is the last tile, and if it's about to leave the screen
if (_activeTiles.Count == 1)
{
bool isLastTileLeaving;
if (_isSurfacing)
{
// When surfacing, check if the bottom of the tile is above the top of the screen
isLastTileLeaving = bottomTile.transform.position.y - tileHeight / 2 > _screenTop + tileSpawnBuffer;
}
else
{
// When descending, check if the top of the tile is below the bottom of the screen
isLastTileLeaving = bottomTile.transform.position.y + tileHeight / 2 < _screenBottom - tileSpawnBuffer;
}
if (isLastTileLeaving)
{
onLastTileLeft.Invoke();
}
}
return;
}
bool shouldSpawn;
float newY;