diff --git a/Scenes/FPSLabel.cs b/Scenes/FPSLabel.cs index a204847..55f8f8d 100644 --- a/Scenes/FPSLabel.cs +++ b/Scenes/FPSLabel.cs @@ -2,7 +2,8 @@ using Godot; public partial class FPSLabel : Label { - bool ShowFPS = true; + [Export] + public bool ShowFPS = true; // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { diff --git a/Scenes/main.tscn b/Scenes/main.tscn index af11d31..e113c39 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -1,11 +1,13 @@ -[gd_scene load_steps=4 format=3 uid="uid://cf34vk5r055dx"] +[gd_scene load_steps=5 format=3 uid="uid://cf34vk5r055dx"] [ext_resource type="Script" path="res://Scripts/Main.cs" id="1_k1i8e"] [ext_resource type="Script" path="res://Scenes/FPSLabel.cs" id="2_8cb7y"] [ext_resource type="Script" path="res://Scenes/SettingsController.cs" id="3_a4w6m"] +[ext_resource type="Script" path="res://Scenes/PerfDetails.cs" id="3_u2p48"] [node name="Main" type="Node2D"] script = ExtResource("1_k1i8e") +DebugVisualization = true BrushSize = 2 [node name="CanvasLayer" type="CanvasLayer" parent="."] @@ -32,6 +34,15 @@ text = "FPS: 0 " script = ExtResource("2_8cb7y") +[node name="PerfDetails" type="Label" parent="CanvasLayer/TopUI"] +layout_mode = 0 +offset_top = 30.0 +offset_right = 76.0 +offset_bottom = 67.0 +theme_override_font_sizes/font_size = 10 +text = "Active Elements: 0" +script = ExtResource("3_u2p48") + [node name="Settings" type="VBoxContainer" parent="CanvasLayer/TopUI"] layout_mode = 1 anchors_preset = 1 diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index ab0ffdf..9050333 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using FOU.Scripts.Elements; using Godot; @@ -16,7 +17,7 @@ public class Chunk { private readonly Image image; private readonly int sizeX; private readonly int sizeY; - private readonly List activeElements = new List(); + private readonly HashSet activeElements; public Chunk(int x, int y, int index) { Index = index; @@ -30,6 +31,8 @@ public class Chunk { } } + activeElements = new HashSet(sizeX * sizeY); + image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8); image.Fill(Colors.Black); } @@ -59,7 +62,7 @@ public class Chunk { if (Elements[X,Y].GetType() != type) { Elements[X, Y] = o as Element; - activeElements.Add(Elements[X, Y]); + Elements[X, Y].Active = true; } } } @@ -78,12 +81,17 @@ public class Chunk { public void SetElementActive(Element e, bool active) { if (e.Active == active) return; - if (active) + GD.Print($"setting {e} to {active}"); + if (active && e.GetType() != typeof(Element)) activeElements.Add(e); else activeElements.Remove(e); } + public int ActiveElemetnsCount() { + return activeElements.Count; + } + public void Swap(Element what, Vector2I pos) { Swap(what, Get(pos)); } diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index cb24643..c0c9731 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -18,7 +18,7 @@ public class Element { protected const float MAX_COLOR_VARIANCE = 0.1f; protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1); - private bool active = true; + private bool active = false; private Color originalColor; public Element(Element e) { @@ -32,18 +32,17 @@ public class Element { Position.Y = y; Chunk = chunk; LastMove = Engine.GetFramesDrawn(); + Active = false; } public bool Active { get => active; set { - active = value; - Chunk.SetElementActive(this, active); + if (active == value) return; - if (active) - Color = originalColor; - else if (Main.Instance.DebugVisualization) - Color = new Color(0.2f, 0.2f, 0.2f); + active = value; + Chunk.SetElementActive(this, value); + SetDebugColors(value); } } @@ -99,4 +98,11 @@ public class Element { originalColor = c; return c; } + + private void SetDebugColors(bool isActive) { + if (isActive) + Color = originalColor; + else if (Main.Instance.DebugVisualization) + Color = new Color(0.2f, 0.2f, 0.2f); + } } diff --git a/Scripts/Level.cs b/Scripts/Level.cs index def3dd1..add31b8 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -18,7 +18,6 @@ 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) { @@ -75,6 +74,10 @@ public class Level { } } + public Chunk[,] GetChunks() { + return chunks; + } + private void MakeItRain() { if (rainAmount < .1f) return; diff --git a/Scripts/Main.cs b/Scripts/Main.cs index 43ab965..fea1519 100644 --- a/Scripts/Main.cs +++ b/Scripts/Main.cs @@ -9,35 +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 { - get => rainAmount; - set { - rainAmount = value; - mLevel.SetRainAmount(rainAmount); - } - } + [Export] public float RainAmount = 0; public static Main Instance; + public Level Level; 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), + Level = new Level(this, (int)(GetViewportRect().Size.X * TextureResolution), (int)(GetViewportRect().Size.Y * TextureResolution)); - mLevel.SetRainAmount(rainAmount); + Level.SetRainAmount(rainAmount); mLevelDrawer = GetNode("CanvasLayer/LevelDrawer"); Instance = this; } public override void _Process(double delta) { - mLevel.Update(); - mLevelDrawer.Texture = ImageTexture.CreateFromImage(mLevel.DrawLevel()); + Level.Update(); + mLevelDrawer.Texture = ImageTexture.CreateFromImage(Level.DrawLevel()); } public override void _UnhandledInput(InputEvent @event) { @@ -46,14 +38,14 @@ public partial class Main : Node2D { if (eventMouseButton.IsPressed()) { Vector2 mouse = GetViewport().GetMousePosition(); - float mappedX = mouse.X / (GetViewportRect().Size.X / mLevel.Resolution.X); - float mappedY = mouse.Y / (GetViewportRect().Size.Y / mLevel.Resolution.Y); + float mappedX = mouse.X / (GetViewportRect().Size.X / Level.Resolution.X); + float mappedY = mouse.Y / (GetViewportRect().Size.Y / Level.Resolution.Y); - mLevel.WritePixel((int)mappedX, (int)mappedY, BrushSize); + Level.WritePixel((int)mappedX, (int)mappedY, BrushSize); } } else if (@event is InputEventKey keyEvent && keyEvent.Pressed) { if (keyEvent.Keycode == Key.F9) - mLevel.StartBenchmark(); + Level.StartBenchmark(); } else base._UnhandledInput(@event); }