I tried to be as clear as possibile in the title, but I'll be more specific to my situation.
I have a Player with some slots which can hold items. Each slot is a
Equippable is an interface just to group different items together: Armor, Weapon, etc. Now I come to face a problem: the Player has some parameters (e.g. Dexterity, Strength, etc.) and when an item is worn, it may affect one of these parameters. Up until now, I had simple items which added a bonus to a parameter, which was determined by the slot on which the item was (e.g.: Hands -> Strength, Feet -> Dexterity); so I just checked the bonus in the worn item and calculated the right parameter, when I needed it. But I need to change this: I need a more versatile way to handle this situation, because I don't want to bind the parameter to the slot and I cannot check every worn item anytime I need to get a parameter. Instead, I think I should be able to do the converse, i.e. update the correct parameter when an item is worn. I know this can take long to design completely and, of course, I am not asking this. I just need the right input because I am stuck.
To put it simply, I need something like: player wears item, does item give one bonus or more? If yes, apply bonus to the correct parameters. If not, do something else.
Possible solution: make an Update method which is called anytime an item is worn or removed. Cons: I must be careful in every place I should call this method. Also, this method should check what type of item it is, if it gives a bonus or more (and to what), or if it does something completely different.
Problem 2: This is related to the first problem, so I put it in the same question. Conceptually, I divided parameters in independent and dependent. The first ones just depend on the Player so they are stored in there, the second ones are calculated from the first ones and, possibly, from items (directly). Until now, I kept the dependent parameters only as properties, whose getter calculated the right formula when needed. So the dependent parameters are not stored, but only calculated as an "output" when required.
But introducting the items requires me to change those values, according to items. Here I used to follow the same reasoning as before: I checked the right item and calculated directly into the formula. Since now I need to change direction (as in problem 1), I need to update them anytime the correct item is worn.
Two possible solutions:
- Store a Bonus field and take it into account in the calculation. Pros: immediate change; Cons: I may need to store various different bonuses, ending with a mess.
- Store also the dependent parameters. Pros: I could use the same reasoning as in Problem 1, when solved. Cons: now, I get a stored parameter and not just the result of a calculation, so I need to change it anytime the independent parameters are changed. This probably can be solved as in Problem 1.
I hope you really can help me with this. Maybe I'm just overdoing and it can be solved with a simpler approach, or by doing a smart implementation of the items.
Addendum: I'll give your more context, as to how items are implemented. For now, they just contain a plain bonus (or more) and the slot in which they are placed determines the parameter. For example, an Armor gives me two bonuses which need to be added to ArmorClass (parameter) and another value involved in calculation. Since only one slot contains Armor, I know where to check. To avoid depending on the slot, I plan to include a key for each bonus which tells me what to modify with it. Also, other items (like rings, etc.) don't have three fixed values like Armor but have a variable number of them. I'll probably need a dictionary. So the operation will be: wear item -> check bonuses -> check corresponding parameter in key -> modify it in Player; and this is more precisely what I'm asking in Problem 1.