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

@@ -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;
}
}
}