问题描述
Imageine一个rpg游戏。
我有一个角色。
我的角色在名为Equipment
的课程中拥有装备插槽
这些插槽可以配备几种类型的物品:Armor
,Weapon
等)。
它们全都来自Item
类。
每个Item
都适合某个Equipment Slot
。
每个Equipment Slot
的类型为:
public enum EquipmentSlottype
{
Head,LeftHand,RightHand,BothHands,Chest,Waist,Legs,Feet,Neck,LeftRingFinger,RightRingFinger,}
我的Character
课没那么有趣:
public class Character
{
public string Name { get; private set; }
public Equipment Equipment { get; private set; }
}
这是我的角色Equipment
类:
public class Equipment
{
public EquipmentSlot Head { get; private set; }
public EquipmentSlot LeftHand{ get; private set; }
public EquipmentSlot RightHand { get; private set; }
public EquipmentSlot BothHands { get; private set; }
public EquipmentSlot Chest { get; private set; }
public EquipmentSlot Waist { get; private set; }
public EquipmentSlot Legs { get; private set; }
public EquipmentSlot Feet { get; private set; }
public EquipmentSlot Neck { get; private set; }
public EquipmentSlot LeftRingFinger { get; private set; }
public EquipmentSlot RightRingFinger { get; private set; }
private System.Reflection.PropertyInfo[] propertyInfos;
public Equipment()
{
Head = new EquipmentSlot(EquipmentSlottype.Head,false);
LeftHand = new EquipmentSlot(EquipmentSlottype.Head,false);
RightHand = new EquipmentSlot(EquipmentSlottype.RightHand,false);
BothHands = new EquipmentSlot(EquipmentSlottype.BothHands,false);
Chest = new EquipmentSlot(EquipmentSlottype.Chest,false);
Waist = new EquipmentSlot(EquipmentSlottype.Waist,false);
Legs = new EquipmentSlot(EquipmentSlottype.Legs,false);
Feet = new EquipmentSlot(EquipmentSlottype.Feet,false);
Neck = new EquipmentSlot(EquipmentSlottype.Neck,false);
LeftRingFinger = new EquipmentSlot(EquipmentSlottype.LeftRingFinger,false);
RightRingFinger = new EquipmentSlot(EquipmentSlottype.RightRingFinger,false);
propertyInfos = typeof(Equipment).GetProperties();
}
}
public bool EquipItemFromInventory(EquippableItem item,Character character)
{
// this needs fixing:
var x = propertyInfos.First(pi => pi.Name.ToString() == item.EquipmentSlottype.ToString());
var y = character.Equipment. .GetProperty(x.Name).SetValue(character,item.);
if (item.EquipmentSlottype == EquipmentSlottype.BothHands)
{
LeftHand.IsUsed = true;
RightHand.IsUsed = true;
BothHands.IsUsed = true;
BothHands.Item = item;
return true;
}
else
{
switch (item.EquipmentSlottype)
{
case EquipmentSlottype.Head:
Head.IsUsed = true;
Head.Item = item;
return true;
case EquipmentSlottype.Chest:
Chest.IsUsed = true;
Chest.Item = item;
return true;
case EquipmentSlottype.LeftHand:
LeftHand.IsUsed = true;
LeftHand.Item = item;
return true;
case EquipmentSlottype.RightHand:
RightHand.IsUsed = true;
RightHand.Item = item;
return true;
case EquipmentSlottype.Waist:
Waist.IsUsed = true;
Waist.Item = item;
return true;
case EquipmentSlottype.Legs:
Legs.IsUsed = true;
Legs.Item = item;
return true;
case EquipmentSlottype.Neck:
Feet.IsUsed = true;
Feet.Item = item;
return true;
case EquipmentSlottype.LeftRingFinger:
Feet.IsUsed = true;
Feet.Item = item;
return true;
case EquipmentSlottype.RightRingFinger:
Feet.IsUsed = true;
Feet.Item = item;
return true;
default:
return false;
}
}
}
}
我知道switch语句太大,应该重构。 我很确定我应该使用反射,但是我不知道该怎么做... 您可以在上方看到我的“结果”。
var x = propertyInfos.First(pi => pi.Name.ToString() == item.EquipmentSlottype.ToString());
这实际上似乎找到了正确的设备插槽。我传入了bow
,它是从Weapon
派生而来的,因此是从Item
派生的,在调试过程中,我看到x
的名称为BothHands
,这是正确的。 bow
具有EquipmentSlottype
中的BothHands
。
因此,我认为我有适当的选择,但是我无法弄清楚将什么传递给.SetValue。 它需要我提供一个对象,但是我无法弄清楚哪一个:(
我在方法Character
中添加了public bool EquipItemFromInventory(EquippableItem item,Character character)
,以便可以传递角色的实例。
有人可以告诉我如何使这种反射有效以及如何正确设置该值吗?
如果您需要更多信息,请告诉我。
非常感谢!!!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)