1

I am developing a game in which the user can turn on / off certain effects. These effects cause a drain on the users' energy and various parts of the program must be able to check if an affect is active. Currently, I'm using an enum type to store and check the effects:

public static enum Effects { SUPER_FIRE, FIRE_SHIELD }

if (someEffect == Effects.SUPER_FIRE) {
    // Breath fire etc..
}

Saying this, I have to store other variables for each effect - such as the level required to use it or the rate at which it drains energy. So, the other method I thought of was to use a class:

public class SuperFire extends Effect {

    public static int levelRequired = 10;
    public static int drainRate = 5;        

    public boolean active() {
        // Check if it's active
    }

    public boolean activate() {
    }

    public boolean deactivate() {
    }

}


SuperFireEffect sfe = new SuperFire();
sfe.activate();
if (sfe.active()) {
    energyLevel -= sfe.drainRate;
}
sfe.deactivate();

Which implementation (or any other) is the best for this situation?

jSherz
  • 927
  • 4
  • 14
  • 33
  • 1
    Use classes, object orientation allows you to put the logic inside the classes and inherit/override them instead of spreading it all around in the game code. – Karl-Bjørnar Øie Feb 15 '12 at 18:33
  • 1
    Keep in mind that [enums can have members and methods](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html). If you go with `enums`, "static" properties like `levelRequired` could be stored within the enum. As could [details about the implementation](http://stackoverflow.com/questions/3373847/business-logic-in-enums). – Rob Hruska Feb 15 '12 at 18:37
  • 1
    Also keep in mind that for true flexibility, storing information about your effects, including class names you can use introspection to instantiate, can be stored in various markup languages, like XML or YAML. – PlayDeezGames Feb 15 '12 at 18:43

3 Answers3

2

I hesitate to say "best" in any case, but it would appear that your second example is "better" in the "more flexible" meaning of the word.

Of course, from your very small code snippets, you are not encapsulating the functionality very well, so it would appear you may wish to do some more design work first.

In the end, you want to have the game code think in terms of the Effect base class and what it can do, and not have to know anything about the implementation of things like SuperFireEffect.

PlayDeezGames
  • 752
  • 4
  • 12
1

I would probably choose the second one as it is more "object-oriented" in my opinion. Plus if you start to add a lot of effects it will be more easily maintainable, and you can benefit from inheritance for super-effects.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
1

The 2nd implementation is better since you mention about the effects which are specified with their own set of features / properties / fields / attributes like "levelRequired, drainRate". So following this approach, you should well define your classes / entities and their features and common characteristics. Object Oriented Programming principles should be conveyed.

Juvanis
  • 25,802
  • 5
  • 69
  • 87