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);
}
}

View File

@@ -5,7 +5,7 @@ namespace FOU.Scripts.Elements;
public class Dirt : Solid {
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
Color = AddColorVariance(Colors.Brown);
color = AddColorVariance(Colors.Brown);
Density = 10;
DiffuseSpeed = 50;
}

View File

@@ -3,35 +3,35 @@
namespace FOU.Scripts.Elements;
public class Element {
public Color Color = Colors.Black;
public Vector2I Position;
public Chunk Chunk;
public int Density;
public int LastUpdate = -1;
public int LastMove = 0;
public Vector2I Position;
public int DiffuseSpeed = 10;
public const int MAX_DIFFUSE_SPEED = 100;
public const int STEPS_UNTIL_INACTIVE = 500;
protected int Density;
protected int DiffuseSpeed = 10;
protected Color color = Colors.Black;
protected const int MAX_DIFFUSE_SPEED = 100;
protected const int STEPS_UNTIL_INACTIVE = 500;
protected const float MAX_COLOR_VARIANCE = 0.1f;
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
protected int lastMove = 0;
private bool active = false;
private int lastUpdate = -1;
private Color originalColor;
public Element(Element e) {
Position = e.Position;
Chunk = e.Chunk;
LastMove = Engine.GetFramesDrawn();
lastMove = Engine.GetFramesDrawn();
}
public Element(int x, int y, Chunk chunk) {
Position.X = x;
Position.Y = y;
Chunk = chunk;
LastMove = Engine.GetFramesDrawn();
lastMove = Engine.GetFramesDrawn();
Active = false;
}
@@ -42,10 +42,12 @@ public class Element {
active = value;
Chunk.SetElementActive(this, value);
SetDebugColors(value);
// SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f));
}
}
public Color Color => color;
/// <summary>
/// base update method, checks if anything is to do at all
/// </summary>
@@ -54,8 +56,8 @@ public class Element {
public virtual bool Update() {
if (!Active) return false;
if (LastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
LastUpdate = Engine.GetFramesDrawn();
if (lastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
lastUpdate = Engine.GetFramesDrawn();
return true;
}
@@ -99,10 +101,13 @@ public class Element {
return c;
}
private void SetDebugColors(bool isActive) {
if (isActive)
Color = originalColor;
else if (Main.Instance.DebugVisualization)
Color = new Color(0.2f, 0.2f, 0.2f);
public void ResetColor() {
color = originalColor;
}
public void SetDebugColor(Color color) {
if (!Main.Instance.DebugVisualization) return;
this.color = color;
}
}

View File

@@ -9,7 +9,7 @@ public abstract class Liquid : Element {
public override bool Update() {
if (!base.Update()) return false;
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
Tick();

View File

@@ -7,7 +7,7 @@ public abstract class Solid : Element {
public override bool Update() {
if (!base.Update()) return false;
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
Tick();

View File

@@ -5,7 +5,7 @@ namespace FOU.Scripts.Elements;
public class Water : Liquid {
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
Color = AddColorVariance(Colors.Blue);
color = AddColorVariance(Colors.Blue);
Density = 1;
}