Witam, napisałem kod który ma na celu stworzenie przedmiotu a następnie nadanie mu właściwości poprzez komponenty, co o nim sądzicie? Można to jakoś lepiej napisać?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public interface ItemComponent { };
public abstract class PhysicalItem
{
public int Slashing { get; set; }
public int Bashing { get; set; }
public int Piercing { get; set; }
}
public abstract class ElementalItem
{
public int Fire { get; set; }
public int Water { get; set; }
public int Earth { get; set; }
public int Wind { get; set; }
}
public class Valuable : ItemComponent
{
public int Cost { get; set; }
public override string ToString()
{
return
string.Format("It is a valuable item, that can be bought for: {0} gold coins.", Cost);
}
};
public class PhysicalProtection : PhysicalItem, ItemComponent
{
public override string ToString()
{
return
string.Format("It gives {0}/{1}/{2} physical protection.", Slashing, Bashing, Piercing);
}
};
public class PhysicalDamage : PhysicalItem, ItemComponent
{
public override string ToString()
{
return
string.Format("It does {0}/{1}/{2} physical damages.", Slashing, Bashing, Piercing);
}
};
public class ElementalProtection : ElementalItem, ItemComponent
{
public override string ToString()
{
return
string.Format("It gives {0}/{1}/{2}/{3} elemental protection.", Fire, Water, Earth, Wind);
}
};
public class ElementalDamage : ElementalItem, ItemComponent
{
public override string ToString()
{
return
string.Format("It does {0}/{1}/{2}/{3} elemental damages.", Fire, Water, Earth, Wind);
}
};
public class Item
{
public string Name { get; private set; }
private List<ItemComponent> components = new List<ItemComponent>();
public Item(string name)
{
Name = name;
}
public Item AddComponents(ItemComponent[] components)
{
this.components.AddRange(components);
return this;
}
public T GetComponent<T>()
{
return (T)components.OfType<T>().FirstOrDefault();
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendLine(Name);
components.ForEach(c => sb.AppendLine(c.ToString()));
return sb.ToString();
}
};
class Equipment
{
public static void Test()
{
var fireSword = new Item("Fire Claymore")
.AddComponents(
new ItemComponent[] { new Valuable { Cost = 1000 },
new PhysicalDamage { Slashing = 100 },
new ElementalDamage { Fire = 80 },
new PhysicalProtection { Bashing = 25 },
new ElementalProtection { Water = -25 }
});
var ancientHelmet = new Item("Ancient Helmet")
.AddComponents(
new ItemComponent[] { new Valuable { Cost = 700 },
new PhysicalProtection { Slashing = 200 },
new ElementalProtection { Fire = 20 } }
);
Console.WriteLine(fireSword);
Console.WriteLine(ancientHelmet);
}
};