I have this RPG game and this has elements such as the map, the monsters and the items. The monsters and the items are stored in List
s and every time I want to show the map around the player, I have to check if the element is within the bounds of the camera.
public void ShowCamera()
{
int size = cameraSize/2;
int topY = player.GetY() - size, bottomY = player.GetY() + size, topX = player.GetX() - size, bottomX = player.GetX() + size;
int setX = 0, setY = 0;
//Print Map
Console.SetCursorPosition(0,0);
Console.ForegroundColor = ConsoleColor.DarkGreen;
for (int i = topX; i < bottomX; i++)
{
for (int j = topY; j < bottomY; j++)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.SetCursorPosition(setY,setX);
Console.Write(map[i, j]);
if (monstros.Count > 0)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
foreach (var elem in monstros)
{
if (elem.ReX() == i && elem.ReY() == j)
{
Console.SetCursorPosition(setY, setX);
Console.Write(elem);
}
}
}
if (items.Count > 0)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
foreach (var elem in items)
{
if (elem.ReX() == i && elem.ReY() == j)
{
Console.SetCursorPosition(setY, setX);
Console.Write(elem);
}
}
}
setY++;
}
Console.Write("\n");
setY = 0;
setX++;
}
So, this works but it is very unoptimized and flickers a lot. I was wondering if there's a way to improve this cycle in any way.
monstros
anditems
? What ismap
? \$\endgroup\$monstros
just to find an appropriate one inside twofor
loops seems like a massive overhead. There's some optimization possible there IMHO, but it's impossible to know unless you show us the relevant code. \$\endgroup\$monstros
code is simply it's appeareance, an X and a Y. That's pretty much what they are. That applies for the items aswell. \$\endgroup\$