需求:在一件装备中,我们希望他具备基础属性,也希望他具备词条属性。这些属性都来自于角色类Character。基于反射的装备系统,可以有效的解决词条的随机性。
1、角色类
public class Character
{
public int Attack { get; set; }
public int Health { get; set; }
public int Defense { get; set; }
public string Name { get; set; }
public Dictionary<EquipmentType, Equipment> figtherEquipment;
public Character(string name,int attack, int health, int defense)
{
Name = name;
Attack = attack;
Health = health;
Defense = defense;
}
public void addEquipment(Equipment equipment)
{
Equipment outEquipment;
// 之前已经装备有装备
if (figtherEquipment.TryGetValue(equipment.Type,out outEquipment))
{
removeEquipment(outEquipment.Type);
}
equipment.WairEquipment(this);
figtherEquipment[equipment.Type] = equipment;
}
public void removeEquipment(EquipmentType type)
{
Equipment outEquipment;
// 之前已经装备有装备
if (figtherEquipment.TryGetValue(type,out outEquipment))
{
outEquipment.removeEquipment(this);
}
figtherEquipment.Remove(type);
}
}
2、装备类
public enum EquipmentType
{
Weapon,
Armor,
Gloves,
Shoes
}
public class Equipment
{
public string Name { get; set; } // 装备名字
public EquipmentType Type { get; set; }// 装备类型
// 属性名,是整数还是浮点数,整形值,浮点数值
public Dictionary<string, Tuple<bool,int,float>> BaseAttributes { get; set; }
public Dictionary<string, Tuple<bool,int,float>> ExtraAttributes { get; set; }
public Equipment(string name, EquipmentType type)
{
Name = name;
Type = type;
BaseAttributes = new Dictionary<string, Tuple<bool,int,float>>();
ExtraAttributes = new Dictionary<string, Tuple<bool,int,float>>();
}
// 将添加装备的基础属性
public void AddBaseAttribute(string attributeName, Tuple<bool,int,float> value)
{
BaseAttributes[attributeName] = value;
}
// 添加装备的额外属性
public void AddExtraAttribute(string attributeName, Tuple<bool,int,float> value)
{
ExtraAttributes[attributeName] = value;
}
// 为某个角色添加装备
public void WairEquipment(Character fighter)
{
string str=fighter.Name +"装备了"+Type+"\n";
// 遍历基础属性
Type type = fighter.GetType();
foreach (string key in BaseAttributes.Keys)
{
str += key+":";
PropertyInfo propertyInfos = type.GetProperty(key);
if (BaseAttributes[key].Item1 == true)//如果是整形值
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)+BaseAttributes[key].Item2);
}
else
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)+BaseAttributes[key].Item3);
}
str += propertyInfos.GetValue(fighter).ToString();
}
// 添加词条属性
str += "\n词条属性:\n";
foreach (string key in ExtraAttributes.Keys)
{
str += key+":";
PropertyInfo propertyInfos = type.GetProperty(key);
if (ExtraAttributes[key].Item1 == true)//如果是整形值
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)+ExtraAttributes[key].Item2);
}
else
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)+ExtraAttributes[key].Item3);
}
str += propertyInfos.GetValue(fighter).ToString();
}
Debug.Log(str);
}
// 为某个角色移除装备
public void removeEquipment(Characterfighter)
{
string str=fighter.Name +"移除装备"+Type+"\n";
// 遍历基础属性
Type type = fighter.GetType();
foreach (string key in BaseAttributes.Keys)
{
str += key+":";
PropertyInfo propertyInfos = type.GetProperty(key);
if (BaseAttributes[key].Item1 == true)//如果是整形值
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)-BaseAttributes[key].Item2);
}
else
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)-BaseAttributes[key].Item3);
}
str += propertyInfos.GetValue(fighter).ToString();
}
// 添加词条属性
foreach (string key in ExtraAttributes.Keys)
{
PropertyInfo propertyInfos = type.GetProperty(key);
str += key+":";
if (ExtraAttributes[key].Item1 == true)//如果是整形值
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)-ExtraAttributes[key].Item2);
}
else
{
propertyInfos.SetValue(fighter,(int)propertyInfos.GetValue(fighter)-ExtraAttributes[key].Item3);
}
str += propertyInfos.GetValue(fighter).ToString();
}
Debug.Log(str);
}
}
3、实现方式
Character player = new Character("yiyi",10,100,1);
Equipment equipment = new Equipment("普通武器",EquipmentType.Weapon);
equipment.AddBaseAttribute("Attack",new Tuple<bool, int, float>(true,10,0f));
player.addEquipment(equipment);
player.removeEquipment(equipment);
player.addEquipment(equipment);
通过上面的方式,我们可以任意定义里面装备的属性,不用再重新定义属性。使得词条可以局限在角色的属性中。有空会为大家带来这种模式下套装功能的实现。如有不足,欢迎一起交流。