From da3afd22fcc8fb89f8885833091dbd5ef594c615 Mon Sep 17 00:00:00 2001 From: rogo Date: Sun, 27 Apr 2025 14:50:23 +0200 Subject: [PATCH] fixed drawpixel optimization minor class refactoring --- Scenes/main.tscn | 1 + Scripts/Elements/Element.cs | 34 ++++++++++++++++++++++------------ Scripts/Elements/Liquid.cs | 5 +++-- Scripts/Elements/Solid.cs | 5 +++-- Scripts/Level.cs | 6 ++++-- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 9d34974..b132ddc 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -7,6 +7,7 @@ [node name="Main" type="Node2D"] script = ExtResource("1_k1i8e") +DebugMode = true BrushSize = 2 RainAmount = 1.0 diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 924ea16..dded893 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -2,7 +2,7 @@ namespace FOU.Scripts.Elements; -public class Element { +public class Element{ public Chunk Chunk; public Vector2I Position; @@ -15,12 +15,12 @@ public class Element { protected const float MAX_COLOR_VARIANCE = 0.1f; protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1); - protected int lastMove = 0; private bool active = false; - private bool wasMovedThisTick = false; private int lastUpdate = -1; + private int lastMove = 0; private Color originalColor; + private bool markedForUpdate = false; public Element(Element e) { Position = e.Position; @@ -34,7 +34,6 @@ public class Element { Chunk = chunk; lastMove = Engine.GetFramesDrawn(); Active = false; - wasMovedThisTick = true; } public bool Active { @@ -44,7 +43,10 @@ public class Element { active = value; Chunk.SetElementActive(this, value); - Moved(); + if (!active) + ResetColor(); + else + Moved(); } } @@ -58,8 +60,13 @@ public class Element { public virtual bool Update() { if (!Active) return false; - if (lastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame - lastUpdate = Engine.GetFramesDrawn(); + int frame = Engine.GetFramesDrawn(); + if (lastMove + STEPS_UNTIL_INACTIVE < frame) + Active = false; + if (lastUpdate == frame) + return false; // already updated this frame + + lastUpdate = frame; return true; } @@ -70,7 +77,6 @@ public class Element { protected virtual void Tick() { Vector2I randomDirection = RandomDirectionDown(); - wasMovedThisTick = false; if (Chunk.IsEmpty(Position + Vector2I.Down)) Chunk.Swap(this, Position + Vector2I.Down); @@ -106,21 +112,25 @@ public class Element { public void ResetColor() { color = originalColor; + MarkForUpdate(); } public void SetDebugColor(Color color) { if (!Main.Instance.DebugMode) return; this.color = color; - wasMovedThisTick = true; } public void Moved() { lastMove = Engine.GetFramesDrawn(); - wasMovedThisTick = true; + MarkForUpdate(); } - public bool WasMoved() { - return wasMovedThisTick; + public bool MarkedForUpdate() { + return markedForUpdate; + } + + public void MarkForUpdate(bool mark = true) { + markedForUpdate = true; } } diff --git a/Scripts/Elements/Liquid.cs b/Scripts/Elements/Liquid.cs index ef1fe19..88fca5a 100644 --- a/Scripts/Elements/Liquid.cs +++ b/Scripts/Elements/Liquid.cs @@ -5,11 +5,12 @@ namespace FOU.Scripts.Elements; public abstract class Liquid : Element { public const int MAX_VERTICAL_SPREAD = 3; - protected Liquid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { } + protected Liquid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { + MarkForUpdate(); + } public override bool Update() { if (!base.Update()) return false; - if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false; Tick(); diff --git a/Scripts/Elements/Solid.cs b/Scripts/Elements/Solid.cs index a9803ca..7c71f38 100644 --- a/Scripts/Elements/Solid.cs +++ b/Scripts/Elements/Solid.cs @@ -3,11 +3,12 @@ namespace FOU.Scripts.Elements; public abstract class Solid : Element { - protected Solid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { } + protected Solid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { + MarkForUpdate(); + } public override bool Update() { if (!base.Update()) return false; - if (lastMove + STEPS_UNTIL_INACTIVE < Engine.GetFramesDrawn()) Active = false; Tick(); diff --git a/Scripts/Level.cs b/Scripts/Level.cs index a26c47a..782b4d6 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -157,8 +157,10 @@ public class Level { for (int y = 0; y < chunkResY; y++) { // TODO: multithreading here! use Chunk.DrawLevel() and stitch images together - if (chunks[cx, cy].Elements[x, y].WasMoved()) - image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx, cy].Elements[x, y].Color); + if (chunks[cx, cy].Elements[x, y].MarkedForUpdate()) { + image.SetPixel(cx * chunkResX + x, cy * chunkResY + y, chunks[cx, cy].Elements[x, y].Color); + chunks[cx, cy].Elements[x, y].MarkForUpdate(false); + } } } }