From bb2f498ac558e82029ca8d40664c57c3abac6af9 Mon Sep 17 00:00:00 2001 From: rogo Date: Sun, 27 Oct 2024 13:53:49 +0100 Subject: [PATCH] re-enabled rain --- Scenes/main.tscn | 1 - Scripts/Chunk.cs | 2 -- Scripts/Level.cs | 30 +++++++++++++++++++++--------- Scripts/Main.cs | 13 ++++++++++++- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 59b5472..9b78cab 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -9,7 +9,6 @@ script = ExtResource("1_k1i8e") DebugVisualization = true BrushSize = 2 TextureResolution = 0.35 -RainAmount = 5.0 [node name="CanvasLayer" type="CanvasLayer" parent="."] diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index 26b62a2..b178496 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -39,8 +39,6 @@ public class Chunk { Elements[x,y].Update(); } } - // TODO: enable rain again - // MakeItRain(_main.RainAmount); } public void WritePixel(int x, int y, int size) { diff --git a/Scripts/Level.cs b/Scripts/Level.cs index cd43941..a471dc1 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -1,4 +1,6 @@ -using Godot; +using System; +using FOU.Scripts.Elements; +using Godot; namespace FOU.Scripts; @@ -16,6 +18,9 @@ public class Level { private int chunkResX; private int chunkResY; + private bool enableRain = false; + private float rainAmount = 0; + public Level(Main main, int sizeX, int sizeY) { GD.Print($"Generating level ({sizeX}:{sizeY}) with {chunksX*chunksY} chunks ({chunkResX} * {chunkResY})"); @@ -51,18 +56,25 @@ public class Level { } public void Update() { + MakeItRain(); + foreach (Chunk c in chunks) c.Update(); } - // TODO! - private void MakeItRain(float rainAmount) { - // int rainDrops = (int)Math.Round(rainAmount); - // - // for (int i = 0; i <= rainDrops; i++) { - // if (GD.Randf() < rainAmount) - // WritePixel((int)(GD.Randi() % SizeX), 0, 1); - // } + public void SetRainAmount(float amount) { + rainAmount = amount; + } + + private void MakeItRain() { + if (rainAmount < .1f) return; + + int rainDrops = (int)Math.Round(rainAmount); + + for (int i = 0; i <= rainDrops; i++) { + if (GD.Randf() < rainAmount) + WritePixel((int)(GD.Randi() % (chunkResX * chunksX)), 0, 1); + } } public void WritePixel(int x, int y, int size) { diff --git a/Scripts/Main.cs b/Scripts/Main.cs index 8c1fc1f..9efc559 100644 --- a/Scripts/Main.cs +++ b/Scripts/Main.cs @@ -9,16 +9,27 @@ public partial class Main : Node2D { [Export] public int BrushSize = 5; [Export] public float TextureResolution = 0.5f; [Export] public int ChunksPerAxis = 2; - [Export] public float RainAmount = 0.01f; + + [Export] + public float RainAmount { + get => rainAmount; + set { + rainAmount = value; + mLevel.SetRainAmount(rainAmount); + } + } public static Main Instance; private TextureRect mLevelDrawer; private Level mLevel; + private bool enableRain; + private float rainAmount; public override void _Ready() { mLevel = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution), (int)(GetViewportRect().Size.Y * TextureResolution)); + mLevel.SetRainAmount(rainAmount); mLevelDrawer = GetNode("CanvasLayer/LevelDrawer"); Instance = this;