using System.Collections.Generic; using System.Linq; using UnityEngine; using AppleHills.Core.Settings; using Core; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.ResourceLocations; namespace Utils { /// /// Utility methods for working with SpriteRenderers and game mechanics. /// public static class AppleHillsUtils { /// /// Copies all relevant visual properties from one SpriteRenderer to another. /// /// The source SpriteRenderer. /// The target SpriteRenderer. 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; } /// /// Calculates a normalized movement speed value that's consistent across different screen sizes /// and applies the current time delta /// /// The base normalized speed value /// The actual movement amount to apply per frame public static float CalculateNormalizedMovementSpeed(float normalizedSpeed) { // Get settings for reference height var settings = GameManager.GetSettingsObject(); if (settings == null) { Logging.Warning("[AppleHillsUtils] Could not get settings, using default reference height"); return CalculateNormalizedMovementSpeed(normalizedSpeed, 1080f); } return CalculateNormalizedMovementSpeed(normalizedSpeed, settings.ReferenceScreenHeight); } /// /// Calculates a normalized movement speed value that's consistent across different screen sizes /// and applies the current time delta /// /// The base normalized speed value /// Reference screen height for normalization (typically 1080) /// The actual movement amount to apply per frame 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 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) { Logging.Warning($"[AppleHillsUtils] Error checking addressable key existence: {ex.Message}"); return false; } } } }