【发布时间】:2016-08-13 21:33:20
【问题描述】:
我已经搜索了很多,但找不到我理解到足以翻译到我的项目中的答案。目标是什么:我需要在列表中找到一个具有最高 armour_class 参数的物品,并将该物品的 armour_class 添加到角色的盔甲等级中。
所以,我们以这种方式创建了一个列表:
public List<Weapon> characterInvWeapon;
public List<Armor> characterInvArmor;
等等
以下是 Armor 类及其属性的创建方式:
public class Armor : Item, IComparable <Armor> {
public string armor_prof;
public string armor_category;
public int armor_class;
public int armor_str_req;
public string armor_stealth_mod;
public Armor (string c_name
, string c_description
, bool c_stackable
, int c_value
, string c_coin_type
, int c_weight
, string c_armor_prof
, string c_armor_category
, int c_armor_class
, int c_armor_str_req
, string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
{
armor_prof = c_armor_prof;
armor_category = c_armor_category;
armor_class = c_armor_class;
armor_str_req = c_armor_str_req;
armor_stealth_mod = c_armor_stealth_mod;
}
public int CompareTo(Armor other)
{
if (armor_class == other.armor_class)
return String.Compare (name, other.name); // a < ab < b
else
return other.armor_class - armor_class;
}
}
Armor 是一个继承自 Item 类的类,它具有前 6 个属性。 Armors 存储在一个特定于 Armor 的列表中 - public List<Armor> characterInvArmor;。
示例项目:
AddToItemStore(new Armor("Breastplate", "Description.", false, 400, "gp", 20, "Breastplate", "Medium Armor", 14, 0, ""));
添加脚本:
public void AddToCharacterInventory(Item it)
{
if (it is Weapon)
{
charInvWeapon.Add((Weapon)it);
charInvWeapon.Sort();
}
else if (it is Armor)
{
charInvArmor.Add((Armor)it);
charInvArmor.Sort();
}
}
现在正如我所提到的,我需要在列表 charInvArmor 中找到一个具有最高 armour_class 参数的项目,并在其他函数中使用它的值,该函数从许多变量中计算装甲等级。
所以在其他函数中,characterArmorClass = armorWithHighestArmorClass + otherVariable + someotherVariable; 等
我怀疑 Linq 中有一些方便的快捷方式,但我会非常感谢没有 Linq 的一些示例。 Linq 也会受到欢迎,但我对它完全陌生,例如,我担心性能和我的应用程序与 iPhone 的兼容性。我读过 iOS 会导致 Linq 出现问题。这必须是快速且兼容的计算。
【问题讨论】:
标签: c# sorting unity3d unity5 generic-list