Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question
1  
Your solution sounds reasonable. You might consider, instead of having a single apply() method, having a few methods that get called at different points. E.g., if a skill is triggered on an attack, you call the attack() method. If it's triggered when a season changes, you call the season() method. Within the talent itself, if it doesn't care about the season() method, it simply doesn't bother to override it, and the base class has a "do nothing" type implementation. –  Cameron Fredman Feb 18 '13 at 21:12
    
Advantage of the approach I described is that you can call all the talents in effect at all the various times a talent can be triggered and then let the talent take care of whether or not it cares about that event. –  Cameron Fredman Feb 18 '13 at 21:13
    
How crazy can talent effects be? Here's one that will break most designs: "Whenever the total number of vertices rendered since the beginning of the game is divisible by 54235, roll a d6. If 1, this talent becomes another random talent if the phase of the moon is within 5 hours of a full moon." –  Anko Feb 18 '13 at 23:04
    
@CamFred, I'll start prototyping and implement both cases - one 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. –  Czar Feb 19 '13 at 8:12
1  
For the Triggers you can use a Rules Engine. Google for 'Drools'. You will have to Build a Knowledge Base with all information what can affect a trigger. Then you write Rules to match against this Knowledge Base. If you have a rather static Knowledgebase These Engines can be inceddibly fast. –  HaBaLeS Nov 7 '13 at 13:34

1 Answer 1

up vote 1 down vote accepted

What CamFred said in his comments is sensible. If you want more suggestions, here's one.

Use an observer/observable pattern. Make the Talent class an Observer, and the character an Observable. Now, adding a Talent to a character is simply a matter of adding an observer. Whenever something happens that might trigger an event, you notify the observers, and they decide whether anything happens.

This is really easy to do in Java - it actually has built-in Observer- and Observable-classes that you can extend. Anyway, here's the Wikipedia page - there's even a Java example.

share|improve this answer
    
Basically this is what I meant. I was just thinking in terms of events, listeners and triggers. –  Czar Feb 19 '13 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.