I'm making a 2d rpg game with libgdx and java, and I'm wondering how I would organize different attack systems - magic, melee, etc. (it's not a turn-based rpg). I have a PlayerClass enum, a Player2D class(2d representation), and a Player class (for storing player data). Would I make an Attack class, with MagicAttack and MeleeAttack? Then have a method in Player2D like attack(Attack attack)? I've considered a few options, but I want to be sure. Thanks!

  • \$\begingroup\$ I like the idea of actually having separate methods for MagicAttack (...) and MeleeAttack (...). You might pass in a specialized class or enum value depending on whether it's elemental/non-elemental, blunt/slashing/piercing/etc. to make resistance checks straight-forward. \$\endgroup\$ Commented Sep 3, 2013 at 5:00
  • \$\begingroup\$ I actually meant having MagicAttack and MeleeAttack be subclasses of Attack... \$\endgroup\$
    – Jimmt
    Commented Sep 3, 2013 at 5:07
  • 1
    \$\begingroup\$ Oh... I'm not so sure about that then, my thinking is that a lot of weapons have magical damage components in addition to physical and some spells do physical damage in addition to magical. It's difficult to handle this simply by subclassing in languages that don't support multiple inheritance. \$\endgroup\$ Commented Sep 3, 2013 at 5:18
  • \$\begingroup\$ ah, good point there. So an Attack class with methods for each type of attack? Where would I put this class? \$\endgroup\$
    – Jimmt
    Commented Sep 3, 2013 at 5:28
  • \$\begingroup\$ IMHO, MagicAttack and MeleeAttack are both attacks. A sword (for example) may have both, or may have a lot of attacks type: (melee, magical melee, magical distance, ranged (if throwing), etc..). What is not clear for me, it if they should be a parameter of the weapon: sword(melee=34, magic-dist:5, ..) or classes. \$\endgroup\$ Commented Sep 3, 2013 at 7:45

1 Answer 1


I would recommend you to create an abstract base class or interface Attack which defines a method perform(Combatant user, Combatant victim).

AttackMagic and AttackMelee would extend this class with their own implementations of this method.

The method would then extract all necessary attributes from the user and the victim, calculate how much damage the victim receives as well as any other consequences from using the attack, and update the victim accordingly.

In the project I am currently working on (an MMORPG server in Java) I did however use a different, very non-object-oriented, approach. I have a single class GameSystem which includes methods for calculating all combat mechanic rules. It has a static method performAttack(Combatant attacker, Combatant victim, Attack attack) with lots of if-else and switch statements for the different kinds of attackers, victims and attacks.

This is usually how you would do things in a procedural language like C, not in an object-oriented language like Java. My reason for doing it this way is that I wanted all game mechanic formulas in a single file and not spread over several, so that it's easier for me to find and change things during the balancing process.

  • \$\begingroup\$ This sounds feasible, currently implementing. Thanks for the extra info about how it's done in non-java languages. \$\endgroup\$
    – Jimmt
    Commented Sep 4, 2013 at 5:45

Not the answer you're looking for? Browse other questions tagged .