128 lines
3.4 KiB
C#
128 lines
3.4 KiB
C#
using System;
|
|
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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
|
image.Fill(Colors.Black);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void WritePixel<T>(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);
|
|
|
|
if (Elements[X,Y].GetType() != type)
|
|
Elements[X,Y] = o as Element;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Image DrawLevel() {
|
|
for (int x = 0; x < sizeX; x++) {
|
|
for (int y = 0; y < sizeY; y++) {
|
|
image.SetPixel(x,y, Elements[x,y].Color);
|
|
}
|
|
}
|
|
return image;
|
|
}
|
|
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
}
|