mardi 3 mai 2016

The best way to create oject hierarchy for inventory prototype

1. Item database

I am making inventory with c#. Usually i write programs with python and that's why i have some design problems with statically typed c#. Because of lack of practise.

I've created simple Item database. I have a general class Item. It has general properties like id, 'name'... And to make new types of ojects I inherite from Item class. For example Weapon item. Here is the code

Item.cs

public class Item {
  public string id;
  public string name;
  public string prefab;
  public string icon;

  public virtual string getTooltipText() {
    return "";
  }
}

Weapon.cs

public class Weapon : Item {
  public int damage;

  public override string getTooltipText() {
    return "damage: " + damage;
  }
}

As you can see every time i need to create new type of object, i need to create new class inherited for Item

All items I load to dictionary and it works awesome

public Dictionary<string, Item> Items = new Dictionary<string, Item>();

And create item class in terms of item type from database file

2. Question Part. Inventory

Right now I want to create an inventory using my Item database from the first part of this question. All items have general properties in user inventory. For example - quantity and ref to item from Item database. But main problem is that many items have their own unique stats. For example durability, speed, quality etc.

How can I design inventory item?

First idea

First idea (and it is ugly) is to create BIG class with all stast for all types of objects

public class InventoryItem {
  //main properties
  public Item item;
  public int quantity;

  //all properties for all other items
  public int durability;
  public int speed;
  public string quality;
  public string magic;
  ...
}

And I have to fill with zero and null all unused properties in terms of Item type. It is ungly and i don't like this way of making InventoryItem class

Second idea

Like in my Item database I can create separate class for every inventory item type: InventoryWeapon, InventoryPlant, InventorySpells etc.

And it looks better than BIG BOSS SINGLE InventoryItem class.

But I have feeling that I am doing something wrong. I am making double work. Every time i need to create new type of object, I have to create new Item in item database (for example public class Plant : Item) and i have to create new InventoryItem (for example public class InventoryPlant : InventoryItem)

My question is: The best way to create oject hierarchy for inventory prototype?

The best design?

Or maybe what I am doing wrong?

Or my idea with class for every type is right way of doing inventory?

Aucun commentaire:

Enregistrer un commentaire