OOP Noob - Extending Classes, or How Else to Create Unique NPCs?

Hello,

I’m just now getting started in OOP after doing everything procedurally in a variety of languages since QBASIC. I’m working through the concepts explained in the Java book I’m reading but instead of creating the pointless business apps they want me to do, I’m trying to make simple text-based games.

So I’m writing a cage-fighting app right now, where I define an NPC class, create two identical NPC objects, and then have them hack away at each other to see whose hitpoints drop below 0 first. More of a simulation than a game.

Right now I’d like to modify it so that there are certain types of NPC. For instance humans, orcs, elves, etc, all with different stats. The next step after that will be creating an arbitrary number of NPCs and have all of them fight each other at the same time. So what I do now needs to be scalable.

What’s the ideal way of going about this? I was thinking about extending the NPC class to include base stats, then overriding them with race-specific values via extension. The only problem is, right now I’ve got it hardcoded so that “nextEnemy = new Fighter();” but I don’t know (yet) how to refer to the object that extends Fighter as anything other than Fighter (i.e. if it’s an Orc, it’s already hardcoded to refer to “new Fighter()”, not “new Orc();”).

Am I supposed to create some kind of container object and use a loop to just have the player attack anything contained within?

I’d appreciate any direction you all could give me as I try to get better at this.

Thanks!

ok so I think what you need to do is either define different classes within the Fighter class like having different stats for different ids. This would allow one class but is probably the least efficient option

Or you can create separate classes that extend Fighter()( so you can get the variable) but you change the stats. This way you could just call nextFighter= new Orc() and it would still be counted as a Fighter

ps: mincraft kinda does the second one for blocks

I think you’re over complicating it.

What you need to do is have a base NPC class that has basic stats like health, weapon damage, defense etc… Then just have sub classes such as orc that extend NPC, and override those stats. The NPC class should only be handling the basics like rendering, updating, moving and attacking, but it should not be setting any stats. You need to super the stats up to the NPC class from sub classes. That way you can just have a array of NPCs, and loop through them all, and you can just see if a certain object in the array is an orc by saying

if(npc[index] instanceof Orc)

A Fighter object can be dynamic, so you can have some sort of condition statement that dynamically sets the fighter to be any one of the Fighter subclasses you create.

So:

Fighter fighter;

if ( <something> )
    fighter = new Orc();
else if ( <something> )
    fighter = new Elf();

That’s kind of crude, but should get the idea across. I think you should supply more code so we can get a better idea of what you are asking.

Ha! That’s how I imagined the code in my head, but just couldn’t come up with the syntax for it.

Am I correct in my understanding of your example that “fighter” basically becomes a container object for any given type of NPC? Thus, regardless of which condition is chosen in your example, I should be able to call fighter.getStrength() and it will show me either the strength value for an orc or elf according to their extended class?

I think that solves my next problem. I’ll plan on using arrays to keep track of multiple fighters and loop through it. Thanks!

[quote]Am I correct in my understanding of your example that “fighter” basically becomes a container object for any given type of NPC? Thus, regardless of which condition is chosen in your example, I should be able to call fighter.getStrength() and it will show me either the strength value for an orc or elf according to their extended class?
[/quote]
This is true. No matter what, since your subclasses such as orc extend fighter, you’ll be able to call something like getStrength. Your sub classes inherit the functions that the super class has, so for instance, say Fighter looks like this:


int strength, hp;

public Fighter(int strength, int hp){
      this.strength = strength;
      this.hp = hp;
}

public int getStrength(){
       return strength;
}

And then orc:


class Orc extends Fighter

public Orc(){
       //super gives the super class the paramters it needs for its constructor
       super(20, 100);
}

public int getStrength(){
//references the getHealth function in super class Fighter
       return this.getStrength();
}

Of course, this isn’t the best way of doing it as you don’t entirely need that getStrength function in the Orc class. I was just trying to show you how sub classes work!

Of course. My understanding is that the getStrength method should be in the super class, and only the race-specific stat tweaks in the sub classes.

Thanks for confirming all of this; I really appreciate it.

No problem, just as tip, the appreciate button gives the use medals, which is a very nice reward :slight_smile: No pressure, but if anyone helps you, and you feel grateful, just hit that button!

And yeah, I was just trying to show you about sub classes and inheritance. That was kind of a poor example on my part, but I think you know what I was trying to say!

Hey, I’m all for giving props where they’re due, but I’m not seeing an appreciate button anywhere. Is that something I need to have a minimum post count/age to be able to do or am I just overlooking it?

try far right-top on a post

Yeah I only have the option to Reply or Quote on others’ posts. I’ll revisit this question later and give credit where it’s due.

Thanks again all.

Ah, yes you need to post more on the forums before you can appreciate anything!

Looks like the option just popped up. I guess the magic number was 5 posts. I gave each of you a karma and it’s not letting me do it any more for an hour, so there you have it :wink:

Here, have your first medal, courtesy of me! :slight_smile:

Enums anyone?

In addition to what the other said (which were all great)

Also depending on how might flexibility you might want.

You might consider having some rand.nextInt(40)+80; for health or something, to give some ‘variety’ to your particular Orc.

you can also, depending if you have their name anywhere, you can have a prefix list like
String prefix[] = {“Mighty”, “Strong”, “Evil”, “Dastardly”, “Weak”};
name = prefix[rand.nextInt(5)] + " Orc";

So you have different Mighty Orcs! Or other variations

If your game is visual, you can add a very simple filter/shade to your texture to give it a slight color hue/tint(many libraries have a way of handling this quite easily) or scaleXY to make them slightly different in size. Depending upon your game.

This may be a bit advanced or a jump to get into, but a component-system based approach is quite efficient, from my own experience. Essentially, each entity is simply an ID that’s assigned predefined component classes - like health, or damage, or movement - that another class, a system or handler, takes into account to make the entity do what you want it to.

The one I like currently is the Artemis Entity System. http://gamadu.com/artemis/

composition over inheritance.

For this type of game, yeah, but let’s not try to confuse him too much ;D

What’s confusion about using data rather than hardcoding? It’s easier to write, maintain and understand. There’s not much need even for composition…just be data driven.