In my game, I have a base class Loot, which has universal methods for everything that can be captured by the player and stored in his inventory. This will include potions, equipment, ammunition, etc. I can equip arrows, but not potions. Thus, Arrow will be a subclass of Ammo, which will ultimately be obtained from Loot. I can drink the potion, but not the arrow. Thus, Potion will subclass Loot, but implement IConsumeable. Etc.
Loot objects have the Quantity property (10 arrows, 2 potions). In my Loot class, I have a Split method that allows a player to take a βstackβ of items (like arrows) and split it into two separate stacks. Therefore, it reduces the number of Arrow instances by a certain amount, and then returns a new Arrow instance with the value Quantity = =, which was taken from the original instance.
My idea was that I would write a method in Loot, since any Loot can be stacked if its int StackLimit property is greater than 1. After decreasing the calling Loot by the specified number, I will return a new object of the same type. The problem is that I do not know what type of subclass Loot this object will have.
public abstract class Loot { public int Quantity { get; set; } public Loot Split(int quantityToTake) { Loot clone = (Loot)this.MemberwiseClone();
Is this really a bad way? I was thinking about reflection, but I hear mixed opinions about whether to use it in such a case.
Is there no way to determine a method for this. FurthestSubClass?
I know that my subclasses may have different constructors, so it is probably not possible to try and return "this this.FurthestSubclass ()" because I donβt know how to build it. But I would like to deal with its Loot methods, so I use Loot for the return type.
user164226
source share