From eb94c3bef98d87ae3a86ffa549a61910f753dfef Mon Sep 17 00:00:00 2001 From: rogo Date: Sat, 2 Dec 2023 00:45:09 +0100 Subject: [PATCH] added density code improvements --- Scripts/Elements/Dirt.cs | 4 ++-- Scripts/Elements/Element.cs | 14 +++++++++++--- Scripts/Elements/Water.cs | 12 +++++++----- Scripts/Level.cs | 2 ++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Scripts/Elements/Dirt.cs b/Scripts/Elements/Dirt.cs index ad829db..680bf0a 100644 --- a/Scripts/Elements/Dirt.cs +++ b/Scripts/Elements/Dirt.cs @@ -5,8 +5,8 @@ namespace FOU.Scripts.Elements; public class Dirt : Solid { public Dirt(int x, int y, ref Level level) : base(x, y, ref level) { - Color = Colors.Brown; - Color = AddColorVariance(Color); + Color = AddColorVariance(Colors.Brown); + Density = 10; } public override bool Update(int currentFrame) { diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 8d9f886..36e0a32 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -6,6 +6,7 @@ public class Element { public Color Color = Colors.Black; public Vector2I Position; + public int Density; protected readonly float MaxColorVariance = 0.1f; protected readonly Level Level; @@ -40,9 +41,7 @@ public class Element { } protected virtual void Tick() { - int randomDirection = 1; - if (GD.Randi() % 2 != 0) - randomDirection *= -1; + int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1; if (Level.IsEmpty(Position + Vector2I.Down)) { Level.Swap(this, Position + Vector2I.Down); @@ -53,6 +52,15 @@ public class Element { } else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection * -1)) { Level.Swap(this, Position + Vector2I.Right*randomDirection); } + + if (GD.Randi() % 10 > 1) return; // ascend slower + + if (Level.Get(Position + Vector2I.Down + // maybe also side + + (GD.Randi() % 2 != 0 ? + Vector2I.Zero : Vector2I.Right * randomDirection) + )?.Density < Density) + Level.Swap(this, Position + Vector2I.Down); } protected Color AddColorVariance(Color baseColor) { diff --git a/Scripts/Elements/Water.cs b/Scripts/Elements/Water.cs index 9484f12..6c30504 100644 --- a/Scripts/Elements/Water.cs +++ b/Scripts/Elements/Water.cs @@ -4,8 +4,8 @@ namespace FOU.Scripts.Elements; public class Water : Solid { public Water(int x, int y, ref Level level) : base(x, y, ref level) { - Color = Colors.Blue; - Color = AddColorVariance(Color); + Color = AddColorVariance(Colors.Blue); + Density = 1; } public override bool Update(int currentFrame) { @@ -17,9 +17,8 @@ public class Water : Solid { } protected override void Tick() { - int randomDirection = 1; - if (GD.Randi() % 2 != 0) - randomDirection *= -1; + int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1; + randomDirection *= GD.Randi() % 2 != 0 ? 1 : 2; if (Level.IsEmpty(Position + Vector2I.Down)) { Level.Swap(this, Position + Vector2I.Down); @@ -31,5 +30,8 @@ public class Water : Solid { Level.Swap(this, Position + Vector2I.Right*randomDirection); return; } + + + } } diff --git a/Scripts/Level.cs b/Scripts/Level.cs index 0474d17..667a140 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -78,6 +78,8 @@ public class Level { public void Swap(Element what, Element swapTo) { Element swap = new Element(what); + if (what == null || swapTo == null) return; + what.Position = swapTo.Position; _elements[swapTo.Position.X, swapTo.Position.Y] = what;