Creating one class or using polymorphism?

Hi all,

So I’m trying to make a 2D game where I have 50 unique collectables(items). These collectables are obtained by the player under certain circumstances, ex: defeating monsters, in-game events…etc.

Upon getting these collectables, the icon of it will appear on the “show room” panel, and the status of it becomes “unlocked”.

These collectable has no real interactions with the game, they only contains some data like an example below:

 public class Collectable
 {
       public int id {get;set;}
       public string name {get;set;}
       public ItemType type {get;set;}
       public int rarity {get;set;}
       public float bonus {get;set;}
 }

Notice that “bonus” is the bonus given to the player in some in-game events, ex: 0.2 additional chance to obtain Collectable XXX upon defeating monsters.

Now I have two questions:

-Should I create only one class like above then assign the values to it at runtime upon creation, or, create a Collectable base class and then create other 50 classes that inherit from the base class? Or if there’s any better way you suggest?

 //Option 1:
 public void CreateCollectables()
 {
          collectablelist.Add(new Collectable(){1,"item1", ItemType.Battle,1,0.1});
          collectablelist.Add(new Collectable(){1,"item2", ItemType.Type1,1,0.1});
          collectablelist.Add(new Collectable(){1,"item3", ItemType.Type2,1,0.1});
          ...
          ...
          collectablelist.Add(new Collectable(){1,"item50", ItemType.Event,1,0.1});
 }
 
 //Option 2:
 public class Collectable1 : Collectable{
         public Collectable1()
        {
                id = 1; 
                name= "item1" 
                ...
         }
 }
 //then
 public void CreateCollectables()
 {
          collectablelist.Add(new Collectable1());
          collectablelist.Add(new Collectable2());
          collectablelist.Add(new Collectable3());
          ...
          ...
          collectablelist.Add(new Collectable50());
 }

-If I were to map a collectable to a slot in the panel, what would be the ideal structure?

 public class Collectable
 {
 ...
 CollectableSlot slot;
 }
 //or?
 public class CollectableSlot
 {
 ...
 Collectable collectable;
 }

Any comments are welcomed, thank you!

For your first question, if only the data change (and not the behaviour), then there is absolutely no reason to have multiple classes of Collectables. Collectable is a pure data container, nothing more.

I would advise you to have a constructor for this class and only readonly public variables. (or pure getters)

For your second question, I would clearly go for the 2nd example. Again, your Collectable is a pure data container and should not be aware of how it will be used. You might want to have a 3D model representing the collectable on the floor, have this collectable displayed in an inventory, have a collectable displayed on a reward screen, etc…