Should the same objects be made in the same class?

My question is this: lets say I have 5 bad guy objects in my game. Which is the correct /and/or better way to implement them?

1: Create 5 bad guy objects in my main game file

or

2: Create 1 bad guy object in my main game file and have that object create the 5 bad guys with an array or something.

I am doing it the first way now, and it works, but I think the correct way would be number 2. I would love some answers about the correct programming practice. I assume it would be number 2, but I’m really not sure.

5 different bad guys should be instantiated as separate instances of a BadGuy class.
ex:

class BadGuy {

// etc…

}

in the main class:

BadGuy bg1, bg2, bg3, bg4, bg5;

@Archive thanks, I appreciate it.

Or have a badguy class and spawn class that does the job of distributing the bad guys. I would put them in an array so they can be iterated through.

[quote=“FabulousFellini,post:1,topic:57712”]
Can you be more specific about exactly what you mean by this? Can you put together a small but runnable example that tries this approach out?

This is as much for you as it is for us. Questions like this are best answered by simply trying both approaches out and seeing which one you like the best.

But it sounds like the first approach is “better” since I’m not even sure what the second approach really means.

For the 2nd way, I meant something like this:

Main:


BadGuy badGuys = new BadGuy();

then the bad guy class:


public class BadGuy {
    public BadGuy(){
        badGuy1 = new BadGuy();
        badGuy2 = new BadGuy();
     }

}

As opposed to option 1, which is this:
Main:


badGuy1 = new BadGuy();
badGuy2 = new BadGuy();

I was thinking about this initially because my game has about a million “brickwalls” that are just named brickwall1, brickwall2, etc, instantiated in my main game file, and I wanted to see how other people instantiate their objects in their games.

Did you actually try that? Try putting together a little example program. You’ll see that this code causes a StackOverflowException.

Sounds like you should just use an ArrayList or some other data structure?

No, I didn’t actually try it yet, but I think I’m going to leave it how it is for now. Seems like that is the best way to go.

I’d use a Factory pattern for this :slight_smile:


List<BadGuy> badGuys = new ArrayList<BadGuy>();

:wink:

I’ve been doing this with an ArrayList and I don’t have to type out badGuy1, badGuy2, badGuy3… for however many objects I need. Plus it’s easier to run update() or whatever your function is for updating the badguys. See sample


public class Game {
	ArrayList<BadGuy> badGuys;
	
	final int NUM_BAD_GUYS = 100;
	
	public Game() {
		badGuys = new ArrayList<BadGuy>();
		
		for (int i = 0; i < NUM_BAD_GUYS; i++) {
				badGuys.add(new BadGuy());
		}
	}
	
	private void update() {
		for (int i = 0; i < badGuys.size(); i++) {
			badGuys.get(i).update();
		}
	}
}

Yeah this is what I do as well. I was just wondering if people instantiate all the bad guys within the bad guy class itself, or a spawn class, instead of in the main game class. In this scenerio you would just instantiate 1 bad guy in the main game file, which would in turn instantiate and handle all the individual bad guys.

I use a ‘manager’ class which spawns the bad guys - this manager class has a thread safe collection list in there where the bad guys are stored.


	// Store baddies in here
	private CopyOnWriteArrayList<BadGuy> badGuys;

Later in my code in the manager class I have a render which calls the render method on the bad guys:


	public void renderBadGuys(Camera camera, SpriteBatch batch) {
		for (int i = 0; i < badGuys.size(); i++) {
			BadGuy badguy = badGuys.get(i);
			if(badguy!=null)
			{
				if (badguy.health <= 0) // if bad guy dead, remove them from the
										// list
					removeBadGuy(badguy);
				else {
	
					badguy.move(badguy.x, badguy.y, camera);
	
					// Only draw outside baddies that are in the camera's view
					if ((badguy.x >= camera.position.x - (width + 30) && badguy.x <= camera.position.x + width + 10)
							&& (badguy.y >= camera.position.y - height && badguy.y <= camera.position.y + height)) {
	
						badguy.render(batch);
					}
				}
			}
		}
	}


If I was you, I’d have an abstract parent class of BadGuy and derive from this with your other ‘bad guys’, this abstract class would have various abstract
methods in there such as move, attack and by using polymorphism like this will allow your other bad guys to act differently if you know what I mean?!

FWIW, I usually create a simple interface which only has one method: void update() - since pretty much every game entity needs updating in the game loop. I apply this interface to all my game entity classes, and store every single game entity in one big list rather than lots of smaller lists. It’s then just a case of iterate through this list calling update() on each one.