added diffuse speed and refactoring

This commit is contained in:
2023-12-02 01:10:18 +01:00
parent eb94c3bef9
commit f0a6396073
2 changed files with 19 additions and 11 deletions

View File

@@ -8,6 +8,9 @@ public class Element {
public Vector2I Position;
public int Density;
public int DiffuseSpeed = 10;
public const int MAX_DIFFUSE_SPEED = 100;
protected readonly float MaxColorVariance = 0.1f;
protected readonly Level Level;
@@ -41,28 +44,32 @@ public class Element {
}
protected virtual void Tick() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;
Vector2I randomDirection = RandomDirectionDown();
if (Level.IsEmpty(Position + Vector2I.Down)) {
Level.Swap(this, Position + Vector2I.Down);
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection)) {
Level.Swap(this, Position + Vector2I.Right * randomDirection);
} else if (Level.IsEmpty(Position + randomDirection)) {
Level.Swap(this, Position + randomDirection);
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection * -1)) {
Level.Swap(this, Position + Vector2I.Right*randomDirection);
} else if (Level.IsEmpty(Position + randomDirection * -Vector2I.Left)) {
Level.Swap(this, Position + randomDirection * -1);
}
if (GD.Randi() % 10 > 1) return; // ascend slower
if (GD.Randi() % 100 > DiffuseSpeed) return; // ascend slower
if (Level.Get(Position + Vector2I.Down
// maybe also side
+ (GD.Randi() % 2 != 0 ?
Vector2I.Zero : Vector2I.Right * randomDirection)
)?.Density < Density)
if (Level.Get(Position + randomDirection)?.Density < Density)
Level.Swap(this, Position + Vector2I.Down);
}
protected Vector2I RandomDirectionDown() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;
return Vector2I.Down
// maybe also side
+ (GD.Randi() % 2 != 0 ? Vector2I.Zero : Vector2I.Right * randomDirection);
}
protected Color AddColorVariance(Color baseColor) {
Color c = baseColor;
c.R += (GD.Randf() - 1) * MaxColorVariance;