From f932ae1bcd0e246d4ae5a0a398888162d8f926e2 Mon Sep 17 00:00:00 2001 From: rogo Date: Thu, 1 May 2025 23:34:31 +0200 Subject: [PATCH] refactoring --- Scripts/Chunk.cs | 2 +- Scripts/Elements/Element.cs | 46 +++++++++++-------------------------- Scripts/Elements/Liquid.cs | 2 +- Scripts/Elements/Solid.cs | 25 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index d114842..23a382d 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -61,7 +61,7 @@ public class Chunk { } public void SetElementActive(Element e, bool active) { - if (e.GetType() == typeof(Element) && active) return; + if (e.GetType() == typeof(Element)) return; if (active) AddToChunk(e); else RemoveFromChunk(e); diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 950036b..af0f4cb 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -6,8 +6,8 @@ public class Element{ public Chunk Chunk; public Vector2I Position; + public int Density; - protected int Density; protected int DiffuseSpeed = 10; protected Color color = Colors.Black; protected const int MAX_DIFFUSE_SPEED = 100; @@ -17,37 +17,40 @@ public class Element{ private const float MAX_COLOR_VARIANCE = 0.1f; private bool active = false; + private bool markedForUpdate = true; private int lastUpdate = -1; private int lastMove = 0; private Color originalColor; - private bool markedForUpdate = false; public Element(Element e) { Position = e.Position; Chunk = e.Chunk; - lastMove = Engine.GetFramesDrawn(); + Active = e.active; + lastMove = e.lastMove; + lastUpdate = e.lastUpdate; + Chunk.SetElementActive(this, Active); } public Element(int x, int y, Chunk chunk) { Position.X = x; Position.Y = y; Chunk = chunk; - Active = active; + lastMove = Engine.GetFramesDrawn(); + Chunk.SetElementActive(this, Active); } public bool Active { get => active; set { if (active == value) return; - active = value; Chunk.SetElementActive(this, value); - lastMove = Engine.GetFramesDrawn(); - if (!active) - ResetColor(); - else + if (active) { Moved(); + } else { + ResetColor(); + } } } @@ -68,7 +71,6 @@ public class Element{ return false; // already updated this frame lastUpdate = frame; - return true; } @@ -76,29 +78,7 @@ public class Element{ return $"{GetType()} {Position}[{Chunk.Index}] {active}"; } - protected virtual void Tick() { - Vector2I randomDirection = RandomDirectionDown(); - - // fall speed reduction: - if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // descend slower - - if (Chunk.IsEmpty(Position + Vector2I.Down)) - Chunk.Swap(this, Position + Vector2I.Down); - - else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection)) - Chunk.Swap(this, Position + Vector2I.Down + randomDirection); - - else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)) - Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE); - - // density check - if (Chunk.Get(Position + Vector2I.Down)?.Density < Density) - Chunk.Swap(this, Position + Vector2I.Down); - else if (Chunk.Get(Position + Vector2I.Down + randomDirection)?.Density < Density) - Chunk.Swap(this, Position + Vector2I.Down + randomDirection); - else if (Chunk.Get(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)?.Density < Density) - Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE); - } + protected virtual void Tick() { } protected Vector2I RandomDirectionDown() { int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1; diff --git a/Scripts/Elements/Liquid.cs b/Scripts/Elements/Liquid.cs index 88fca5a..4240d80 100644 --- a/Scripts/Elements/Liquid.cs +++ b/Scripts/Elements/Liquid.cs @@ -17,7 +17,7 @@ public abstract class Liquid : Element { return true; // not necessarily end, subclasses could do some more things } - protected override void Tick() { + protected virtual void Tick() { Vector2I randomDirection = RandomDirectionDown(); if (randomDirection.Y != 0) randomDirection *= Vector2I.Left * (int)(GD.Randi() % MAX_VERTICAL_SPREAD); diff --git a/Scripts/Elements/Solid.cs b/Scripts/Elements/Solid.cs index 7c71f38..1a01a99 100644 --- a/Scripts/Elements/Solid.cs +++ b/Scripts/Elements/Solid.cs @@ -14,4 +14,29 @@ public abstract class Solid : Element { return true; // not necessarily end, subclasses could do some more things } + + protected virtual void Tick() { + Vector2I randomDirection = RandomDirectionDown(); + + // fall speed reduction: + if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // descend slower + + if (Chunk.IsEmpty(Position + Vector2I.Down)) + Chunk.Swap(this, Position + Vector2I.Down); + + else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection)) + Chunk.Swap(this, Position + Vector2I.Down + randomDirection); + + else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)) + Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE); + + // density check + if (Chunk.Get(Position + Vector2I.Down)?.Density < Density) + Chunk.Swap(this, Position + Vector2I.Down); + else if (Chunk.Get(Position + Vector2I.Down + randomDirection)?.Density < Density) + Chunk.Swap(this, Position + Vector2I.Down + randomDirection); + else if (Chunk.Get(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)?.Density < Density) + Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE); + } + }