fixed collection access/modification

This commit is contained in:
2024-12-21 15:08:55 +01:00
parent 8ebaa9e987
commit 4e6b1d642c

View File

@@ -17,6 +17,8 @@ public class Chunk {
private readonly int sizeX; private readonly int sizeX;
private readonly int sizeY; private readonly int sizeY;
private readonly HashSet<Element> activeElements; private readonly HashSet<Element> activeElements;
private readonly HashSet<Element> toAddElements = new();
private readonly HashSet<Element> toRemoveElements = new();
public Chunk(int x, int y, int index) { public Chunk(int x, int y, int index) {
Index = index; Index = index;
@@ -37,15 +39,17 @@ public class Chunk {
} }
public void Update() { public void Update() {
// for (int x = 0; x < sizeX; x++) { foreach (Element e in activeElements)
// for (int y = 0; y < sizeY; y++) {
// if (Elements[x,y] != null)
// Elements[x,y].Update();
// }
// }
foreach (Element e in activeElements) {
e.Update(); e.Update();
}
// apply changes:
foreach (Element removeElement in toRemoveElements)
activeElements.Remove(removeElement);
toRemoveElements.Clear();
foreach (Element addElement in toAddElements)
activeElements.Add(addElement);
toAddElements.Clear();
} }
public void WritePixel<T>(int x, int y, int size) { public void WritePixel<T>(int x, int y, int size) {
@@ -67,21 +71,9 @@ public class Chunk {
} }
} }
// TODO: use this function and stitch together partial images in Level
public Image DrawLevel() {
for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) {
image.SetPixel(x,y, Elements[x,y].Color);
}
}
return image;
}
public void SetElementActive(Element e, bool active) { public void SetElementActive(Element e, bool active) {
if (active && e.GetType() != typeof(Element)) if (active) toAddElements.Add(e);
activeElements.Add(e); else toRemoveElements.Remove(e);
else
activeElements.Remove(e);
} }
public int ActiveElemetnsCount() { public int ActiveElemetnsCount() {