Author Topic: Is there a better way to do this?  (Read 17567 times)

Vanguard

  • Rogueliker
  • ***
  • Posts: 1112
  • Karma: +0/-0
    • View Profile
Is there a better way to do this?
« on: June 25, 2009, 01:24:48 AM »
Hello again, I'm back to ask for more advice on how I should do something.  This time I'm trying to find the best way to generate npcs.  The only things I can think of are something like this:

Npc npcArray[] = new Npc[numberOfNpcs];
npcArray[0] = new Bandit();
npcArray[1] = new Wizard();

and so forth.  Bandit and Wizard would both be subclasses of the Npc class.

Then in my method for generating the actual npc I'd need I'd have something like

public Npc npcGenerator(int npcNumber) {
     Npc chosenNpc = new Npc();
     chosenNpc.strength = npcArray[npcNumber].strength;
     chosenNpc.hp = npcArray[npcNumber].hp;
}

And so forth, setting all of the stats for the npc I'm generating match up with those of the npc type whose number I passed in.

The other thing I thought of doing is just a big switch statement like this:

Npc chosenNpc;
switch(npcNumber) {
     case 0:
          chosenNpc = new Bandit();
     break;
     case 1:
          chosenNpc = new Wizard();
     break;
}

I'm not totally happy with either of these though.  Is there a way to make it so the chosen Npc in the first example actually is a member of the Bandit or Wizard classes rather than just copying all of their values over?  I'm using Java if that makes any difference at all.

I'm still learning about all of this programming business.

Any help would be appreciated.

purestrain

  • Rogueliker
  • ***
  • Posts: 172
  • Karma: +0/-0
    • View Profile
Re: Is there a better way to do this?
« Reply #1 on: June 25, 2009, 06:48:52 AM »
Hello,

i would do something similar to the first approach. Let every npc be a "creature" (maybe keep npc, but i like to handle every living object the same, incuding the player) and create templates for specific ones. After creation you just have to create a clone.

E.g.
Templates.Add( new Creature("orc",100,10,10));
Templates.Add( new Creature("elf",50,40,40));
........
Creature concreteOne = CreatureFactory.Create("elf"); => this would do a lookup and create a copy.

maybe:
Creature.Clone()
{
   return new Creature(this.health, this......);
}

If you really want to have different classes for different NPCs, you should try the factory pattern. Basically each specific creature has a factory which instantiates the proper object.

e.g.
ICreature = CreatureFactory.Create("elf");
Now the creature factory has 10 concrete factories registered and create the proper object. Maybe use the chain of responsible pattern to check if the factory can handle the request.


BanditFactory : ICreatureFactory {
  bool CanHandle(string request) {
    return request == "bandit";
  }
  ICreature Create() {
    return new Bandit();
  }
}

Bandit : ICreature {
 ...
}


Just some thoughts

Anvilfolk

  • Rogueliker
  • ***
  • Posts: 374
  • Karma: +0/-0
    • View Profile
Re: Is there a better way to do this?
« Reply #2 on: June 25, 2009, 11:37:47 AM »
Also, instead of making every creature of the same type exactly the same, you could try using some random values, e.g. orc strength = 20 + 10*random(-1, 1). This would create orcs with strength uniformly between 10 and 30. You could also look into normal distributions which make a bit more sense!

That's just a tip :)
"Get it hot! Hit it harder!!!"
 - The tutor warcry

One of They Who Are Too Busy

Vanguard

  • Rogueliker
  • ***
  • Posts: 1112
  • Karma: +0/-0
    • View Profile
Re: Is there a better way to do this?
« Reply #3 on: June 25, 2009, 04:18:43 PM »
Let every npc be a "creature" (maybe keep npc, but i like to handle every living object the same, incuding the player) and create templates for specific ones.

I'm going to keep them all of the same "Actor" class, that was just for the sake of example.

Also, instead of making every creature of the same type exactly the same, you could try using some random values, e.g. orc strength = 20 + 10*random(-1, 1).

Yeah, I'm going to keep that option open in case I want to do that.

Edit:

I've never worked with templates like you have up there before.  Would it be possible to provide a quick explanation of how they work?  Like I said, I'm kind of new to programming.
« Last Edit: June 25, 2009, 11:51:00 PM by Curseman »