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

110 lines
4.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
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, Addressables.MergeMode mergeMode = Addressables.MergeMode.Union)
{
try
{
// Handle different key types
if (key is string[] keyArray)
{
// For string arrays, use the array as is with merge mode
return Addressables.LoadResourceLocationsAsync(keyArray, mergeMode).WaitForCompletion()?.Count > 0;
}
else if (key is IEnumerable<object> keyList)
{
// For collections of keys, convert to object[]
return Addressables.LoadResourceLocationsAsync(keyList.ToArray(), mergeMode).WaitForCompletion()?.Count > 0;
}
else if (key is string stringKey)
{
// For single string keys, wrap in array
return Addressables.LoadResourceLocationsAsync(new string[] { stringKey }, mergeMode).WaitForCompletion()?.Count > 0;
}
else
{
// For other single keys (AssetReference, etc.), wrap in object[]
return Addressables.LoadResourceLocationsAsync(new object[] { key }, mergeMode).WaitForCompletion()?.Count > 0;
}
}
catch (System.Exception ex)
{
Debug.LogWarning($"[AppleHillsUtils] Error checking addressable key existence: {ex.Message}");
return false;
}
}
}
}