fixed collection access/modification
This commit is contained in:
@@ -17,6 +17,8 @@ public class Chunk {
|
||||
private readonly int sizeX;
|
||||
private readonly int sizeY;
|
||||
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) {
|
||||
Index = index;
|
||||
@@ -37,15 +39,17 @@ public class Chunk {
|
||||
}
|
||||
|
||||
public void Update() {
|
||||
// for (int x = 0; x < sizeX; x++) {
|
||||
// for (int y = 0; y < sizeY; y++) {
|
||||
// if (Elements[x,y] != null)
|
||||
// Elements[x,y].Update();
|
||||
// }
|
||||
// }
|
||||
foreach (Element e in activeElements) {
|
||||
foreach (Element e in activeElements)
|
||||
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) {
|
||||
@@ -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) {
|
||||
if (active && e.GetType() != typeof(Element))
|
||||
activeElements.Add(e);
|
||||
else
|
||||
activeElements.Remove(e);
|
||||
if (active) toAddElements.Add(e);
|
||||
else toRemoveElements.Remove(e);
|
||||
}
|
||||
|
||||
public int ActiveElemetnsCount() {
|
||||
|
||||
Reference in New Issue
Block a user