重构
- 统一 ItemDescForm/RewardItem/GoodsItem 的描述格式 - 减少组件属性的 Clone 数量,目前只在合并仓库、组装防御塔时才进行复制 - 删除先前留下的冗余 fallback 代码
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD.CustomUtility;
|
||||
using GeometryTD.Definition;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
@@ -22,11 +20,6 @@ namespace GeometryTD.UI
|
||||
private bool _hasRefreshed;
|
||||
private string _tipText = "Select one reward";
|
||||
|
||||
public RewardSelectFormUseCase()
|
||||
{
|
||||
ConfigureRewardPool(BuildDefaultRewardPool());
|
||||
}
|
||||
|
||||
public void ConfigureRewardPool(
|
||||
IReadOnlyList<RewardSelectItemRawData> rewardPool,
|
||||
int displayCount = 3,
|
||||
@@ -38,15 +31,14 @@ namespace GeometryTD.UI
|
||||
_rewardPool.Clear();
|
||||
if (rewardPool != null)
|
||||
{
|
||||
for (int i = 0; i < rewardPool.Count; i++)
|
||||
foreach (var item in rewardPool)
|
||||
{
|
||||
RewardSelectItemRawData item = rewardPool[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_rewardPool.Add(CloneRawItem(item));
|
||||
_rewardPool.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +156,7 @@ namespace GeometryTD.UI
|
||||
for (int i = 0; i < finalCount; i++)
|
||||
{
|
||||
RewardSelectItemRawData source = _rewardPool[indexes[i]];
|
||||
results[i] = CloneRawItem(source);
|
||||
results[i] = source;
|
||||
}
|
||||
|
||||
return results;
|
||||
@@ -209,131 +201,5 @@ namespace GeometryTD.UI
|
||||
|
||||
return GameEntry.PlayerInventory.TryConsumeGold(_refreshCost);
|
||||
}
|
||||
|
||||
private static List<RewardSelectItemRawData> BuildDefaultRewardPool()
|
||||
{
|
||||
BackpackInventoryData inventory = InventorySeedUtility.CreateSampleInventory();
|
||||
List<RewardSelectItemRawData> rewards = new List<RewardSelectItemRawData>();
|
||||
|
||||
if (inventory?.MuzzleComponents != null)
|
||||
{
|
||||
for (int i = 0; i < inventory.MuzzleComponents.Count; i++)
|
||||
{
|
||||
MuzzleCompItemData item = inventory.MuzzleComponents[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
rewards.Add(BuildRawItem(item));
|
||||
}
|
||||
}
|
||||
|
||||
if (inventory?.BearingComponents != null)
|
||||
{
|
||||
for (int i = 0; i < inventory.BearingComponents.Count; i++)
|
||||
{
|
||||
BearingCompItemData item = inventory.BearingComponents[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
rewards.Add(BuildRawItem(item));
|
||||
}
|
||||
}
|
||||
|
||||
if (inventory?.BaseComponents != null)
|
||||
{
|
||||
for (int i = 0; i < inventory.BaseComponents.Count; i++)
|
||||
{
|
||||
BaseCompItemData item = inventory.BaseComponents[i];
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
rewards.Add(BuildRawItem(item));
|
||||
}
|
||||
}
|
||||
|
||||
return rewards;
|
||||
}
|
||||
|
||||
private static RewardSelectItemRawData BuildRawItem(TowerCompItemData item)
|
||||
{
|
||||
return new RewardSelectItemRawData
|
||||
{
|
||||
RewardId = item.InstanceId,
|
||||
SlotType = item.SlotType,
|
||||
Title = item.Name,
|
||||
TypeText = BuildTypeText(item.SlotType),
|
||||
Description = BuildDescription(item),
|
||||
Rarity = item.Rarity,
|
||||
Tags = item.Tags != null ? (TagType[])item.Tags.Clone() : Array.Empty<TagType>(),
|
||||
Icon = null,
|
||||
IsSelectable = true,
|
||||
SourceItem = item
|
||||
};
|
||||
}
|
||||
|
||||
private static RewardSelectItemRawData CloneRawItem(RewardSelectItemRawData source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new RewardSelectItemRawData
|
||||
{
|
||||
RewardId = source.RewardId,
|
||||
SlotType = source.SlotType,
|
||||
Title = source.Title,
|
||||
TypeText = source.TypeText,
|
||||
Description = source.Description,
|
||||
Rarity = source.Rarity,
|
||||
Tags = source.Tags != null ? (TagType[])source.Tags.Clone() : Array.Empty<TagType>(),
|
||||
Icon = source.Icon,
|
||||
IsSelectable = source.IsSelectable,
|
||||
SourceItem = source.SourceItem,
|
||||
UserData = source.UserData
|
||||
};
|
||||
}
|
||||
|
||||
private static string BuildTypeText(TowerCompSlotType slotType)
|
||||
{
|
||||
return slotType switch
|
||||
{
|
||||
TowerCompSlotType.Muzzle => "Muzzle Component",
|
||||
TowerCompSlotType.Bearing => "Bearing Component",
|
||||
TowerCompSlotType.Base => "Base Component",
|
||||
TowerCompSlotType.Accessory => "Accessory",
|
||||
_ => "Component"
|
||||
};
|
||||
}
|
||||
|
||||
private static string BuildDescription(TowerCompItemData item)
|
||||
{
|
||||
if (item is MuzzleCompItemData muzzle)
|
||||
{
|
||||
int damage = muzzle.AttackDamage != null && muzzle.AttackDamage.Length > 0 ? muzzle.AttackDamage[0] : 0;
|
||||
return $"Damage: {damage}, Spread: {muzzle.DamageRandomRate:P0}";
|
||||
}
|
||||
|
||||
if (item is BearingCompItemData bearing)
|
||||
{
|
||||
float range = bearing.AttackRange != null && bearing.AttackRange.Length > 0 ? bearing.AttackRange[0] : 0f;
|
||||
float rotateSpeed = bearing.RotateSpeed != null && bearing.RotateSpeed.Length > 0 ? bearing.RotateSpeed[0] : 0f;
|
||||
return $"Range: {range:0.##}, Rotate Speed: {rotateSpeed:0.##}";
|
||||
}
|
||||
|
||||
if (item is BaseCompItemData baseComp)
|
||||
{
|
||||
float attackSpeed = baseComp.AttackSpeed != null && baseComp.AttackSpeed.Length > 0 ? baseComp.AttackSpeed[0] : 0f;
|
||||
return $"Attack Speed: {attackSpeed:0.##}, Property: {baseComp.AttackPropertyType}";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user