为 RepoItem 添加背景耐久变色

This commit is contained in:
SepComet
2026-03-04 19:17:13 +08:00
parent 1e5803f4c5
commit 298345fa17
10 changed files with 424 additions and 232 deletions
@@ -37,6 +37,7 @@ namespace GeometryTD.Definition
/// 组件当前耐久(0~100)。
/// </summary>
public float Endurance { get; set; } = 100f;
public bool IsAssembledIntoTower { get; set; }
/// <summary>
@@ -109,6 +109,9 @@ namespace GeometryTD.UI
return System.Array.Empty<RepoItemContext>();
}
Dictionary<long, MuzzleCompItemData> muzzleMap = BuildComponentMap(inventory.MuzzleComponents);
Dictionary<long, BearingCompItemData> bearingMap = BuildComponentMap(inventory.BearingComponents);
Dictionary<long, BaseCompItemData> baseMap = BuildComponentMap(inventory.BaseComponents);
List<RepoItemContext> itemContexts = new List<RepoItemContext>();
if (inventory.Towers != null)
@@ -125,6 +128,7 @@ namespace GeometryTD.UI
{
InstanceId = tower.InstanceId,
CanDrag = false,
EnduranceRate01 = ItemDescUtility.ResolveTowerEnduranceRate(tower, muzzleMap, bearingMap, baseMap),
ComponentSlotType = TowerCompSlotType.None,
IconAreaContext = BuildIconAreaContext(tower)
});
@@ -133,7 +137,7 @@ namespace GeometryTD.UI
tower.InstanceId,
tower.Name,
"Tower",
ItemDescUtility.BuildTowerDesc(tower.Stats) ?? string.Empty,
ItemDescUtility.BuildTowerDesc(tower, muzzleMap, bearingMap, baseMap) ?? string.Empty,
tower.Stats != null ? tower.Stats.Tags : null);
}
}
@@ -152,6 +156,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = false,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Muzzle,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -178,6 +183,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = false,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Bearing,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -205,6 +211,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = false,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Base,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -254,6 +261,29 @@ namespace GeometryTD.UI
};
}
private static Dictionary<long, TComp> BuildComponentMap<TComp>(IReadOnlyList<TComp> items)
where TComp : TowerCompItemData
{
Dictionary<long, TComp> map = new Dictionary<long, TComp>();
if (items == null)
{
return map;
}
for (int i = 0; i < items.Count; i++)
{
TComp item = items[i];
if (item == null || item.InstanceId <= 0)
{
continue;
}
map[item.InstanceId] = item;
}
return map;
}
private static IconAreaContext BuildIconAreaContext(TowerItemData tower)
{
if (tower == null)
@@ -8,6 +8,7 @@ namespace GeometryTD.UI
{
public long InstanceId;
public bool CanDrag;
public float EnduranceRate01;
public TowerCompSlotType ComponentSlotType;
public IconAreaContext IconAreaContext;
}
@@ -102,6 +102,9 @@ namespace GeometryTD.UI
return null;
}
Dictionary<long, MuzzleCompItemData> muzzleMap = BuildComponentMap(rawData.Inventory.MuzzleComponents);
Dictionary<long, BearingCompItemData> bearingMap = BuildComponentMap(rawData.Inventory.BearingComponents);
Dictionary<long, BaseCompItemData> baseMap = BuildComponentMap(rawData.Inventory.BaseComponents);
List<RepoItemContext> items = new List<RepoItemContext>();
if (rawData.Inventory.Towers != null)
@@ -117,6 +120,7 @@ namespace GeometryTD.UI
{
InstanceId = tower.InstanceId,
CanDrag = true,
EnduranceRate01 = ItemDescUtility.ResolveTowerEnduranceRate(tower, muzzleMap, bearingMap, baseMap),
ComponentSlotType = TowerCompSlotType.None,
IconAreaContext = BuildIconAreaContext(tower)
});
@@ -124,7 +128,7 @@ namespace GeometryTD.UI
tower.InstanceId,
tower.Name,
"Tower",
ItemDescUtility.BuildTowerDesc(tower.Stats) ?? string.Empty,
ItemDescUtility.BuildTowerDesc(tower, muzzleMap, bearingMap, baseMap) ?? string.Empty,
tower.Stats != null ? tower.Stats.Tags : null);
}
}
@@ -142,6 +146,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = true,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Muzzle,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -167,6 +172,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = true,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Bearing,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -192,6 +198,7 @@ namespace GeometryTD.UI
{
InstanceId = item.InstanceId,
CanDrag = true,
EnduranceRate01 = ItemDescUtility.ResolveComponentEnduranceRate(item),
ComponentSlotType = TowerCompSlotType.Base,
IconAreaContext = BuildIconAreaContext(item)
});
@@ -258,6 +265,29 @@ namespace GeometryTD.UI
};
}
private static Dictionary<long, TComp> BuildComponentMap<TComp>(IReadOnlyList<TComp> items)
where TComp : TowerCompItemData
{
Dictionary<long, TComp> map = new Dictionary<long, TComp>();
if (items == null)
{
return map;
}
for (int i = 0; i < items.Count; i++)
{
TComp item = items[i];
if (item == null || item.InstanceId <= 0)
{
continue;
}
map[item.InstanceId] = item;
}
return map;
}
private static IconAreaContext BuildIconAreaContext(TowerItemData tower)
{
if (tower == null)
@@ -57,7 +57,7 @@ namespace GeometryTD.UI
ConfigId = 2,
Name = "控制枪口",
Rarity = RarityType.Blue,
Endurance = 100f,
Endurance = 80,
IsAssembledIntoTower = false,
AttackDamage = new[] { 30, 50, 70, 90, 100 },
DamageRandomRate = 0.01f,
@@ -86,7 +86,7 @@ namespace GeometryTD.UI
ConfigId = 1,
Name = "元素轴承",
Rarity = RarityType.Green,
Endurance = 95f,
Endurance = 1f,
IsAssembledIntoTower = true,
RotateSpeed = new[] { 10f, 12f, 13f, 14f, 15f },
AttackRange = new[] { 2f, 2f, 2f, 2f, 2f },
@@ -100,7 +100,7 @@ namespace GeometryTD.UI
ConfigId = 2,
Name = "控制轴承",
Rarity = RarityType.Blue,
Endurance = 100f,
Endurance = 20,
IsAssembledIntoTower = false,
RotateSpeed = new[] { 20f, 25f, 30f, 32f, 35f },
AttackRange = new[] { 6f, 6.5f, 7f, 8f, 8f },
@@ -141,7 +141,7 @@ namespace GeometryTD.UI
ConfigId = 2,
Name = "控制底座",
Rarity = RarityType.Blue,
Endurance = 100f,
Endurance = 50f,
IsAssembledIntoTower = false,
AttackSpeed = new[] { 4f, 4.2f, 4.4f, 4.6f, 4.8f },
AttackPropertyType = AttackPropertyType.Ice,
@@ -154,7 +154,7 @@ namespace GeometryTD.UI
ConfigId = 3,
Name = "穿透底座",
Rarity = RarityType.Purple,
Endurance = 95f,
Endurance = 30f,
IsAssembledIntoTower = false,
AttackSpeed = new[] { 1f, 1f, 1f, 1f, 1f },
AttackPropertyType = AttackPropertyType.Physics,
@@ -9,11 +9,13 @@ namespace GeometryTD.UI
{
public class RepoItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[SerializeField] private Image _highlightImage;
[SerializeField] private Image _bgImage;
[SerializeField] private IconArea _iconArea;
private static readonly Color NormalColor = new Color32(40, 40, 40, 180);
private static readonly Color SelectedColor = new Color32(255, 216, 102, 255);
private static readonly Color EmptyEnduranceColor = new Color(0.8f, 0f, 0f, 1f);
private static readonly Color FullEnduranceColor = new Color(0f, 0.8f, 0f, 1f);
private static readonly Vector2 DefaultDragGhostSize = new Vector2(64f, 64f);
[SerializeField] private RepoItemContext _context;
@@ -74,12 +76,24 @@ namespace GeometryTD.UI
public void SetSelected(bool isSelected)
{
_isSelected = isSelected;
if (_highlightImage == null)
RefreshBackgroundColor();
}
private void RefreshBackgroundColor()
{
if (_bgImage == null)
{
return;
}
_highlightImage.color = isSelected ? SelectedColor : NormalColor;
if (_isSelected)
{
_bgImage.color = SelectedColor;
return;
}
float enduranceRate = _context != null ? Mathf.Clamp01(_context.EnduranceRate01) : 1f;
_bgImage.color = Color.Lerp(EmptyEnduranceColor, FullEnduranceColor, enduranceRate);
}
public void OnClick()
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using GeometryTD.Definition;
@@ -54,6 +55,73 @@ namespace GeometryTD.CustomUtility
return sb.ToString();
}
public static string BuildTowerDesc(
TowerItemData tower,
IReadOnlyDictionary<long, MuzzleCompItemData> muzzleMap,
IReadOnlyDictionary<long, BearingCompItemData> bearingMap,
IReadOnlyDictionary<long, BaseCompItemData> baseMap)
{
if (tower == null)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder();
sb.Append(BuildTowerDesc(tower.Stats ?? new TowerStatsData()));
float enduranceRate = ResolveTowerEnduranceRate(tower, muzzleMap, bearingMap, baseMap);
sb.AppendLine($"平均耐久: {enduranceRate * 100f:0.#}");
return sb.ToString();
}
public static float ResolveComponentEnduranceRate(TowerCompItemData item)
{
if (item == null)
{
return 1f;
}
return UnityEngine.Mathf.Clamp01(item.Endurance / 100f);
}
public static float ResolveTowerEnduranceRate(
TowerItemData tower,
IReadOnlyDictionary<long, MuzzleCompItemData> muzzleMap,
IReadOnlyDictionary<long, BearingCompItemData> bearingMap,
IReadOnlyDictionary<long, BaseCompItemData> baseMap)
{
if (tower == null)
{
return 1f;
}
float sum = 0f;
int count = 0;
if (muzzleMap != null && muzzleMap.TryGetValue(tower.MuzzleComponentInstanceId, out MuzzleCompItemData muzzle))
{
sum += ResolveComponentEnduranceRate(muzzle);
count++;
}
if (bearingMap != null && bearingMap.TryGetValue(tower.BearingComponentInstanceId, out BearingCompItemData bearing))
{
sum += ResolveComponentEnduranceRate(bearing);
count++;
}
if (baseMap != null && baseMap.TryGetValue(tower.BaseComponentInstanceId, out BaseCompItemData baseComp))
{
sum += ResolveComponentEnduranceRate(baseComp);
count++;
}
if (count <= 0)
{
return 1f;
}
return UnityEngine.Mathf.Clamp01(sum / count);
}
public static string BuildMuzzleDesc(MuzzleCompItemData muzzleData)
{
StringBuilder sb = new StringBuilder();
@@ -66,9 +134,11 @@ namespace GeometryTD.CustomUtility
}
sb.Append('\n');
sb.Append($"伤害浮动:{muzzleData.DamageRandomRate:P0}\n");
sb.AppendLine($"伤害浮动:{muzzleData.DamageRandomRate:P0}");
sb.Append($"攻击方式:{ConvertAttackMethod(muzzleData.AttackMethodType)}\n");
sb.AppendLine($"攻击方式:{ConvertAttackMethod(muzzleData.AttackMethodType)}");
sb.AppendLine($"当前耐久:{muzzleData.Endurance}");
return sb.ToString();
}
@@ -93,6 +163,8 @@ namespace GeometryTD.CustomUtility
}
sb.Append('\n');
sb.AppendLine($"当前耐久:{bearingData.Endurance}");
return sb.ToString();
}
@@ -108,7 +180,9 @@ namespace GeometryTD.CustomUtility
}
sb.Append('\n');
sb.Append($"伤害属性:{ConvertAttackProperty(baseData.AttackPropertyType)}\n");
sb.AppendLine($"伤害属性:{ConvertAttackProperty(baseData.AttackPropertyType)}");
sb.AppendLine($"当前耐久:{baseData.Endurance}");
return sb.ToString();
}
@@ -118,7 +192,7 @@ namespace GeometryTD.CustomUtility
return type switch
{
AttackMethodType.None => "无效",
AttackMethodType.NormalBullet => "发射子弹",
AttackMethodType.NormalBullet => "发送子弹",
AttackMethodType.Range => "范围攻击",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
@@ -131,7 +205,7 @@ namespace GeometryTD.CustomUtility
AttackPropertyType.None => "无效",
AttackPropertyType.Physics => "<color=#ffff00ff>物理</color>", //yellow
AttackPropertyType.Fire => "<color=#ff0000ff>火</color>", //red
AttackPropertyType.Water => "<color=#00ffffff>水</color>", //cyan
AttackPropertyType.Water => "<color=#00ffffff>水/color>", //cyan
AttackPropertyType.Earth => "<color=#00ff00ff>自然</color>", //lime
AttackPropertyType.Poison => "<color=#008000ff>毒</color>", //green
AttackPropertyType.Ice => "<color=#0000a0ff>冰</color>", //darkblue
@@ -139,4 +213,6 @@ namespace GeometryTD.CustomUtility
};
}
}
}
}