digitalmars.D.learn - How best to implement items and weapons in my RPG. Nested classes?
- Liam McGillivray (48/48) Apr 03 For my tactical role-playing game, I want to finally implement
- Olivier Pisano (7/7) Apr 04 Hi,
For my tactical role-playing game, I want to finally implement items and weapons. These are objects that a `Unit` object can make use of. Just as tools are often said to be an extension of their user, I want items to be an extension of the unit who uses them. I'm trying to figure out how to best implement them. I already have an `Item` and `Weapon` class in [my repository](https://github.com/LiamM32/Open_Emblem), with the latter being a derived class of the former. But they are not used much, as they are not well-developed yet. [`unit` module](https://github.com/LiamM32/Open_Emblem/blob/master/source/unit.d) [`item` module](https://github.com/LiamM32/Open_Emblem/blob/master/source/item.d) Different weapon types would have different formulas to determine whether or not they can reach a certain tile, and different formulas for damage. Therefore, they would need their own functions. For example, a bow would have a function that checks for obstructions over it's range, while a sword would simply check whether the target is in range. There are even more functions for non-weapon items. I want each item to be able to return a list of actions that can be performed in it, for when it is selected in the menu. For weapons, this would be "equip", but also a list of moves that can be performed. If possible, I may want them to be able to access protected members of the `Unit` object that uses them. In order to pass a list of options to a menu, I'm thinking of having a `ItemAction` struct which contains a string for it's name (as would appear on the menu), and a delegate for the function that would be called if selected. Each item can have an array of these. For implementation of their unique functions, I've thought of two ideas. One is that the `Weapon` class will have many derivative classes, such as `Bow`, `Sword`, `Spear` etc, each with their own functions. The other idea is that there is only one weapon class, but it holds an array of functions or delegates for various moves, which would be given the appropriate functions for it's weapon type during construction. An advantage of the latter is that the weapon type can be determined within the constructor. Another idea I'm not sure of is whether the `Item` class and it's derivatives should be a nested class within `Unit`. If I understand correctly, objects of nested classes are always associated with an object of the outer class, and have access to it's protected and private members. This may be an advantage if I decide to set any of the required unit attributes to `protected`. If I don't make them nested classes, the functions in the `Item` class and it's derivatives would need to have either a reference to it's unit in the parameters, or the relevant attributes of the unit.
Apr 03
Hi, You should have a look at the decorator design pattern, it reduces the amount of classes to implement if you need to combine different effects such as elemental damage to your weapons (e.g. if you want flame arrows). https://en.wikipedia.org/wiki/Decorator_pattern https://refactoring.guru/design-patterns/decorator
Apr 04