【发布时间】:2012-05-24 00:41:32
【问题描述】:
基本上,我正在创建一个伪 RPG 游戏,其中玩家拥有物品清单和角色玩偶(以了解当前装备了哪些物品)。
您建议处理当前装备物品集合的最佳方式是什么?
我目前拥有的是一个 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 的其他方法中将键作为枚举访问,因为它们是在构造函数中添加的。我不确定我还可以将它们添加到哪里,以供全班其他人使用。
我的字典方法是不是错误的方法来解决这个问题?
【问题讨论】:
-
“但是当我编写这个代码时,我开始意识到它不起作用,因为我无法在我的播放器的其他方法中访问密钥作为枚举,因为它们是在构造函数中添加的。”当然可以,你总是可以使用 PlayerEquipment[EquipmentSlot .Head] 来获取当前的 Helm 或者使用这条线来设置当前的 helm。总的来说,我认为这是一个合理的方法。
标签: c#