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!
1 Answers
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.

- 119,250
- 27
- 256
- 336
-
This sounds feasible, currently implementing. Thanks for the extra info about how it's done in non-java languages. – Jimmt Sep 04 '13 at 05:45
MagicAttack (...)
andMeleeAttack (...)
. 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. – Andon M. Coleman Sep 03 '13 at 05:00