creating many enemies from the same class
Hi everyone,
Im currently making a fullscreen 2D RPG that loads the maps from .txt files. Everything is working well so far, you can explore the map, collision detection is satisfactory.
My problem now is trying to figure out how to handle creating the enemies, creatures etc. I want to have a certain letter in the .txt file (eg: O - standing for Orc). When the hero walks into an area of the map where this 'O' is visible, an Orc is created. This is where my problem starts. How do I handle having more than one Orc walking around trying to bite my arm off?
My only idea is to create an instance of an Orc class whenever an 'O' is encountered and pass the orcs XY coordinates to the constructor. I would then add this orc to an arraylist. I could then update each creature in the arraylist on every iteration through the gameloop. Something like this:
Orc enemyOrc;
ArrayList enemyList =new ArrayList();
privatevoid createOrc(int x,int y)
{
enemyOrc =new Orc(x, y);
enemyList.add(enemyOrc);
}
Is this a feasable way of doing it since all the orc instances are going to have the same name (ie: enemyOrc) so will they still all be different objects with different xy values so that even if there are 10 Orcs on the screen, they can all be updated seperatly?
Another question is what do I do when an Orc dies? I will need to remove that specific Orc instance from the arrayList but how will I remove the correct one? Then what do I do with that Orc? will automatic garbage collection dispose of it since it will no longer be in the arrayList and will hopefully not have any more references to it?
I hope ive explained my problem correctly. Please let me know if im on the right track or alternativly, how should I approach doing this?
Thanks, much appreciated
R
[2139 byte] By [
RisseN] at [2007-9-30 7:34:47]

Hi RisseN,
By and large your approach seems to be correct. You need to keep a list of the critters on the map at any one point, and an arraylist is a good way to handle it.
If all orcs have the same health, equipment etc..., the only thing you need to pass in your constructor is indeed the location. You'll have different orcs with unique locations. When an orc dies, you can remove it from the List and it will be garbage-collected.
I'm not sure having the monsters be part of your map data is a good idea, though. Say that later on in the game, you'd like to have a few varieties of monster in the game, then you'd quickly run out of symbols. Likewise, you'll run into trouble if at one point you'd like to have different statistics for monsters.
Another issue you may have with defining monsters on the map is that of random encounters. After some time in the game, every orc is going to be killed by the player and the map will wind up quite empty. It'd be nice to have a mechanism in place which plops a random creature on the map from time to time.
You may want to keep lists for various items
monsters
treasure
players
specials (fountains etc)
Then include coordinates in each object.
That seems most logical and could extend movement, ie moving all ten orcs (just iterate through a list)
But you could go other ways too... if you just have a huge list of everything by coordinates then just add it to that list.
thanks for the advice. I think im going to use some sort of basic algorithm which will randomly place the creatures on the map so I dont have to add them to the .txt files and then keep them in a list as mentioned.
Is it correct in the way I was going to create several instances using the same name? So for example if I wanted to create 10 orcs then would this be correct:
Orc enemyOrc;
for (int i=0; i<10; i++)
{
enemyOrc = new enemyOrc(i, i);
}
Will I now have 10 different orc instances which all have the same name?
Thanks again, dukes on their way
R
Orc enemyOrc;
Random rando = new Random();
for (int i = 0; i < 10; i++) {
enemyOrc = new enemyOrc(rando.nextInt(mapWidth), rando.nextInt(mapHeight));
list.add(enemyOrc);
}
That'll do it. You can't forget to add the Orc to your list each time you create one otherwise you'll just be reassigning that same variable to a new orc, and you'll only end up with 1 orc cause it never went anywhere different.