From 9a36418d23ffbad6771872ceba446d0e2e30e8e9 Mon Sep 17 00:00:00 2001 From: rogo Date: Thu, 1 May 2025 23:46:18 +0200 Subject: [PATCH] activated activateneighbors experiment --- Scripts/Chunk.cs | 20 ++++++++++++-------- Scripts/Elements/Element.cs | 19 +++++++++++++++++++ Scripts/Elements/Liquid.cs | 25 ++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index 23a382d..9436fc3 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -13,7 +13,6 @@ public class Chunk { public Chunk NeighborS = null; public Chunk NeighborW = null; - private readonly Image image; private readonly int sizeX; private readonly int sizeY; private readonly HashSet activeElements; @@ -33,9 +32,6 @@ public class Chunk { } activeElements = new HashSet(sizeX * sizeY); - - image = Image.CreateEmpty(sizeX, sizeY, false, Image.Format.Rgb8); - image.Fill(Colors.Black); } public void Update() { @@ -90,10 +86,6 @@ public class Chunk { return activeElements.Count; } - public void Swap(Element what, Vector2I pos) { - Swap(what, Get(pos)); - } - public Element Get(Vector2I pos) { Element e = null; @@ -129,6 +121,10 @@ public class Chunk { return Get(pos)?.GetType() == typeof(Element); } + public void Swap(Element what, Vector2I pos) { + Swap(what, Get(pos)); + } + private void Swap(Element what, Element swapTo) { if (what == null || swapTo == null) { GD.PrintErr("Trying to swap null"); @@ -155,8 +151,16 @@ public class Chunk { what.Active = true; what.Moved(); + if (what.IsEmpty()) + what.ActivateNeighbors(); + swapTo.Active = true; swapTo.Moved(); + if (swapTo.IsEmpty()) + swapTo.ActivateNeighbors(); + + // TODO: + // if either "what" or "swapTo" is empty, re-activate all neighbors of the now empty ones } public override string ToString() { diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index af0f4cb..b4a2029 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -131,4 +131,23 @@ public class Element{ public bool IsEmpty() { return GetType() == typeof(Element); } + + public virtual void ActivateNeighbors() { + + Element n = Chunk.Get(Position + Vector2I.Up); + if (n != null && !n.IsEmpty()) + n.Active = true; + // + // Element e = Chunk.Get(Position + Vector2I.Right); + // if (e != null && !e.IsEmpty()) + // e.Active = true; + // + // Element s = Chunk.Get(Position + Vector2I.Down); + // if (null != s && !s.IsEmpty()) + // s.Active = true; + // + // Element w = Chunk.Get(Position + Vector2I.Left); + // if (null != w && !w.IsEmpty()) + // w.Active = true; + } } diff --git a/Scripts/Elements/Liquid.cs b/Scripts/Elements/Liquid.cs index 4240d80..f3f61bc 100644 --- a/Scripts/Elements/Liquid.cs +++ b/Scripts/Elements/Liquid.cs @@ -24,11 +24,30 @@ public abstract class Liquid : Element { if (Chunk.IsEmpty(Position + Vector2I.Down)) Chunk.Swap(this, Position + Vector2I.Down); + else if (Chunk.IsEmpty(Position + randomDirection)) + Chunk.Swap(this, Position + randomDirection); + else if (Chunk.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE)) + Chunk.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE); else if (Chunk.IsEmpty(Position + randomDirection)) - Chunk.Swap(this, Position + Vector2I.Right * randomDirection); + Chunk.Swap(this, Position + Vector2I.Right + randomDirection); + else if (Chunk.IsEmpty(Position - randomDirection)) + Chunk.Swap(this, Position + Vector2I.Right + randomDirection * VERTICAL_OPPOSITE); + } - else if (Chunk.IsEmpty(Position + randomDirection)) - Chunk.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE); + public override void ActivateNeighbors() { + + Element n = Chunk.Get(Position + Vector2I.Up); + if (n != null && !n.IsEmpty()) + n.Active = true; + + // only activate liquids left or right + Element e = Chunk.Get(Position + Vector2I.Right); + if (e != null && e.GetType() == typeof(Liquid)) + e.Active = true; + + Element s = Chunk.Get(Position + Vector2I.Down); + if (null != s && s.GetType() == typeof(Liquid)) + s.Active = true; } }