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.

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

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

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?

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

1

if(npc[index] instanceofOrc)

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

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?

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:

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?

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 likeString 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.

Yeah. I love enums, but sometimes, I feel like it isn't enough. You can do enums, or you can make a static class like minecraft does for blocks. Like, every block extends Block, and all blocks are predefined objects in it, like this:

It really depends on what you want out of your program. Both are viable solutions, they both get the job done in different ways. However, you'll need an actual class to host the enum, so depending on how you want your project to be set up, you'll probably just need to go with static functions rather than an enum. If that makes sense! Typing on a phone is rather bad for going back over your post

Using enums, anonymous inner classes, etc all require learning new language feature(s) AND they don't scale very well and are pretty much only a minor step up from hardcoding (creating a sub-class for each monster). They can all be reasonable choices if the number of monsters is small, but why not do it one of the reasonable ways from the start?

Maybe I'm crazy but even for a beginner I'd suggest mimicking what you'd do in real life with a pencil-and-paper RPG. Let's say you have a green-and-pink polka-dotted dragon fighting 10 rabies-infested-vorpal-furry-bunnies. You look up the "bunnies" in your Monsterz Emanuel do you expect to see:

rabies-infested-vorpal-furry-bunnyIs a vorpal-furry-bunny with "And they have rabies!" appended to the description. "+2% of inflicting "rabies" per hit appended to the attack description, etc.

Now you go lookup: vorpal-furry-bunny and they define how they are different from "furry-bunny" which is defined how it's different from....

Nah, what you expect to see a complete description for pretty much every monster. Now when you proceed to play out he combat do you make 10 photocopies of the bunny description? Nope you jot down on a piece of paper the things that make each one unique (say its starting hit-points). You refer to the book when you need the definition and the piece of paper to track the changing information.

Monsterz Emanuel in code:

1 2 3 4 5 6 7 8 9

/** One instance of this class per monster defination (entry in Emanuel)*/publicclassMonsterDesc{intmaxHP;intar;intdr;intexp;Stringdesc;}

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org