Object oriented design of game in Java: How to handle a party of NPCs?

Posted by Arvanem on Stack Overflow See other posts from Stack Overflow or by Arvanem
Published on 2010-04-20T02:33:44Z Indexed on 2010/04/20 2:43 UTC
Read the original article Hit count: 375

Hi folks,

I'm making a very simple 2D RPG in Java. My goal is to do this in as simple code as possible. Stripped down to basics, my class structure at the moment is like this:

  • Physical objects have an x and y dimension.
    • Roaming objects are physical objects that can move().
      • Humanoid objects are roaming objects that have inventories of GameItems.
        • The Player is a singleton humanoid object that can hire up to 4 NPC Humanoids to join his or her party, and do other actions, such as fight non-humanoid objects.
        • NPC Humanoids can be hired by the Player object to join his or her party, and once hired can fight for the Player.

So far I have given the Player class a "party" ArrayList of NPC Humanoids, and the NPC Humanoids class a "hired" Boolean.

However, my fight method is clunky, using an if to check the party size before implementing combat, e.g.

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, am I on the right track to do this in as simple code as possible? Is there a better way?

© Stack Overflow or respective owner

Related posts about java

Related posts about object-oriented-design