fixed wrong opposite random direction

This commit is contained in:
2023-12-02 01:43:09 +01:00
parent f0a6396073
commit 7db4ba3c2a
2 changed files with 20 additions and 20 deletions

View File

@@ -11,7 +11,8 @@ public class Element {
public int DiffuseSpeed = 10;
public const int MAX_DIFFUSE_SPEED = 100;
protected readonly float MaxColorVariance = 0.1f;
protected const float MAX_COLOR_VARIANCE = 0.1f;
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
protected readonly Level Level;
private int lastUpdate = -1;
@@ -52,11 +53,11 @@ public class Element {
} else if (Level.IsEmpty(Position + randomDirection)) {
Level.Swap(this, Position + randomDirection);
} else if (Level.IsEmpty(Position + randomDirection * -Vector2I.Left)) {
Level.Swap(this, Position + randomDirection * -1);
} else if (Level.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE)) {
Level.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE);
}
if (GD.Randi() % 100 > DiffuseSpeed) return; // ascend slower
if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // ascend slower
if (Level.Get(Position + randomDirection)?.Density < Density)
Level.Swap(this, Position + Vector2I.Down);
@@ -65,16 +66,15 @@ public class Element {
protected Vector2I RandomDirectionDown() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;
return Vector2I.Down
// maybe also side
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;
c.G += (GD.Randf() - 1) * MaxColorVariance;
c.B += (GD.Randf() - 1) * MaxColorVariance;
c.R += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
c.G += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
c.B += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
return c;
}

View File

@@ -3,6 +3,8 @@
namespace FOU.Scripts.Elements;
public class Water : Solid {
public const int MAX_VERTICAL_SPREAD = 3;
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
Color = AddColorVariance(Colors.Blue);
Density = 1;
@@ -17,21 +19,19 @@ public class Water : Solid {
}
protected override void Tick() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;
randomDirection *= GD.Randi() % 2 != 0 ? 1 : 2;
Vector2I randomDirection = RandomDirectionDown();
if (randomDirection.Y != 0)
randomDirection *= Vector2I.Left * (int)(GD.Randi() % MAX_VERTICAL_SPREAD);
if (Level.IsEmpty(Position + Vector2I.Down)) {
Level.Swap(this, Position + Vector2I.Down);
return;
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection)) {
} else if (Level.IsEmpty(Position + randomDirection)) {
Level.Swap(this, Position + Vector2I.Right * randomDirection);
return;
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection * -1)) {
Level.Swap(this, Position + Vector2I.Right*randomDirection);
return;
}
} else if (Level.IsEmpty(Position + randomDirection)) {
Level.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE);
}
}
}