From 7db4ba3c2a14f815d971cc644bc8b709efc24c92 Mon Sep 17 00:00:00 2001 From: rogo Date: Sat, 2 Dec 2023 01:43:09 +0100 Subject: [PATCH] fixed wrong opposite random direction --- Scripts/Elements/Element.cs | 18 +++++++++--------- Scripts/Elements/Water.cs | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index b7f26da..fa953d9 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -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; } diff --git a/Scripts/Elements/Water.cs b/Scripts/Elements/Water.cs index 6c30504..930b9ad 100644 --- a/Scripts/Elements/Water.cs +++ b/Scripts/Elements/Water.cs @@ -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); + } - - - } }