Short preface
I am a part of a small team which is entering game development. We're creating a new RPG setting with custom game system and mechanics based on dice rolls. I'm server-side java programmer, so the game engine is my responsibility.
The problem
I have a problem designing talent classes. Each character may have a set of talents. The problem is, most talents are unique and different from others. For example:
Talent Season fever gives character a stat bonus depending on the season of the year, e.g. strength +2 in Winter, dexterity +2 in Spring, etc. Which gives player an opportunity to add and swap talents because of changed stats four times a year.
Talent Fortune Wheel allows a player to roll d6 each morning and get a bonus on one of the six stats (strength, dexterity, etc.) for that day.
Talent Attuned to partner gives a player +2 on his hit roll if his character has exactly one partner in the battle. If his partner also has this talent, both of them get +4 instead.
As you see mechanics of each talent are different. Question is how to universally implement all this different functionalities in Java classes.
Some technical details
I use latest version of JDK 7.
Springframework stack (spring-data, spring-mvc, spring-security, etc.)
MongoDB for persistent storage.
My ideas
The solution I came up with:
- Create a parent Talent class with generic fields and methods, like
id, name, description, requirements to obtain, etc. and an abstract
apply()
method. - For each talent in the game extend Talent class and add custom logic by implementing the
apply()
method. - I also need some trigger mechanism (I don't really know how it would look, so please advise if you have ideas). For example, if character has some talent which gives him bonus to a melee attack in some situations (preconditions) the trigger would fire on each event of melee attack made by the player, when the trigger fires it also checks if preconditions are met and if they are it calls the
apply()
method of a talent. - I have to implement scheduling for triggering or processing talents which depend on times or periods like the Season fever talent described above.
Disclaimer I have really small experience of asking questions on stack-exchange, so feel free to point out suggestions on question format and additional info required. Also, English is not my native language, so feel free to correct me if something annoys you (grammar-nazi style)
apply()
method and several methods. But your solution has an obvious flaw if I understood correctly. For now I can specify all triggers because I know all of them, but in the future (and maybe nearest one) other talents may be added or the engine may be used for another setting and talent triggers will change - some will be added, some removed, so I'll need to refactor the base class which I want to avoid. So one generic method implemented by subclasses of Talent is more appealing for now. \$\endgroup\$