diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 9b78cab..af11d31 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -6,9 +6,7 @@ [node name="Main" type="Node2D"] script = ExtResource("1_k1i8e") -DebugVisualization = true BrushSize = 2 -TextureResolution = 0.35 [node name="CanvasLayer" type="CanvasLayer" parent="."] diff --git a/Scripts/Chunk.cs b/Scripts/Chunk.cs index b178496..ab0ffdf 100644 --- a/Scripts/Chunk.cs +++ b/Scripts/Chunk.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using FOU.Scripts.Elements; using Godot; @@ -15,6 +16,7 @@ public class Chunk { private readonly Image image; private readonly int sizeX; private readonly int sizeY; + private readonly List activeElements = new List(); public Chunk(int x, int y, int index) { Index = index; @@ -33,11 +35,14 @@ public class Chunk { } public void Update() { - for (int x = 0; x < sizeX; x++) { - for (int y = 0; y < sizeY; y++) { - if (Elements[x,y] != null) - Elements[x,y].Update(); - } + // for (int x = 0; x < sizeX; x++) { + // for (int y = 0; y < sizeY; y++) { + // if (Elements[x,y] != null) + // Elements[x,y].Update(); + // } + // } + foreach (Element e in activeElements) { + e.Update(); } } @@ -52,12 +57,15 @@ public class Chunk { int Y = Mathf.Clamp(y + j, 0, sizeY-1); object o = Activator.CreateInstance(type, X, Y, this); - if (Elements[X,Y].GetType() != type) - Elements[X,Y] = o as Element; + if (Elements[X,Y].GetType() != type) { + Elements[X, Y] = o as Element; + activeElements.Add(Elements[X, Y]); + } } } } + // TODO: use this function and stitch together partial images in Level public Image DrawLevel() { for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { @@ -67,6 +75,15 @@ public class Chunk { return image; } + public void SetElementActive(Element e, bool active) { + if (e.Active == active) return; + + if (active) + activeElements.Add(e); + else + activeElements.Remove(e); + } + public void Swap(Element what, Vector2I pos) { Swap(what, Get(pos)); } diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 3b9185e..cb24643 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -38,6 +38,7 @@ public class Element { get => active; set { active = value; + Chunk.SetElementActive(this, active); if (active) Color = originalColor; @@ -98,5 +99,4 @@ public class Element { originalColor = c; return c; } - }