improved handling of colors
improved debug colors refactoring
This commit is contained in:
20
Scenes/PerfDetails.cs
Normal file
20
Scenes/PerfDetails.cs
Normal file
@@ -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}";
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user