From 0b97027e00084c5e6a78179441879fca714ca9cf Mon Sep 17 00:00:00 2001 From: rogo Date: Wed, 30 Apr 2025 23:13:53 +0200 Subject: [PATCH] fixed stuck rain refactoring --- Scenes/main.tscn | 1 - Scripts/Chunk.cs | 2 +- Scripts/Elements/Element.cs | 10 ++++++++-- Scripts/Level.cs | 14 ++++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 002e36e..b1a1e66 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -9,7 +9,6 @@ script = ExtResource("1_k1i8e") DebugMode = true BrushSize = 4 -RainAmount = 1.0 [node name="CanvasLayer" type="CanvasLayer" parent="."] diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index 342fe5c..b642d54 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -77,7 +77,7 @@ public class Chunk { object o = Activator.CreateInstance(type, x, y, this); // check if not empty and overwrite not allowed: - if (Elements[x,y].GetType() != typeof(Element) && !Main.Instance.AllowOverwrite) { + if (!Elements[x,y].IsEmpty() && !Main.Instance.AllowOverwrite) { return; } diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index dded893..dd264fd 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -32,8 +32,7 @@ public class Element{ Position.X = x; Position.Y = y; Chunk = chunk; - lastMove = Engine.GetFramesDrawn(); - Active = false; + Active = true; } public bool Active { @@ -43,6 +42,8 @@ public class Element{ active = value; Chunk.SetElementActive(this, value); + lastMove = Engine.GetFramesDrawn(); + if (!active) ResetColor(); else @@ -93,6 +94,7 @@ public class Element{ Chunk.Swap(this, Position + Vector2I.Down); } + /// -1, 0 or 1 protected Vector2I RandomDirectionDown() { int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1; @@ -133,4 +135,8 @@ public class Element{ public void MarkForUpdate(bool mark = true) { markedForUpdate = true; } + + public bool IsEmpty() { + return GetType() == typeof(Element); + } } diff --git a/Scripts/Level.cs b/Scripts/Level.cs index 51660ac..c14b7d8 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -84,8 +84,11 @@ public class Level { int rainDrops = (int)Math.Round(rainAmount); for (int i = 0; i <= rainDrops; i++) { - if (GD.Randf() < rainAmount) - WritePixel((int)(GD.Randi() % (chunkResX * chunksPerX)), 0, 1); + if (GD.Randf() < rainAmount) { + int d = (int)(GD.Randi() % (chunkResX * chunksPerX)); + if (GetElement(d, 0).IsEmpty()) + WritePixel(d, 0, 1); + } } } @@ -145,6 +148,13 @@ public class Level { } } + public Element GetElement(int x, int y) { + if (x < 0 || x > chunkResX * chunkResX) return null; + if (y < 0 || y > chunkResY * chunkResY) return null; + + return chunks[x / chunkResX, y / chunkResY].Elements[x % chunkResX, y % chunkResY]; + } + public Image DrawLevel() { // chunk for (int cx = 0; cx < chunksPerX; cx++) {