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);
|
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);
|
image.Fill(Colors.Black);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,14 +43,29 @@ public class Chunk {
|
|||||||
e.Update();
|
e.Update();
|
||||||
|
|
||||||
// apply changes:
|
// apply changes:
|
||||||
foreach (Element removeElement in toRemoveElements)
|
if (toRemoveElements.Count > 0) {
|
||||||
|
foreach (Element removeElement in toRemoveElements) {
|
||||||
activeElements.Remove(removeElement);
|
activeElements.Remove(removeElement);
|
||||||
|
removeElement.ResetColor();
|
||||||
|
}
|
||||||
toRemoveElements.Clear();
|
toRemoveElements.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (Element addElement in toAddElements)
|
if (toAddElements.Count > 0) {
|
||||||
|
foreach (Element addElement in toAddElements) {
|
||||||
activeElements.Add(addElement);
|
activeElements.Add(addElement);
|
||||||
|
addElement.SetDebugColor(Colors.Green);
|
||||||
|
}
|
||||||
toAddElements.Clear();
|
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) {
|
public void WritePixel<T>(int x, int y, int size) {
|
||||||
int halfsize = size/2;
|
int halfsize = size/2;
|
||||||
@@ -63,20 +78,21 @@ public class Chunk {
|
|||||||
int Y = Mathf.Clamp(y + j, 0, sizeY-1);
|
int Y = Mathf.Clamp(y + j, 0, sizeY-1);
|
||||||
object o = Activator.CreateInstance(type, X, Y, this);
|
object o = Activator.CreateInstance(type, X, Y, this);
|
||||||
|
|
||||||
if (Elements[X,Y].GetType() != type) {
|
// 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] = o as Element;
|
||||||
Elements[X, Y].Active = true;
|
Elements[X, Y].Active = true;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetElementActive(Element e, bool active) {
|
public int ActiveElementsCount() {
|
||||||
if (active) toAddElements.Add(e);
|
|
||||||
else toRemoveElements.Remove(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int ActiveElemetnsCount() {
|
|
||||||
return activeElements.Count;
|
return activeElements.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +140,12 @@ public class Chunk {
|
|||||||
GD.PrintErr("Trying to swap null");
|
GD.PrintErr("Trying to swap null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (what.Chunk != swapTo.Chunk) {
|
||||||
|
what.Chunk.RemoveFromChunk(what);
|
||||||
|
swapTo.Chunk.AddToChunk(what);
|
||||||
|
}
|
||||||
|
|
||||||
Element temp = new Element(what);
|
Element temp = new Element(what);
|
||||||
|
|
||||||
what.Position = swapTo.Position;
|
what.Position = swapTo.Position;
|
||||||
@@ -137,4 +159,16 @@ public class Chunk {
|
|||||||
what.Active = true;
|
what.Active = true;
|
||||||
swapTo.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 class Dirt : Solid {
|
||||||
|
|
||||||
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
||||||
Color = AddColorVariance(Colors.Brown);
|
color = AddColorVariance(Colors.Brown);
|
||||||
Density = 10;
|
Density = 10;
|
||||||
DiffuseSpeed = 50;
|
DiffuseSpeed = 50;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,35 +3,35 @@
|
|||||||
namespace FOU.Scripts.Elements;
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
public class Element {
|
public class Element {
|
||||||
public Color Color = Colors.Black;
|
|
||||||
|
|
||||||
public Vector2I Position;
|
|
||||||
public Chunk Chunk;
|
public Chunk Chunk;
|
||||||
public int Density;
|
public Vector2I Position;
|
||||||
public int LastUpdate = -1;
|
|
||||||
public int LastMove = 0;
|
|
||||||
|
|
||||||
public int DiffuseSpeed = 10;
|
protected int Density;
|
||||||
public const int MAX_DIFFUSE_SPEED = 100;
|
protected int DiffuseSpeed = 10;
|
||||||
public const int STEPS_UNTIL_INACTIVE = 500;
|
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 const float MAX_COLOR_VARIANCE = 0.1f;
|
||||||
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
||||||
|
protected int lastMove = 0;
|
||||||
|
|
||||||
private bool active = false;
|
private bool active = false;
|
||||||
|
private int lastUpdate = -1;
|
||||||
private Color originalColor;
|
private Color originalColor;
|
||||||
|
|
||||||
public Element(Element e) {
|
public Element(Element e) {
|
||||||
Position = e.Position;
|
Position = e.Position;
|
||||||
Chunk = e.Chunk;
|
Chunk = e.Chunk;
|
||||||
LastMove = Engine.GetFramesDrawn();
|
lastMove = Engine.GetFramesDrawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element(int x, int y, Chunk chunk) {
|
public Element(int x, int y, Chunk chunk) {
|
||||||
Position.X = x;
|
Position.X = x;
|
||||||
Position.Y = y;
|
Position.Y = y;
|
||||||
Chunk = chunk;
|
Chunk = chunk;
|
||||||
LastMove = Engine.GetFramesDrawn();
|
lastMove = Engine.GetFramesDrawn();
|
||||||
Active = false;
|
Active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,10 +42,12 @@ public class Element {
|
|||||||
|
|
||||||
active = value;
|
active = value;
|
||||||
Chunk.SetElementActive(this, value);
|
Chunk.SetElementActive(this, value);
|
||||||
SetDebugColors(value);
|
// SetDebugColor(value, new Color(0.2f, 0.2f, 0.2f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Color Color => color;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// base update method, checks if anything is to do at all
|
/// base update method, checks if anything is to do at all
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -54,8 +56,8 @@ public class Element {
|
|||||||
public virtual bool Update() {
|
public virtual bool Update() {
|
||||||
if (!Active) return false;
|
if (!Active) return false;
|
||||||
|
|
||||||
if (LastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
|
if (lastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame
|
||||||
LastUpdate = Engine.GetFramesDrawn();
|
lastUpdate = Engine.GetFramesDrawn();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -99,10 +101,13 @@ public class Element {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetDebugColors(bool isActive) {
|
public void ResetColor() {
|
||||||
if (isActive)
|
color = originalColor;
|
||||||
Color = originalColor;
|
}
|
||||||
else if (Main.Instance.DebugVisualization)
|
|
||||||
Color = new Color(0.2f, 0.2f, 0.2f);
|
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() {
|
public override bool Update() {
|
||||||
if (!base.Update()) return false;
|
if (!base.Update()) return false;
|
||||||
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
||||||
|
|
||||||
Tick();
|
Tick();
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public abstract class Solid : Element {
|
|||||||
|
|
||||||
public override bool Update() {
|
public override bool Update() {
|
||||||
if (!base.Update()) return false;
|
if (!base.Update()) return false;
|
||||||
if (LastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false;
|
||||||
|
|
||||||
Tick();
|
Tick();
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace FOU.Scripts.Elements;
|
|||||||
public class Water : Liquid {
|
public class Water : Liquid {
|
||||||
|
|
||||||
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
|
||||||
Color = AddColorVariance(Colors.Blue);
|
color = AddColorVariance(Colors.Blue);
|
||||||
Density = 1;
|
Density = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user