code cleanup

This commit is contained in:
2024-08-25 18:45:38 +02:00
parent a470593ae4
commit 8c3f7c2964
4 changed files with 26 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ 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;
@@ -15,18 +16,16 @@ public class Chunk {
private readonly int sizeX;
private readonly int sizeY;
private int frame;
private Chunk _this;
public Chunk(int x, int y) {
_this = this;
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);
Elements[i,j] = new Element(i, j, this);
}
}
@@ -55,7 +54,7 @@ public class Chunk {
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);
object o = Activator.CreateInstance(type, X, Y, this);
if (Elements[X,Y].GetType() != type)
Elements[X,Y] = o as Element;
@@ -77,19 +76,27 @@ public class Chunk {
}
public Element Get(Vector2I pos) {
if (pos.Y < 0)
if (NeighborN != null) return NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y);
Element e = null;
if (pos.Y >= sizeY)
if (NeighborS != null) return NeighborS.Get(pos.X, sizeY - pos.Y);
if (pos.Y < 0) {
if (NeighborN != null)
e = NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y);
if (pos.X < 0)
if (NeighborE != null) return NeighborE.Get(NeighborE.sizeX + pos.X, pos.Y);
} else if (pos.Y >= sizeY) {
if (NeighborS != null)
e = NeighborS.Get(pos.X, sizeY - pos.Y);
if (pos.X >= sizeX)
if (NeighborW != null) return NeighborW.Get(sizeX - pos.X, pos.Y);
} else if (pos.X < 0) {
if (NeighborE != null)
e = NeighborE.Get(NeighborE.sizeX + pos.X, pos.Y);
return Get(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) {