Files
FOU/Scripts/Chunk.cs

171 lines
4.6 KiB
C#

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<Element> activeElements;
private readonly HashSet<Element> toAddElements = new();
private readonly HashSet<Element> 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<Element>(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(addElement.Color * 1.5f);
}
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<T>(int x, int y) {
if (x < 0 || x >= sizeX || y < 0 || y >= sizeY) {
GD.PrintErr($"Out of bounds for chunk: {x}:{y}");
return;
}
Type type = typeof(T);
object o = Activator.CreateInstance(type, x, y, this);
// check if not empty and overwrite not allowed:
if (Elements[x,y].GetType() != typeof(Element) && !Main.Instance.AllowOverwrite) {
return;
}
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;
what.Moved();
swapTo.Active = true;
swapTo.Moved();
}
public override string ToString() {
return $"Chunk {Index}";
}
private void RemoveFromChunk(Element e) {
toRemoveElements.Add(e);
}
private void AddToChunk(Element e) {
toAddElements.Add(e);
}
}