improved handling of colors

improved debug colors
refactoring
This commit is contained in:
2024-12-21 23:04:01 +01:00
parent 4e6b1d642c
commit 71db6513f2
7 changed files with 97 additions and 38 deletions

View File

@@ -34,7 +34,7 @@ public class Chunk {
activeElements = new HashSet<Element>(sizeX * sizeY);
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
image = Image.CreateEmpty(sizeX, sizeY, false, Image.Format.Rgb8);
image.Fill(Colors.Black);
}
@@ -43,13 +43,28 @@ public class Chunk {
e.Update();
// apply changes:
foreach (Element removeElement in toRemoveElements)
activeElements.Remove(removeElement);
toRemoveElements.Clear();
if (toRemoveElements.Count > 0) {
foreach (Element removeElement in toRemoveElements) {
activeElements.Remove(removeElement);
removeElement.ResetColor();
}
toRemoveElements.Clear();
}
foreach (Element addElement in toAddElements)
activeElements.Add(addElement);
toAddElements.Clear();
if (toAddElements.Count > 0) {
foreach (Element addElement in toAddElements) {
activeElements.Add(addElement);
addElement.SetDebugColor(Colors.Green);
}
toAddElements.Clear();
}
}
public void SetElementActive(Element e, bool active) {
if (e.GetType() == typeof(Element) && active) return;
if (active) AddToChunk(e);
else RemoveFromChunk(e);
}
public void WritePixel<T>(int x, int y, int size) {
@@ -63,20 +78,21 @@ public class Chunk {
int Y = Mathf.Clamp(y + j, 0, sizeY-1);
object o = Activator.CreateInstance(type, X, Y, this);
if (Elements[X,Y].GetType() != type) {
Elements[X, Y] = o as Element;
Elements[X, Y].Active = true;
// check if not empty:
if (Elements[X,Y].GetType() != typeof(Element)) {
return;
}
// if (Elements[X,Y].GetType() != type) {
Elements[X, Y].Chunk.RemoveFromChunk(Elements[X, Y]);
Elements[X, Y] = o as Element;
Elements[X, Y].Active = true;
// }
}
}
}
public void SetElementActive(Element e, bool active) {
if (active) toAddElements.Add(e);
else toRemoveElements.Remove(e);
}
public int ActiveElemetnsCount() {
public int ActiveElementsCount() {
return activeElements.Count;
}
@@ -124,6 +140,12 @@ public class Chunk {
GD.PrintErr("Trying to swap null");
return;
}
if (what.Chunk != swapTo.Chunk) {
what.Chunk.RemoveFromChunk(what);
swapTo.Chunk.AddToChunk(what);
}
Element temp = new Element(what);
what.Position = swapTo.Position;
@@ -137,4 +159,16 @@ public class Chunk {
what.Active = true;
swapTo.Active = true;
}
public override string ToString() {
return $"Chunk {Index}";
}
private void RemoveFromChunk(Element e) {
toRemoveElements.Add(e);
}
private void AddToChunk(Element e) {
toAddElements.Add(e);
}
}