Files
AppleHillsProduction/Assets/Scripts/Utils/AppleHillsUtils.cs

82 lines
3.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using AppleHills.Core.Settings;
using Core;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.ResourceLocations;
namespace Utils
{
/// <summary>
/// Utility methods for working with SpriteRenderers and game mechanics.
/// </summary>
public static class AppleHillsUtils
{
/// <summary>
/// Copies all relevant visual properties from one SpriteRenderer to another.
/// </summary>
/// <param name="from">The source SpriteRenderer.</param>
/// <param name="to">The target SpriteRenderer.</param>
public static void CopySpriteRendererProperties(SpriteRenderer from, SpriteRenderer to)
{
if (from == null || to == null) return;
to.sprite = from.sprite;
to.color = from.color;
to.flipX = from.flipX;
to.flipY = from.flipY;
to.sharedMaterial = from.sharedMaterial; // Use sharedMaterial instead of material to avoid instantiation in edit mode
to.drawMode = from.drawMode;
to.size = from.size;
to.maskInteraction = from.maskInteraction;
to.sortingLayerID = from.sortingLayerID;
to.sortingOrder = from.sortingOrder;
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)
{
Logging.Warning("[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;
}
public static bool AddressableKeyExists(object key)
{
IList<IResourceLocation> locations;
return Addressables.LoadResourceLocationsAsync(key).WaitForCompletion()?.Count > 0;
}
}
}