diff --git a/Scenes/PerfDetails.cs b/Scenes/PerfDetails.cs new file mode 100644 index 0000000..219ee7c --- /dev/null +++ b/Scenes/PerfDetails.cs @@ -0,0 +1,20 @@ +using FOU.Scripts; +using Godot; + +public partial class PerfDetails : Label +{ + [Export] + public bool ShowDetails = true; + + public override void _Process(double delta) + { + Text = ""; + if (!ShowDetails) return; + + int activeElements = 0; + foreach (Chunk c in Main.Instance.Level.GetChunks()) + activeElements += c.ActiveElementsCount(); + + Text += $"Active Elements: {activeElements}"; + } +} diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index c94da6c..cab7041 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -34,7 +34,7 @@ public class Chunk { activeElements = new HashSet(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(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); + } } diff --git a/Scripts/Elements/Dirt.cs b/Scripts/Elements/Dirt.cs index e55b14e..f6e5653 100644 --- a/Scripts/Elements/Dirt.cs +++ b/Scripts/Elements/Dirt.cs @@ -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; } diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 60e141a..29fad70 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -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; + /// /// base update method, checks if anything is to do at all /// @@ -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; } } diff --git a/Scripts/Elements/Liquid.cs b/Scripts/Elements/Liquid.cs index 1d40ae6..ef1fe19 100644 --- a/Scripts/Elements/Liquid.cs +++ b/Scripts/Elements/Liquid.cs @@ -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(); diff --git a/Scripts/Elements/Solid.cs b/Scripts/Elements/Solid.cs index 0257ed4..a9803ca 100644 --- a/Scripts/Elements/Solid.cs +++ b/Scripts/Elements/Solid.cs @@ -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(); diff --git a/Scripts/Elements/Water.cs b/Scripts/Elements/Water.cs index 200f2b5..aeb93b8 100644 --- a/Scripts/Elements/Water.cs +++ b/Scripts/Elements/Water.cs @@ -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; }