1

I'm building an RPG with JavaFX and need to get some advice from the experts.

What is the proper way to load certain resources? I'm not talking about images and sound, that part is easy. I'm talking about classes. For instance; I have like some odd 400+ abilities that you can activate. I have a separate class for each ability (or arte as I call them). To access this ability I want to be able to call

Data.getArte(idOfArte);

and this should return an object of type Arte. All of the artes have a separte class file.

There are other resources that are this way as well like Heroes, Enemies, and such. What would be the best way to load and call these resources for use? Is there a better way of doing this?

Edit: I'm also very concerned with performance.

Philip Vaughn
  • 606
  • 6
  • 20
  • @PhilipVaughn Can you clarify a bit more what you mean? You're actually just asking about creating objects, rather than loading classes, right? What do you mean in the comment below the current answer by "dynamically loading the *files* through getting all the files in a folder then casting them to a new class"? You can't cast a file to a class (you can cast an object reference to a class). How are you loading the "files" into a cache? – James_D Sep 24 '16 at 22:03

2 Answers2

3

A more efficient approach might be to use Entity Component System or at least borrow the composition design. This allows you to have a single concrete class, say Ability, that will contain generic fields common to all abilities, e.g. skill points cost, duration of ability, target types, activation types, etc. Then you would have a component for each special value you need to add and a control for each special behavior you need to add to that generic ability. Example:

Ability ability = new Ability();
ability.addComponent(new DurationComponent(double seconds)); // specify how long effect lasts
ability.addControl(new DamagingControl(int damage, Object targetType, etc.)); // so ability can damage
ability.addControl(new ElementAugmentingControl(Element element, Object weapon/armor, etc.)); // so ability can change status effects / elements

This should give you the idea of composition. Based on the common behavior of your abilities, you should end up with about 10-30 classes, while your 400 abilities simply become configurations of the base generic ability. To give you an example here's an RPG with roughly 100 abilities (skills) which are implemented as 6 classes. The same design can also be used with any game items / characters.

As for object creation you can do:

public static final int ABILITY_ID_SOME_NAME = 1000;    

ability.addComponent(new IDComponent(ABILITY_ID_SOME_NAME));

Then each of your abilities could be a part of a global data store, where only ability prototypes are stored:

Ability ability = DataStore.getByID(ABILITY_ID_SOME_NAME).clone();

Alternatively, make the data store return an already cloned ability so that you don't expose the prototypes.

Finally, you can consider using a scripting language, e.g. javascript, to change the behavior of the generic ability. In this case all of your abilities would be stored in a folder scripts/abilities/ which you load at runtime and only the ones you need. Some arbitrary example: (heal.js file)

function onUse(object, healValue) {
    if (object.hasComponent(HP_COMPONENT)) {
        val hp = object.getComponent(HP_COMPONENT);
        hp.value += healValue;
    }
}

Here's an article that shows how to call javascript functions inside java.

AlmasB
  • 3,377
  • 2
  • 15
  • 17
  • 1
    Sorry it took so long to accept an answer here (lol) I've been struggling with what to do but your answer intrigued me the most. I consider myself a veteran programmer (even having a CS degree) but have never come across anything quite like this (a shame really). It's going to take a bit of reworking of some classes but this (I believe) will be pretty efficient. – Philip Vaughn Nov 21 '16 at 20:24
1

You are looking for the Factory Pattern. I've found a good article about it here: http://alvinalexander.com/java/java-factory-pattern-example

I assume that you do not have to sideload class files at runtime? If that were the case I'd suggest to take a look here: Method to dynamically load java class files

Community
  • 1
  • 1
whitebrow
  • 2,015
  • 21
  • 24
  • Um... this is actually suppose to be a self packaged application (once it's done of course) no cloud based stuff going on here. – Philip Vaughn Sep 24 '16 at 21:11
  • I looked at the Factory Pattern example you gave and this was something I had thought of originally. I may need to implement something like this just because of the amount of files I have. Currently I'm already dynamically loading the files through getting all the files in a folder then casting them to a new class but I'm concerned about long term performance because I'm loading them all into a cache. – Philip Vaughn Sep 24 '16 at 21:24