Compare commits

4 Commits

Author SHA1 Message Date
f932ae1bcd refactoring 2025-05-01 23:34:31 +02:00
c41cdd57c4 fixed activate not set on copy 2025-05-01 23:06:46 +02:00
e653f23a42 dirt shall only move downwards 2025-05-01 23:01:42 +02:00
6acccefa0a diffuse in both directions 2025-05-01 22:51:39 +02:00
5 changed files with 41 additions and 33 deletions

View File

@@ -1,5 +1,3 @@
using System.Reflection;
using FOU.Scripts;
using Godot;
public partial class SettingsController : VBoxContainer

View File

@@ -61,7 +61,7 @@ public class Chunk {
}
public void SetElementActive(Element e, bool active) {
if (e.GetType() == typeof(Element) && active) return;
if (e.GetType() == typeof(Element)) return;
if (active) AddToChunk(e);
else RemoveFromChunk(e);

View File

@@ -6,48 +6,51 @@ public class Element{
public Chunk Chunk;
public Vector2I Position;
public int Density;
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);
private const float MAX_COLOR_VARIANCE = 0.1f;
private bool active = false;
private bool markedForUpdate = true;
private int lastUpdate = -1;
private int lastMove = 0;
private Color originalColor;
private bool markedForUpdate = false;
public Element(Element e) {
Position = e.Position;
Chunk = e.Chunk;
lastMove = Engine.GetFramesDrawn();
Active = e.active;
lastMove = e.lastMove;
lastUpdate = e.lastUpdate;
Chunk.SetElementActive(this, Active);
}
public Element(int x, int y, Chunk chunk) {
Position.X = x;
Position.Y = y;
Chunk = chunk;
Active = true;
lastMove = Engine.GetFramesDrawn();
Chunk.SetElementActive(this, Active);
}
public bool Active {
get => active;
set {
if (active == value) return;
active = value;
Chunk.SetElementActive(this, value);
lastMove = Engine.GetFramesDrawn();
if (!active)
ResetColor();
else
if (active) {
Moved();
} else {
ResetColor();
}
}
}
@@ -68,7 +71,6 @@ public class Element{
return false; // already updated this frame
lastUpdate = frame;
return true;
}
@@ -76,24 +78,7 @@ public class Element{
return $"{GetType()} {Position}[{Chunk.Index}] {active}";
}
protected virtual void Tick() {
Vector2I randomDirection = RandomDirectionDown();
if (Chunk.IsEmpty(Position + Vector2I.Down))
Chunk.Swap(this, Position + Vector2I.Down);
else if (Chunk.IsEmpty(Position + randomDirection))
Chunk.Swap(this, Position + randomDirection);
else if (Chunk.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE))
Chunk.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE);
// density check
if (Chunk.Get(Position + Vector2I.Down)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down);
else if (Chunk.Get(Position + Vector2I.Down + randomDirection)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down + randomDirection);
}
protected virtual void Tick() { }
protected Vector2I RandomDirectionDown() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;

View File

@@ -17,7 +17,7 @@ public abstract class Liquid : Element {
return true; // not necessarily end, subclasses could do some more things
}
protected override void Tick() {
protected virtual void Tick() {
Vector2I randomDirection = RandomDirectionDown();
if (randomDirection.Y != 0)
randomDirection *= Vector2I.Left * (int)(GD.Randi() % MAX_VERTICAL_SPREAD);

View File

@@ -14,4 +14,29 @@ public abstract class Solid : Element {
return true; // not necessarily end, subclasses could do some more things
}
protected virtual void Tick() {
Vector2I randomDirection = RandomDirectionDown();
// fall speed reduction:
if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // descend slower
if (Chunk.IsEmpty(Position + Vector2I.Down))
Chunk.Swap(this, Position + Vector2I.Down);
else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection))
Chunk.Swap(this, Position + Vector2I.Down + randomDirection);
else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE))
Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE);
// density check
if (Chunk.Get(Position + Vector2I.Down)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down);
else if (Chunk.Get(Position + Vector2I.Down + randomDirection)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down + randomDirection);
else if (Chunk.Get(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE);
}
}