I have a game that i’m working on where I have a particle effect as a separate class
public class Explosion
{
private List<Particle> Particles;
private const int particle_Count = 100;
public Explosion(Vector2 location) {
Random random = new Random();
Particles = new List<Particle>(particle_Count);
for(int i = 0; i < particle_Count; i++)
Particles.add(new Particle(location, new Vector2(random.NextDouble() * i,
random.NextDouble() * i));
}
}
Now, there need to be several different types of explosions in my game, but they only vary very slightly (It’s literally the difference between one Vector2 object (Which is just an object that stores two coordinates and a few helper functions for those whom aren’t familiar with XNA))
My question is, because Random is only accessible once I enter the constructer, how do i make variations on of this explosion?
Should I make a new class for each explosion? (That seems unnecessary due to how little changes between explosions)
Should I overload the function to take a separate parameter (maybe an int) for each different explosion?
Should I directly pass the Vector2 in through the constructor (which seems like it would break good OOP principle to me)
Or is there another way to get around this that i’m just not seeing.
Any help would be much appreciated, I like to do things right the first time, and this is an important part of my application.
Thanks.
Since there’s only one parameter difference between each variation, the best thing is just to pass another parameter into the constructor (as you mentioned). Just pass in the minimal info you need; if you like, give it a default value so that it’s not required:
If it’s a complex object, and not just an int, then consider making a helper method of some kind:
This way calling the constructor for Explosion doesn’t have to get too messy: