Hello! I'm currently making a 2D game using XNA. I've made a class named GL_Object that contains my objects data (Sprite, Position, etc), and also contains a GL_Path object. GL_Path contains a GL_Motion. This GL_Motion object contains a list of GL_Movement objects.
GL_Movement is a interface which holds two main methods, "ProcessMove", and "DestroyMove".
So I have three ways of handling movements:
- GL_PhysicsMotion -> Uses Farseer Engine to adjust Position
- GL_IndependentMotion -> Here I manipulate manually the position based on my own created movements
- GL_MoveCollection -> Contains a group of motions, and handles all of them to generate a new "movement".
GL_Path holds the Motion Collection and determines which movement is to be played, based on time or specific conditions.
In the Objects update, they call Path.ProcessMove() which by the way calls the current active movement and process it. (Like a cascade, everything here is a GL_Movement)
It works perfectly... until I have 500+ objects in the scene. I usually don't need that many, but in the middle of a battle, when you're simutaneously attacking 3 or 4 monsters, lots of effects show up and things get messy. Even when the object has only 1 movement, and even when the object has just one frame of animation. It's weird because it doesn't slow down all of a sudden... it gradully slows down.
Well, not really perfectly. I tested with 48 objects... it doesn't slow down at first, but after several minutes, everything is slow.
I figured out it was my Movement classes because when I commented them up i could draw almost 1000 objects without "progressive" slowdowns.
I thought I was infinitely adding something to the list, or something like that, but i think i'm not. The only thing called in the update is "ProcessMove". So, my code: (Don't consider "Pathways", "MoveCollection" or "PhysicsMovement" stuff because i'm not testing with them)
GL_Path.ProcessMove
public void ProcessMove(ref Body body, GameTime gameTime)
{
HandlePathways(ref body, gameTime);
if (IsFollowingPathWays)
{
GL_Motion Move = MotionList[ActiveMotionNumber];
Move.ProcessMove(ref body, gameTime);
return;
}
else
{
for (int i = 0; i < MotionList.Count; i++)
{
MotionList[i].ProcessMove(ref body, gameTime);
}
}
}
GL_IndependentMotion.ProcessMove
//Processing Motion
public override void ProcessMove(ref Body body, GameTime gameTime)
{
//Resets Variation
Variation = Vector2.Zero;
//Executes MotionBehaviour
Variation += FixedVariation;
MotionBehaviour(Factor);
//Increases Factor
Factor += VariationFactor;
//Returns Body
body.Position += Variation * gameTime.ElapsedGameTime.Milliseconds / 16;
}
Another guess is that working with delegates might not be a nice option when you have to update the game at a relatively quick rate. All I'm doing with MotionBehaviour (delegate) is changing position based on other values, though. Nothing really time-consuming i think. Any ideas on what might be slowing things down? Should I hard-code each movement instead of making programming simpler?