Object Oriented Game Design in Java: How to handle an NPC batch?

I am making a very simple 2D RPG in Java. My goal is to make this as simple as possible code. Divided into the basics, my class structure at the moment looks like this:

  • Physical objects have x and y dimensions.
    • Roaming objects are physical objects that can be moved ().
      • Humanoid objects are roaming objects that have GameItems reserves.
        • A player is a single-user humanoid object that can hire up to 4 NPCs of Humanoids to join his or her side and perform other actions, such as fighting non-humanoid objects.
        • NPC Humanoids can be hired by a player's object to join a party, and once hired can fight for a player.

So far, I have given the Player class a "member" ArrayList from NPC Humanoids, and the NPC Humanoids class has "hired" a logical one.

However, my battle method is awkward, use if you need to check the size of the party before the start of the battle, for example.

public class Player extends Humanoids {
   private ArrayList<Humanoids> party;
   // GETTERS AND SETTERS for party here
   //...

   public void fightEnemy(Enemy eneObj) {
      if (this.getParty().size() == 0)
        // Do combat without party issues
      else if (this.getParty().size() == 1)
        // Do combat with party of 1
      else if (this.getParty().size() == 2)
        // Do combat with party of 2
      // etc. 

My question is, thinking in object-oriented design, I'm on the right track to make this as simple as possible, as much as possible? Is there a better way?

+3
source share
4 answers

Well, forgetting the general design, from a programming base point, instead of having a structure if, you should have a method that takes a batch size as an argument. That way you can just go in this.getParty().size()and get rid of ifs.

i.e.

  combatManager.fight(this.getParty().size(), eneObj);

combatManager - ( , ), , fight.

, , if s.

Player , - , , , fightEnemy engageEnemy - , Manager .

+2

" , , - , , ?"

, , , .

x y ().
[- - , ]
- , . - singleton ,
[ ] 4 NPC Humanoids

, - , . has-a/ is-a.

, , , . , , ? ? , , -. , ; . , , .

, : . , position:: move(). , NPC, - - , , - , , doesn ' t . NPC? , , NPC NPC.

, , 4 ( 5) ? Zero, One, Infinity , , . "" , .

, - . . - , , , .

+7

, . , , , , .

0

. NPC . NPC? , NPC ( Humanoid) AI, NPC NPC, .

0

Source: https://habr.com/ru/post/1741875/


All Articles