I'm new to Monogame and I'm having some perfomance issues, I'm loading a matrix of 9 maps (3x3) each with 50x50 tiles of 32x32 pixels each. I store that in a list of maps from 0 to 8 and call them by their filename from [0,0] ( [X,Y] ) up to [2,2] completing the 3x3 matrix in order to know what and where to load each new map.
Whenever my player goes to the border of the "playzone" [1,1] (the middle map) I load the respective three maps that follow, in this case I implemented the right loading, when the player heads to the right, over to [1,2] I need to load [0,3][1,3][2,3] and I unload [0,0][0,1][0,2] but whenever my game does this it stutters for a second. I might have overcomplicated things a bit and I'd love to be pointed in the right direction to optimize this. Thank you!
GameScreen class
namespace Game1.Screens
{
public class GameplayScreen : GameScreen
{
Player player;
List<Map> maps;
float chunkWidth;
Vector2 viewChunk;
MapConfig mapConfig;
XmlManager<Map> mapLoader;
XmlManager<MapConfig> mapConfigLoader;
bool loadingMaps;
public GameplayScreen()
{
chunkWidth = 0;
viewChunk = new Vector2(0, 0);
mapLoader = new XmlManager<Map>();
mapConfigLoader = new XmlManager<MapConfig>();
mapConfig = mapConfigLoader.Load("Load/Gameplay/Maps/MapConfig.xml");
loadingMaps = false;
}
public override void LoadContent()
{
base.LoadContent();
XmlManager<Player> playerLoader = new XmlManager<Player>();
player = playerLoader.Load("Load/Gameplay/Player.xml");
player.LoadContent();
LoadMaps(new Vector2(0,0));
if (maps.Count > 0)
chunkWidth = maps[0].Layer[0].TiledMap.Row.Count * maps[0].TileDimensions.X;
}
public void LoadMaps(Vector2 startingPosition)
{
maps = new List<Map>();
int mapX = 0;
int mapY = 0;
int currentMapX = (int)(Math.Floor(player.CurrentPos.X / mapConfig.TileDimensions.X / mapConfig.ColumnCount));
int currentMapY = (int)(Math.Floor(player.CurrentPos.Y / mapConfig.TileDimensions.Y / mapConfig.RowCount));
for (int i = 0; i < mapConfig.ChunkMatrixAmount.Y; i++)
{
mapY = currentMapY + (i - 1);
for (int j = 0; j < mapConfig.ChunkMatrixAmount.X; j++)
{
mapX = currentMapX + (j - 1);
maps.Add(mapLoader.Load("Load/Gameplay/Maps/Map" + mapX + mapY + ".xml"));
}
}
for (int i = 0; i <= maps.Count - 1; i++)
maps[i].LoadContent(i, startingPosition);
loadingMaps = false;
}
public override void UnloadContent()
{
base.UnloadContent();
player.UnloadContent();
foreach (Map map in maps)
map.UnloadContent();
maps = new List<Map>();
}
public override void Update(GameTime gameTime)
{
Vector2 endingMatrixPos = new Vector2(0, 0);
Vector2 currentMatrixPos = new Vector2
{
X = (float)(Math.Floor(player.CurrentPos.X / mapConfig.TileDimensions.X / mapConfig.ColumnCount)),
Y = (float)(Math.Floor(player.CurrentPos.Y / mapConfig.TileDimensions.Y / mapConfig.RowCount))
};
//Load a new RIGHT and unload LEFT
if (currentMatrixPos.X >= Math.Floor(maps[5].Layer[0].Image.Position.X / mapConfig.TileDimensions.X / mapConfig.ColumnCount) &&
maps[8].Layer[0].Image.Position.X != 0 &&
!loadingMaps)
{
loadingMaps = true;
foreach (Map map in maps)
map.UnloadContent();
endingMatrixPos.X = maps[0].Layer[0].Image.Position.X + mapConfig.TileDimensions.X;
endingMatrixPos.Y = (maps[0].Layer[0].Image.Position.Y + mapConfig.TileDimensions.Y) - (mapConfig.TileDimensions.Y * mapConfig.RowCount);
LoadMaps(endingMatrixPos);
}
base.Update(gameTime);
player.Update(gameTime);
foreach (Map map in maps)
map.Update(gameTime, ref player);
if (player.CameraFollow)
{
Camera.IsFollowingObject = true;
Camera.Pos = -player.CurrentPos;
}
Camera.Update();
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Camera.Transform);
base.Draw(spriteBatch);
foreach (Map map in maps)
map.Draw(spriteBatch, "Underlay");
player.Draw(spriteBatch);
foreach (Map map in maps)
map.Draw(spriteBatch, "Overlay");
spriteBatch.End();
}
}
}
If you need more information I'll provide it when possible! Thank you for your help again!