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

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.Serialization; using UnityEngine.Serialization;
@@ -49,6 +50,12 @@ namespace Minigames.DivingForPictures
private float _screenTop; private float _screenTop;
private TrenchTilePool _tilePool; 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 TileSpawnZ = -1f;
private const float DefaultTileHeight = 5f; private const float DefaultTileHeight = 5f;
@@ -89,16 +96,58 @@ namespace Minigames.DivingForPictures
{ {
CalculateScreenBounds(); CalculateScreenBounds();
SpawnInitialTiles(); SpawnInitialTiles();
// Initialize velocity and start the velocity calculation coroutine
_currentVelocity = moveSpeed * Time.fixedDeltaTime;
StartCoroutine(VelocityCalculationRoutine());
} }
private void Update() private void Update()
{ {
MoveTiles(); HandleMovement();
HandleTileDestruction(); HandleTileDestruction();
HandleTileSpawning(); HandleTileSpawning();
HandleSpeedRamping(); 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> /// <summary>
/// Calculate height values for all tile prefabs /// Calculate height values for all tile prefabs
/// </summary> /// </summary>
@@ -170,9 +219,25 @@ namespace Minigames.DivingForPictures
/// </summary> /// </summary>
private void SpawnInitialTiles() 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++) for (int i = 0; i < initialTileCount; i++)
{ {
float y = _screenBottom; float y = startingY;
// Calculate proper Y position based on previous tiles // Calculate proper Y position based on previous tiles
if (i > 0 && _activeTiles.Count > 0) if (i > 0 && _activeTiles.Count > 0)
{ {
@@ -206,16 +271,15 @@ namespace Minigames.DivingForPictures
} }
/// <summary> /// <summary>
/// Move all active tiles upward /// Handles the movement of all active tiles based on the current velocity
/// </summary> /// </summary>
private void MoveTiles() private void HandleMovement()
{ {
float moveDelta = moveSpeed * Time.deltaTime;
foreach (var tile in _activeTiles) foreach (var tile in _activeTiles)
{ {
if (tile != null) if (tile != null)
{ {
tile.transform.position += Vector3.up * moveDelta; tile.transform.position += Vector3.up * _currentVelocity;
} }
} }
} }