Finalize normalized movement settings

This commit is contained in:
Michal Pikulski
2025-10-07 09:42:59 +02:00
parent 348e982930
commit 328e8dea0a
5 changed files with 75 additions and 122 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections;
using AppleHills.Core.Settings;
using Pooling;
using Utils;
namespace Minigames.DivingForPictures
{
@@ -182,15 +183,15 @@ namespace Minigames.DivingForPictures
/// </summary>
private IEnumerator MovementCoroutine()
{
Debug.Log($"[FloatingObstacle] Started movement coroutine with speed: {moveSpeed:F3}");
Debug.Log($"[FloatingObstacle] Started movement coroutine with speed: {_baseMoveSpeed:F3}");
while (enabled && gameObject.activeInHierarchy)
{
// Use velocity factor sign to determine direction
Vector3 direction = Vector3.up * Mathf.Sign(_velocityFactor);
// Apply normalized movement with deltaTime for frame rate independence
float speed = moveSpeed * Time.deltaTime;
// Apply normalized movement using the shared utility method
float speed = AppleHillsUtils.CalculateNormalizedMovementSpeed(_baseMoveSpeed);
// Apply movement in correct direction
transform.position += direction * speed;

View File

@@ -5,6 +5,7 @@ using UnityEngine.Events;
using UnityEngine.Serialization;
using Pooling;
using AppleHills.Core.Settings;
using Utils;
namespace Minigames.DivingForPictures
{
@@ -400,7 +401,7 @@ namespace Minigames.DivingForPictures
/// </summary>
private IEnumerator MovementCoroutine()
{
Debug.Log($"[TrenchTileSpawner] Started movement coroutine with normalized speed: {_currentVelocity:F3}");
Debug.Log($"[TrenchTileSpawner] Started movement coroutine with normalized speed: {_baseMoveSpeed:F3}");
while (enabled && gameObject.activeInHierarchy)
{
@@ -414,8 +415,8 @@ namespace Minigames.DivingForPictures
// Use velocity factor sign to determine direction
Vector3 direction = Vector3.up * Mathf.Sign(_velocityFactor);
// Apply normalized movement with deltaTime for frame rate independence
float speed = _currentVelocity * _screenNormalizationFactor;
// Apply normalized movement using the shared utility method
float speed = AppleHillsUtils.CalculateNormalizedMovementSpeed(_baseMoveSpeed);
// Move all active tiles
foreach (var tile in _activeTiles)

View File

@@ -1,9 +1,10 @@
using UnityEngine;
using AppleHills.Core.Settings;
namespace Utils
{
/// <summary>
/// Utility methods for working with SpriteRenderers.
/// Utility methods for working with SpriteRenderers and game mechanics.
/// </summary>
public static class AppleHillsUtils
{
@@ -28,5 +29,43 @@ namespace Utils
to.enabled = true;
to.transform.localScale = from.transform.localScale;
}
/// <summary>
/// Calculates a normalized movement speed value that's consistent across different screen sizes
/// and applies the current time delta
/// </summary>
/// <param name="normalizedSpeed">The base normalized speed value</param>
/// <returns>The actual movement amount to apply per frame</returns>
public static float CalculateNormalizedMovementSpeed(float normalizedSpeed)
{
// Get settings for reference height
var settings = GameManager.GetSettingsObject<IDivingMinigameSettings>();
if (settings == null)
{
Debug.LogWarning("[AppleHillsUtils] Could not get settings, using default reference height");
return CalculateNormalizedMovementSpeed(normalizedSpeed, 1080f);
}
return CalculateNormalizedMovementSpeed(normalizedSpeed, settings.ReferenceScreenHeight);
}
/// <summary>
/// Calculates a normalized movement speed value that's consistent across different screen sizes
/// and applies the current time delta
/// </summary>
/// <param name="normalizedSpeed">The base normalized speed value</param>
/// <param name="referenceScreenHeight">Reference screen height for normalization (typically 1080)</param>
/// <returns>The actual movement amount to apply per frame</returns>
public static float CalculateNormalizedMovementSpeed(float normalizedSpeed, float referenceScreenHeight)
{
// Calculate the screen normalization factor
float screenNormalizationFactor = Screen.height / referenceScreenHeight;
// Apply time delta for frame rate independence
float frameAdjustedSpeed = normalizedSpeed * Time.deltaTime;
// Apply screen normalization
return frameAdjustedSpeed * screenNormalizationFactor;
}
}
}