Remember that object oriented programming is usually using some form of data structure. It is probably easier to think of it in that format.
So... when creating an item. A common implementation is to just make an "onUse" function. Which... is ok, but kinda pointless. The item may have more than one effect. So... lets make it more interesting.
Class Item : {
private:
array<Action> ActionList;
public:
AddActions<Action>;
Use(Target, Target2 = NULL){
for( However many actions in action list)
ActionList.onUse(Target, Target2)
};
}
Here, our item has a target and an action list. The target is who the item is being used on. Of course you can always pass an array for multiple targets.
So...now the action list. What is an action list? If you are familiar with components, then it is basically the same thing. Action Lists are modules of code with data that is created on instantiation via factory or something.
First the interphase.
Class Action {
virtual update(Target, Target2 = Null) =0;
}
Next the implementation.
Class RestoreHP : Action {
private:
_healthRestore;
public:
RestoreHP(HealthRestore);
Override onUse(Target, Target2 = Null);
}
Class AddShield : Action {
private:
_shieldStrength;
_shieldDuration;
public:
AddShield(ShieldDuration, ShieldStrength)
Override onUse(Target, Target2 = Null);
}
When you make a new item. You add a series of Actions into it's array, and run through them.
For example... we want to make a heroic potion? Well The action list contains a heal and a shield module.
The benefit to the design is simplicity, easy to define archetypes. Easy to add more modules.
Also remember that you do not need to rebuild this class for each object of the same item. You only need to hold a reference, as the item class does not hold position.