131 lines
4.5 KiB
C#
131 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
|
|
namespace GeometryTD.CustomUtility
|
|
{
|
|
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 s_RequestedMuzzle;
|
|
private static bool s_RequestedBearing;
|
|
private static bool s_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 (GameEntry.SpriteCache == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!s_RequestedMuzzle)
|
|
{
|
|
s_RequestedMuzzle = true;
|
|
GameEntry.SpriteCache.GetSprite(MuzzleAssetName, sprite => { _muzzleSprite = sprite; });
|
|
}
|
|
|
|
if (!s_RequestedBearing)
|
|
{
|
|
s_RequestedBearing = true;
|
|
GameEntry.SpriteCache.GetSprite(BearingAssetName, sprite => { _bearingSprite = sprite; });
|
|
}
|
|
|
|
if (!s_RequestedBase)
|
|
{
|
|
s_RequestedBase = true;
|
|
GameEntry.SpriteCache.GetSprite(BaseAssetName, sprite => { _baseSprite = sprite; });
|
|
}
|
|
}
|
|
}
|
|
}
|