Compare commits

2 Commits

Author SHA1 Message Date
2d38774341 fixed elements crossing chunk borders 2024-08-25 18:45:54 +02:00
8c3f7c2964 code cleanup 2024-08-25 18:45:38 +02:00
4 changed files with 35 additions and 34 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) {
@@ -104,21 +111,19 @@ public class Chunk {
}
private void Swap(Element what, Element swapTo) {
Element swap = new Element(what);
GD.Print($"Swap: {what} -> {swapTo}");
if (swapTo.Position.Y == 0) GD.Print($"");
if (what == null || swapTo == null) return;
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;
Elements[swapTo.Position.X, swapTo.Position.Y] = what;
swapTo.Position = swap.Position;
swapTo.Chunk = swap.Chunk;
Elements[swap.Position.X, swap.Position.Y] = swapTo;
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;

View File

@@ -59,7 +59,7 @@ public class Element {
}
public override string ToString() {
return $"{GetType()} {Position}";
return $"{GetType()} {Position}[{Chunk.Index}]";
}
protected virtual void Tick() {

View File

@@ -17,7 +17,7 @@ public class Level {
private int chunkResY;
public Level(Main main, int sizeX, int sizeY) {
GD.Print($"Generating level ({sizeX}:{sizeY})");
GD.Print($"Generating level ({sizeX}:{sizeY}) with {chunksX*chunksY} chunks ({chunkResX} * {chunkResY})");
Resolution = new Vector2I(sizeX, sizeY);
chunksX = main.ChunksPerAxis;
@@ -26,15 +26,14 @@ public class Level {
chunkResX = sizeX / chunksX;
chunkResY = sizeY / chunksY;
GD.Print($"{chunksX}x{chunksY} chunks. {chunkResX}:{chunkResY} res each.");
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
chunks = new Chunk[chunksX, chunksY];
int index = 0;
// create all chunks
for (int x = 0; x < chunksX; x++) {
for (int y = 0; y < chunksY; y++) {
chunks[x, y] = new Chunk(chunkResX, chunkResY);
chunks[x, y] = new Chunk(chunkResX, chunkResY, index++);
}
}
@@ -47,7 +46,6 @@ public class Level {
if (x > 0) chunks[x, y].NeighborE = chunks[x-1, y];
if (x < chunksX-1) chunks[x, y].NeighborW = chunks[x+1, y];
}
}
}

View File

@@ -43,5 +43,3 @@ public partial class Main : Node2D {
} else base._UnhandledInput(@event);
}
}
}
}