I wrote a little program in C# that contains classes representing Items in RPG game. I wanted to have access to all inherited classes parameters from list contains parent Item class instances, so this is my code:
using System;
using System.Collections.Generic;
public interface SpecialObject
{
int GetIntParameter
{
get;
}
}
public interface ElementalsObject
{
int[] GetElementalsParameters
{
get;
}
}
public class Item
{
private string _name;
private int _cost;
public string name
{
get { return _name; }
}
public int cost
{
get { return _cost; }
}
public Item(string name, int cost)
{
_name = name;
_cost = cost;
}
}
public class Weapon : Item, SpecialObject, ElementalsObject
{
private int dmgs;
private int[] elementalsDmgs;
public int GetIntParameter
{
get { return dmgs; }
}
public int[] GetElementalsParameters
{
get { return elementalsDmgs; }
}
public Weapon(string name, int cost, int dmgs, int[] elementalsDmgs = null) : base(name, cost)
{
if (elementalsDmgs != null)
this.elementalsDmgs = elementalsDmgs;
else
this.elementalsDmgs = new int[] { 0, 0, 0, 0 };
this.dmgs = dmgs;
}
}
public class Armor : Item, SpecialObject, ElementalsObject
{
private int armorPts;
private int[] elementalsArmorPts;
public int GetIntParameter
{
get { return armorPts; }
}
public int[] GetElementalsParameters
{
get { return elementalsArmorPts; }
}
public Armor(string name, int cost, int armorPts, int[] elementalsArmorPts = null) : base(name, cost)
{
if (elementalsArmorPts != null)
this.elementalsArmorPts = elementalsArmorPts;
else
this.elementalsArmorPts = new int[] { 0, 0, 0, 0 };
this.armorPts = armorPts;
}
}
public class Food : Item, SpecialObject
{
private int _hungryRestorePts;
public int GetIntParameter
{
get { return _hungryRestorePts; }
}
public Food(string name, int cost, int hungryRestorePts) : base(name, cost)
{
_hungryRestorePts = hungryRestorePts;
}
}
class Equipment
{
public static void Test()
{
List<Item> items = new List<Item>();
items.AddRange(new Item[] { new Item("Stone", 1),
new Armor("Steel Helmet", 80, 10),
new Weapon("Great Sword", 120, 80),
new Armor("Storm Cuirass", 1000, 120, new int[4] {0, 0, 0, 60}),
new Weapon("Fire Spear", 1400, 60, new int[4] {0, 80, 0, 0}),
new Food("Apple", 5, 10) });
string[] el = new string[4] { "Water", "Fire", "Earth", "Wind" };
foreach(Item i in items)
{
Console.WriteLine("Name: " + i.name + ", Cost: " + i.cost);
if(i is SpecialObject)
{
//SpecialObject so = i as SpecialObject;
SpecialObject so = (SpecialObject)i;
Console.WriteLine(" Damages/ArmorPts/HungryRestorePts: " + so.GetIntParameter);
}
if(i is ElementalsObject)
{
ElementalsObject eo = i as ElementalsObject;
Console.WriteLine(" Elementals parameters: ");
for (int e = 0; e < el.Length; e++)
{
Console.WriteLine(" " + el[e] + ": " + eo.GetElementalsParameters[e] + ", ");
}
}
Console.WriteLine();
}
}
}
What do you think about it? Is it good code? If no then how I can write it better? :)