Working surfacing function

This commit is contained in:
2025-09-22 10:55:45 +02:00
parent 8326f03086
commit bb3c91ec94
3 changed files with 104 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
using UnityEditor;
using UnityEngine;
using Minigames.DivingForPictures;
/// <summary>
/// Custom editor for TrenchTileSpawner that adds a runtime button to test the StartSurfacing function
/// </summary>
[CustomEditor(typeof(TrenchTileSpawner))]
public class TrenchTileSpawnerEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
// Draw the default inspector
DrawDefaultInspector();
// Get the target TrenchTileSpawner
TrenchTileSpawner spawner = (TrenchTileSpawner)target;
// Add space between default inspector and custom button
EditorGUILayout.Space(10);
// Separator line
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
// Add a label for the runtime testing section
EditorGUILayout.LabelField("Runtime Testing", EditorStyles.boldLabel);
// Only enable the button during play mode
EditorGUI.BeginDisabledGroup(!Application.isPlaying);
// Add the button to call StartSurfacing
if (GUILayout.Button("Start Surfacing", GUILayout.Height(30)))
{
spawner.StartSurfacing();
}
EditorGUI.EndDisabledGroup();
// Add explanatory text
EditorGUILayout.HelpBox("This button will reverse the direction of the trench movement, making the player surface instead of descend. Only works in Play Mode.", MessageType.Info);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9fb1a138e45d4720ba5c95da894b4491
timeCreated: 1758531024

View File

@@ -59,6 +59,9 @@ namespace Minigames.DivingForPictures
private const float TileSpawnZ = -1f; private const float TileSpawnZ = -1f;
private const float DefaultTileHeight = 5f; private const float DefaultTileHeight = 5f;
// Direction state
private bool _isSurfacing = false;
private void Awake() private void Awake()
{ {
_mainCamera = Camera.main; _mainCamera = Camera.main;
@@ -270,6 +273,26 @@ namespace Minigames.DivingForPictures
_screenTop = top.y; _screenTop = top.y;
} }
/// <summary>
/// Reverses direction to start surfacing
/// </summary>
public void StartSurfacing()
{
if (_isSurfacing) return; // Already surfacing
// Reverse the movement direction
moveSpeed *= -1;
// Update velocity immediately
CalculateVelocity();
// Reverse the active tiles array to maintain consistent indexing logic
_activeTiles.Reverse();
// Set surfacing flag
_isSurfacing = true;
}
/// <summary> /// <summary>
/// Handles the movement of all active tiles based on the current velocity /// Handles the movement of all active tiles based on the current velocity
/// </summary> /// </summary>
@@ -279,6 +302,7 @@ namespace Minigames.DivingForPictures
{ {
if (tile != null) if (tile != null)
{ {
// Movement will automatically adapt to negative velocity when surfacing
tile.transform.position += Vector3.up * _currentVelocity; tile.transform.position += Vector3.up * _currentVelocity;
} }
} }
@@ -299,7 +323,20 @@ namespace Minigames.DivingForPictures
} }
float tileHeight = GetTileHeight(topTile); float tileHeight = GetTileHeight(topTile);
if (topTile.transform.position.y - tileHeight / 2 > _screenTop + tileSpawnBuffer)
bool shouldDestroy;
if (_isSurfacing)
{
// When surfacing, destroy tiles at the bottom
shouldDestroy = topTile.transform.position.y + tileHeight / 2 < _screenBottom - tileSpawnBuffer;
}
else
{
// When descending, destroy tiles at the top
shouldDestroy = topTile.transform.position.y - tileHeight / 2 > _screenTop + tileSpawnBuffer;
}
if (shouldDestroy)
{ {
_activeTiles.RemoveAt(0); _activeTiles.RemoveAt(0);
onTileDestroyed?.Invoke(topTile); onTileDestroyed?.Invoke(topTile);
@@ -339,14 +376,31 @@ namespace Minigames.DivingForPictures
} }
float tileHeight = GetTileHeight(bottomTile); float tileHeight = GetTileHeight(bottomTile);
float bottomEdge = bottomTile.transform.position.y - tileHeight / 2;
if (bottomEdge > _screenBottom - tileSpawnBuffer) bool shouldSpawn;
float newY;
if (_isSurfacing)
{
// When surfacing, spawn new tiles at the top
float topEdge = bottomTile.transform.position.y + tileHeight / 2;
shouldSpawn = topEdge < _screenTop + tileSpawnBuffer;
newY = bottomTile.transform.position.y + tileHeight;
}
else
{
// When descending, spawn new tiles at the bottom
float bottomEdge = bottomTile.transform.position.y - tileHeight / 2;
shouldSpawn = bottomEdge > _screenBottom - tileSpawnBuffer;
newY = bottomTile.transform.position.y - tileHeight;
}
if (shouldSpawn)
{ {
float newY = bottomTile.transform.position.y - tileHeight;
SpawnTileAtY(newY); SpawnTileAtY(newY);
} }
} }
/// <summary> /// <summary>
/// Handle increasing the movement speed over time /// Handle increasing the movement speed over time
/// </summary> /// </summary>