I'm creating an inventory system C# and I'm worried about the following code
//===========================Item Type Enumeration===========================//
public enum ItemType
{
NONE,
UKNOWN,
ROSE,
SALT_BULLET,
SHOTGUN,
WELL_BUCKET,
WELL_ROPE,
WELL_PUMP,
LIBRARY_BOOK,
KEY
}
//===========================Item Type Enumeration===========================//
//===============================Game Item Base==============================//
public abstract class GameItemBase
{
public ItemType m_ItemType; //What type of item is it?
public int m_MaxStacks; //Maximum stacks.
public bool m_IsQuestItem; //Is a quest item?
public bool m_CanBeHold; //Can the player hold it in his hands?
}
//===============================Game Item Base==============================//
//===============================Game Item None==============================//
public class GameItemNone : GameItemBase
{
public GameItemNone()
{
m_ItemType = ItemType.NONE; //What type of item is it?
m_MaxStacks = 0; //Maximum stacks.
m_IsQuestItem = false; //Is a quest item?
m_CanBeHold = false; //Can the player hold it in his hands?
}
}
//===============================Game Item None==============================//
But most importantly:
public GameItemBase GenerateGameItem()
{
switch (InventoryItemType)
{
case ItemType.NONE:
return new GameItemNone();
case ItemType.UKNOWN:
return new GameItemUknown();
case ItemType.ROSE:
return new GameItemRose();
case ItemType.SALT_BULLET:
return new GameItemSaltBullet();
case ItemType.SHOTGUN:
return new GameItemShotgun();
case ItemType.WELL_BUCKET:
return new GameItemWellBucket();
case ItemType.WELL_ROPE:
return new GameItemWellRope();
case ItemType.WELL_PUMP:
return new GameItemWellPump();
default:
return null;
}
}
In order to instantiate a GameItem, I have to switch through the entire ItemType enum.
Is there a way to automate this functionality? For Long Term Support purposes, this will get out of hand if I have lots of different game items, for example, if I'm making an RPG game.
Can I use templates to automate this somehow?