基本上,我正在创建一个伪 RPG 游戏,其中玩家拥有元素 list 和角色娃娃(以了解当前装备了哪些元素)。
您建议处理当前装备元素收集的最佳方法是什么?
我当前拥有的是一个 EquipmentSlot 枚举,其中包含玩家上装备元素的所有可能位置,我可以将其设置为玩家拥有的每个元素的属性。
public enum EquipmentSlot
{
Head,
Chest,
Arms,
Legs,
Feet,
OffHand,
MainHand
}
然后我有一个字典,其中包含每个枚举作为键,在 Player 构造函数中将它们全部初始化为 null:
PlayerEquipment = new Dictionary<EquipmentSlot, Item>(7);
PlayerEquipment.Add(EquipmentSlot.Head, null);
PlayerEquipment.Add(EquipmentSlot.Chest, null);
PlayerEquipment.Add(EquipmentSlot.Arms, null);
PlayerEquipment.Add(EquipmentSlot.Legs, null);
PlayerEquipment.Add(EquipmentSlot.Feet, null);
PlayerEquipment.Add(EquipmentSlot.OffHand, null);
PlayerEquipment.Add(EquipmentSlot.MainHand, null);
但是当我编写这个代码时,我开始意识到它不起作用,因为我无法在 Player 的其他方法中以枚举形式访问键,因为它们是添加在构造函数中的。我不确定还可以在哪里添加它们,以便类的其他人也可以使用它们。
我的字典方法是不是解决这个问题的错误方法?
最佳答案
我不确定你的意思:
I can't access the keys as enumerations in other methods of my Player, because they are added in the constructor
但是为什么不...
public class Player
{
public Item EquipmentHead { get; set; }
public Item EquipmentChest { get; set; }
public Item EquipmentArms { get; set; }
public Item EquipmentLegs { get; set; }
public Item EquipmentFeet { get; set; }
public Item EquipmentOffHand { get; set; }
public Item EquipmentMainHand { get; set; }
}
附注我不会说您的 Dictionary 实现方式错误,这只是您可能能够更轻松地理解的另一种选择。
关于c# - 如何持有伪RPG玩家装备的有限收藏元素(字典?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10729755/