Improvements to tile spawning and refactors in the tile management

This commit is contained in:
2025-09-22 09:27:01 +02:00
parent 2942b22c62
commit 8326f03086
2 changed files with 73 additions and 8 deletions

View File

@@ -2001,7 +2001,7 @@ MonoBehaviour:
- {fileID: 2956826569642009690, guid: 7f7f10ca24a5afe46be797daea64111a, type: 3}
initialTileCount: 3
tileSpawnBuffer: 1
moveSpeed: 3
moveSpeed: 2
speedUpFactor: 0
speedUpInterval: 0
maxMoveSpeed: 12
@@ -2014,6 +2014,7 @@ MonoBehaviour:
onTileDestroyed:
m_PersistentCalls:
m_Calls: []
velocityCalculationInterval: 0.5
--- !u!1 &1834056336
GameObject:
m_ObjectHideFlags: 0

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
@@ -48,6 +49,12 @@ namespace Minigames.DivingForPictures
private float _screenBottom;
private float _screenTop;
private TrenchTilePool _tilePool;
// Current velocity for tile movement
private float _currentVelocity;
// Interval for velocity calculations (seconds)
[SerializeField] private float velocityCalculationInterval = 0.5f;
private const float TileSpawnZ = -1f;
private const float DefaultTileHeight = 5f;
@@ -89,15 +96,57 @@ namespace Minigames.DivingForPictures
{
CalculateScreenBounds();
SpawnInitialTiles();
// Initialize velocity and start the velocity calculation coroutine
_currentVelocity = moveSpeed * Time.fixedDeltaTime;
StartCoroutine(VelocityCalculationRoutine());
}
private void Update()
{
MoveTiles();
HandleMovement();
HandleTileDestruction();
HandleTileSpawning();
HandleSpeedRamping();
}
/// <summary>
/// Gets the current velocity of the tiles
/// </summary>
/// <returns>The current upward velocity of the tiles</returns>
public float GetCurrentVelocity()
{
return _currentVelocity;
}
/// <summary>
/// Sets a custom velocity, overriding the calculated one
/// </summary>
/// <param name="velocity">The new velocity value</param>
public void SetVelocity(float velocity)
{
_currentVelocity = velocity;
}
/// <summary>
/// Coroutine that periodically calculates the velocity based on game state
/// </summary>
private IEnumerator VelocityCalculationRoutine()
{
while (true)
{
CalculateVelocity();
yield return new WaitForSeconds(velocityCalculationInterval);
}
}
/// <summary>
/// Calculates the current velocity based on move speed
/// </summary>
private void CalculateVelocity()
{
_currentVelocity = moveSpeed * Time.fixedDeltaTime;
}
/// <summary>
/// Calculate height values for all tile prefabs
@@ -170,9 +219,25 @@ namespace Minigames.DivingForPictures
/// </summary>
private void SpawnInitialTiles()
{
// Calculate starting Y position - moved 2 tiles up from the bottom of the screen
float startingY = _screenBottom;
// If we have prefab tiles with known heights, use their average height for offset calculation
float tileHeightEstimate = DefaultTileHeight;
if (tilePrefabs != null && tilePrefabs.Count > 0 && tilePrefabs[0] != null)
{
if (_tileHeights.TryGetValue(tilePrefabs[0], out float height))
{
tileHeightEstimate = height;
}
}
// Move starting position up by 2 tile heights
startingY += tileHeightEstimate * 2;
for (int i = 0; i < initialTileCount; i++)
{
float y = _screenBottom;
float y = startingY;
// Calculate proper Y position based on previous tiles
if (i > 0 && _activeTiles.Count > 0)
{
@@ -206,16 +271,15 @@ namespace Minigames.DivingForPictures
}
/// <summary>
/// Move all active tiles upward
/// Handles the movement of all active tiles based on the current velocity
/// </summary>
private void MoveTiles()
private void HandleMovement()
{
float moveDelta = moveSpeed * Time.deltaTime;
foreach (var tile in _activeTiles)
{
if (tile != null)
{
tile.transform.position += Vector3.up * moveDelta;
tile.transform.position += Vector3.up * _currentVelocity;
}
}
}