From 4e6b1d642c5fc186a62ec8dc07e06c07c0158d9d Mon Sep 17 00:00:00 2001 From: rogo Date: Sat, 21 Dec 2024 15:08:55 +0100 Subject: [PATCH] fixed collection access/modification --- Scripts/Chunk.cs | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index 3af20de..c94da6c 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -17,6 +17,8 @@ public class Chunk { private readonly int sizeX; private readonly int sizeY; private readonly HashSet activeElements; + private readonly HashSet toAddElements = new(); + private readonly HashSet 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(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() {