added simple activeElements list

This commit is contained in:
2024-10-27 14:44:14 +01:00
parent 34aee98a6a
commit d1dac0b855
3 changed files with 25 additions and 10 deletions

View File

@@ -6,9 +6,7 @@
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_k1i8e") script = ExtResource("1_k1i8e")
DebugVisualization = true
BrushSize = 2 BrushSize = 2
TextureResolution = 0.35
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using FOU.Scripts.Elements; using FOU.Scripts.Elements;
using Godot; using Godot;
@@ -15,6 +16,7 @@ public class Chunk {
private readonly Image image; private readonly Image image;
private readonly int sizeX; private readonly int sizeX;
private readonly int sizeY; private readonly int sizeY;
private readonly List<Element> activeElements = new List<Element>();
public Chunk(int x, int y, int index) { public Chunk(int x, int y, int index) {
Index = index; Index = index;
@@ -33,11 +35,14 @@ public class Chunk {
} }
public void Update() { public void Update() {
for (int x = 0; x < sizeX; x++) { // for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) { // for (int y = 0; y < sizeY; y++) {
if (Elements[x,y] != null) // if (Elements[x,y] != null)
Elements[x,y].Update(); // 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); int Y = Mathf.Clamp(y + j, 0, sizeY-1);
object o = Activator.CreateInstance(type, X, Y, this); object o = Activator.CreateInstance(type, X, Y, this);
if (Elements[X,Y].GetType() != type) if (Elements[X,Y].GetType() != type) {
Elements[X,Y] = o as Element; 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() { public Image DrawLevel() {
for (int x = 0; x < sizeX; x++) { for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) { for (int y = 0; y < sizeY; y++) {
@@ -67,6 +75,15 @@ public class Chunk {
return image; 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) { public void Swap(Element what, Vector2I pos) {
Swap(what, Get(pos)); Swap(what, Get(pos));
} }

View File

@@ -38,6 +38,7 @@ public class Element {
get => active; get => active;
set { set {
active = value; active = value;
Chunk.SetElementActive(this, active);
if (active) if (active)
Color = originalColor; Color = originalColor;
@@ -98,5 +99,4 @@ public class Element {
originalColor = c; originalColor = c;
return c; return c;
} }
} }