using System; using System.Collections.Generic; using FOU.Scripts.Elements; using Godot; namespace FOU.Scripts; public class Chunk { public Element[,] Elements; public int Index = -1; public Chunk NeighborN = null; public Chunk NeighborE = null; public Chunk NeighborS = null; public Chunk NeighborW = null; private readonly Image image; private readonly int sizeX; private readonly int sizeY; private readonly HashSet activeElements; private readonly HashSet toAddElements = new(); private readonly HashSet toRemoveElements = new(); public Chunk(int x, int y, int index) { Index = index; sizeX = x; sizeY = y; Elements = new Element[x, y]; for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeY; j++) { Elements[i,j] = new Element(i, j, this); } } activeElements = new HashSet(sizeX * sizeY); image = Image.CreateEmpty(sizeX, sizeY, false, Image.Format.Rgb8); image.Fill(Colors.Black); } public void Update() { foreach (Element e in activeElements) e.Update(); // apply changes: if (toRemoveElements.Count > 0) { foreach (Element removeElement in toRemoveElements) { activeElements.Remove(removeElement); removeElement.ResetColor(); } toRemoveElements.Clear(); } if (toAddElements.Count > 0) { foreach (Element addElement in toAddElements) { activeElements.Add(addElement); addElement.SetDebugColor(Colors.Green); } toAddElements.Clear(); } } public void SetElementActive(Element e, bool active) { if (e.GetType() == typeof(Element) && active) return; if (active) AddToChunk(e); else RemoveFromChunk(e); } public void WritePixel(int x, int y, int size) { int halfsize = size/2; Type type = typeof(T); for (int i = -halfsize; i <= halfsize; i++) { for (int j = -halfsize; j <= halfsize; j++) { int X = Mathf.Clamp(x + i, 0, sizeX-1); int Y = Mathf.Clamp(y + j, 0, sizeY-1); object o = Activator.CreateInstance(type, X, Y, this); // check if not empty: if (Elements[X,Y].GetType() != typeof(Element)) { return; } // if (Elements[X,Y].GetType() != type) { Elements[X, Y].Chunk.RemoveFromChunk(Elements[X, Y]); Elements[X, Y] = o as Element; Elements[X, Y].Active = true; // } } } } public int ActiveElementsCount() { return activeElements.Count; } public void Swap(Element what, Vector2I pos) { Swap(what, Get(pos)); } public Element Get(Vector2I pos) { Element e = null; if (pos.Y < 0) { if (NeighborN != null) e = NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y); } else if (pos.Y >= sizeY) { if (NeighborS != null) e = NeighborS.Get(pos.X, sizeY - pos.Y); } else if (pos.X < 0) { if (NeighborE != null) e = NeighborE.Get(NeighborE.sizeX + pos.X, pos.Y); } else if (pos.X >= sizeX) { if (NeighborW != null) e = NeighborW.Get(sizeX - pos.X, pos.Y); } else e = Get(pos.X, pos.Y); return e; } private Element Get(int x, int y) { if (x < 0 || x >= sizeX) return null; if (y < 0 || y >= sizeY) return null; return Elements[x,y]; } public bool IsEmpty(Vector2I pos) { return Get(pos)?.GetType() == typeof(Element); } private void Swap(Element what, Element swapTo) { if (what == null || swapTo == null) { GD.PrintErr("Trying to swap null"); return; } if (what.Chunk != swapTo.Chunk) { what.Chunk.RemoveFromChunk(what); swapTo.Chunk.AddToChunk(what); } Element temp = new Element(what); what.Position = swapTo.Position; what.Chunk = swapTo.Chunk; what.Chunk.Elements[what.Position.X, what.Position.Y] = what; swapTo.Position = temp.Position; swapTo.Chunk = temp.Chunk; swapTo.Chunk.Elements[swapTo.Position.X, swapTo.Position.Y] = swapTo; what.Active = true; swapTo.Active = true; } public override string ToString() { return $"Chunk {Index}"; } private void RemoveFromChunk(Element e) { toRemoveElements.Add(e); } private void AddToChunk(Element e) { toAddElements.Add(e); } }