125 lines
4.4 KiB
C#
125 lines
4.4 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.Core;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.Core
|
|
{
|
|
public static class TowerComposedIconCacheUtility
|
|
{
|
|
private const string MuzzleAssetName = "Muzzle";
|
|
private const string BearingAssetName = "Bearing";
|
|
private const string BaseAssetName = "Base";
|
|
|
|
private static Sprite _muzzleSprite;
|
|
private static Sprite _bearingSprite;
|
|
private static Sprite _baseSprite;
|
|
private static bool _requestedMuzzle;
|
|
private static bool _requestedBearing;
|
|
private static bool _requestedBase;
|
|
|
|
public static Sprite ResolveTowerIconSprite(
|
|
TowerItemData tower,
|
|
IReadOnlyDictionary<long, MuzzleCompItemData> muzzleMap,
|
|
IReadOnlyDictionary<long, BearingCompItemData> bearingMap,
|
|
IReadOnlyDictionary<long, BaseCompItemData> baseMap)
|
|
{
|
|
if (tower == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!TryGetComponents(tower, muzzleMap, bearingMap, baseMap,
|
|
out MuzzleCompItemData muzzleComp,
|
|
out BearingCompItemData bearingComp,
|
|
out BaseCompItemData baseComp))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Color muzzleColor = IconColorGenerator.GenerateForComponent(muzzleComp);
|
|
Color bearingColor = IconColorGenerator.GenerateForComponent(bearingComp);
|
|
Color baseColor = IconColorGenerator.GenerateForComponent(baseComp);
|
|
|
|
string cacheKey = TowerIconComposeUtility.BuildCacheKey(
|
|
tower.MuzzleComponentInstanceId,
|
|
tower.BearingComponentInstanceId,
|
|
tower.BaseComponentInstanceId,
|
|
muzzleColor,
|
|
bearingColor,
|
|
baseColor);
|
|
|
|
if (tower.ComposedIconSprite != null && string.Equals(tower.ComposedIconKey, cacheKey))
|
|
{
|
|
return tower.ComposedIconSprite;
|
|
}
|
|
|
|
EnsureBaseSpritesRequested();
|
|
if (_muzzleSprite == null || _bearingSprite == null || _baseSprite == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Sprite composedSprite = TowerIconComposeUtility.Compose(
|
|
_muzzleSprite,
|
|
muzzleColor,
|
|
_bearingSprite,
|
|
bearingColor,
|
|
_baseSprite,
|
|
baseColor);
|
|
|
|
if (composedSprite == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
tower.ComposedIconSprite = composedSprite;
|
|
tower.ComposedIconKey = cacheKey;
|
|
return composedSprite;
|
|
}
|
|
|
|
private static bool TryGetComponents(
|
|
TowerItemData tower,
|
|
IReadOnlyDictionary<long, MuzzleCompItemData> muzzleMap,
|
|
IReadOnlyDictionary<long, BearingCompItemData> bearingMap,
|
|
IReadOnlyDictionary<long, BaseCompItemData> baseMap,
|
|
out MuzzleCompItemData muzzleComp,
|
|
out BearingCompItemData bearingComp,
|
|
out BaseCompItemData baseComp)
|
|
{
|
|
muzzleComp = null;
|
|
bearingComp = null;
|
|
baseComp = null;
|
|
|
|
if (tower == null || muzzleMap == null || bearingMap == null || baseMap == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return muzzleMap.TryGetValue(tower.MuzzleComponentInstanceId, out muzzleComp) &&
|
|
bearingMap.TryGetValue(tower.BearingComponentInstanceId, out bearingComp) &&
|
|
baseMap.TryGetValue(tower.BaseComponentInstanceId, out baseComp) &&
|
|
muzzleComp != null && bearingComp != null && baseComp != null;
|
|
}
|
|
|
|
private static void EnsureBaseSpritesRequested()
|
|
{
|
|
if (!_requestedMuzzle)
|
|
{
|
|
_requestedMuzzle = true;
|
|
CoreServiceHub.SpriteCache.Get(MuzzleAssetName, sprite => { _muzzleSprite = sprite; });
|
|
}
|
|
|
|
if (!_requestedBearing)
|
|
{
|
|
_requestedBearing = true;
|
|
CoreServiceHub.SpriteCache.Get(BearingAssetName, sprite => { _bearingSprite = sprite; });
|
|
}
|
|
|
|
if (!_requestedBase)
|
|
{
|
|
_requestedBase = true;
|
|
CoreServiceHub.SpriteCache.Get(BaseAssetName, sprite => { _baseSprite = sprite; });
|
|
}
|
|
}
|
|
}
|
|
} |