I finally managed to plug my menu to my inventory system, display current items, use them etc. But the way I did it... makes me feel dirty.
To cut short, my items are described this way:
using System;
public abstract class Item {
public abstract string Name { get; }
public abstract string Description { get; }
public virtual bool IsUsable { get { return false; } }
public virtual bool IsEquippable { get { return false; } }
public virtual bool NeedTarget { get { return false; } }
public virtual void OnUse() { }
}
public class Potion : Item {
public override string Name { get { return "Potion"; } }
public override string Description { get { return "Restores 100HP to one character"; } }
public override bool IsUsable { get { return true; } }
public override bool NeedTarget { get { return true; } }
public void OnUse(PlayerCharacter target) {
Effects.HealTarget(target, 100);
}
public override void OnUse() {
OnUse(GameData.getFirstPc());
}
}
public class Poison : Item {
public override string Name { get { return "Poison"; } }
public override string Description { get { return "Deals 100HP damage to one character"; } }
public override bool IsUsable { get { return true; } }
public override bool NeedTarget { get { return true; } }
public void OnUse(PlayerCharacter target) {
Effects.DamageTarget(target, 50);
}
public override void OnUse() {
OnUse(GameData.getFirstPc());
}
}
My Inventory is declared like this:
public class Inventory {
private static Dictionary<string, Item> ItemDB = new Dictionary<string, Item>() {
{ "00001", new Potion()},
{ "00002", new Poison()},
{ "00003", new HiPotion()},
{ "00004", new FangOfDestroyer()},
{ "00005", new Elixir()}
};
public static Dictionary<string, int> CarriedInventory = new Dictionary<string, int>();
}
When I want to add items to CarriedInventory I basically call CarriedInventory[itemID] = newAmount
Everything only uses the ID, and when I want to get an item's name I call ItemDB[itemID].Name, when I want to use an item I call ItemDB[itemID].OnUse() , etc
My reasons for doing this were:
- I can be as specific as I want on item uses as I want, I can have unique item effects without having to worry about extending it to all other items
- I can use the effects in the Effects class for general effects (heal, damage, etc)
- if later on I realize I need a new property for an item, I can just add a virtual property as default to the Item class (e.g. I realize I want an item to not be droppable, I add a public virtual bool IsDroppable with a value of true, and non-droppable items can override this
- I avoid inheritance hell
And... it works, it works, but it feels wrong.
Is it wrong? How wrong?
Is there a better way to do it?