- Home /
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!
Answer by Hellium · Apr 26, 2020 at 09:13 AM
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...
Hi Hellium,
Thank you for your answer, great explanation.
Yet I have an additional question. By changing the variables to readonly
I can then only initialize them through constructor. But what if I have another variable public bool status
, which tells if it's locked or unlocked, should I just keep it as a public property that has both a getter and setter, as I will need to change it as game progresses?
As I said Collectable
is a pure data container. I don't believe it should contain anything related to "how" the data must be used. In your case, you can have another entity responsible for giving the status of the collectible in the appropriate situation.
Here are several examples of what I mean
public interface ICollectableStatusProvider
{
bool IsLocked(Collectable collectable);
}
public class CharacterCollectableStatusProvider : ICollectableStatusProvider
{
private Character character;
public CharacterCollectableStatusProvider(Character character)
{
this.character = character;
}
public bool IsLocked(Collectable collectable)
{
if(character.class == "$$anonymous$$age" && collectable.Type == ItemType.Sword)
return true;
if(character.class == "Warrior" && collectable.Type == ItemType.Wand)
return true;
return false;
}
}
public class InventoryCollectableStatusProvider : ICollectableStatusProvider
{
private Inventory inventory;
public InventoryCollectableStatusProvider(Inventory inventory)
{
this.inventory = inventory;
}
public bool IsLocked(Collectable collectable)
{
foreach(Item item in inventory.Items)
{
if(item.id == collectable.id)
return false;
}
return true;
}
}
public class OnGroundCollectableStatusProvider : ICollectableStatusProvider
{
public bool IsLocked(Collectable collectable)
{
return false;
}
}
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
How would i trigger sound on collision? 1 Answer
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
Hide selected object with button 0 Answers
Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer