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!
I would recommend you to create an abstract base class or interface
Attack which defines a method
perform(Combatant user, Combatant victim).
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
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.