I am trying to create a hell of sand like game and I am trying to use a recycler for my square sprites so that I am not constantly creating brand new objects and deleting them. The problem is although it is reusing the element water, after a minute of spawning it will begin to bog down as though there were a ton spawned on the screen. If more info is needed please let me know, and I am brand new to this game engine so I apologize if I did something really ridiculous.
PS: I am using Farseer physics engine
///
/// This is the main type for your game.
///
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Element[] waterDrops = new Element[20];
private int activeIndex, frameCounter;
private MouseState mouseState;
private World world;
private Texture2D waterTexture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
world = new World(new Vector2(0, 15.0f));
activeIndex = 0;
frameCounter = 0;
IsMouseVisible = true;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
waterTexture = Content.Load<Texture2D>("Water");
waterDrops[0] = new Element(waterTexture, world, 0, 0);
waterDrops[0].setDead();
for (int index = 1; index < waterDrops.Length; index++)
{
waterDrops[index] = (Element)waterDrops[0].Clone();
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
waterTexture.Dispose();
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
{
activeIndex = recycleIndex();
waterDrops[activeIndex].Initialize(waterTexture, world, mouseState.X, mouseState.Y);
}
if(frameCounter%5 == 0)
foreach (Element e in waterDrops)
if (!e.isOnScreen(graphics))
e.setDead();
world.Step((float)(gameTime.ElapsedGameTime.TotalMilliseconds * .001));
base.Update(gameTime);
frameCounter++;
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
// Background color
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
foreach (Element e in waterDrops)
if(e.getAlive())
e.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
private int recycleIndex()
{
int index = activeIndex;
if (!waterDrops[index].getAlive())
return index;
index = 0;
foreach (Element e in waterDrops)
{
if (!e.getAlive())
return index;
index++;
}
if (activeIndex + 1 > waterDrops.Length - 1)
return 0;
return activeIndex + 1;
}
}