Compare commits
2 Commits
a470593ae4
...
2d38774341
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d38774341 | |||
| 8c3f7c2964 |
@@ -6,6 +6,7 @@ namespace FOU.Scripts;
|
|||||||
|
|
||||||
public class Chunk {
|
public class Chunk {
|
||||||
public Element[,] Elements;
|
public Element[,] Elements;
|
||||||
|
public int Index = -1;
|
||||||
public Chunk NeighborN = null;
|
public Chunk NeighborN = null;
|
||||||
public Chunk NeighborE = null;
|
public Chunk NeighborE = null;
|
||||||
public Chunk NeighborS = null;
|
public Chunk NeighborS = null;
|
||||||
@@ -15,18 +16,16 @@ public class Chunk {
|
|||||||
private readonly int sizeX;
|
private readonly int sizeX;
|
||||||
private readonly int sizeY;
|
private readonly int sizeY;
|
||||||
private int frame;
|
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;
|
sizeX = x;
|
||||||
sizeY = y;
|
sizeY = y;
|
||||||
|
|
||||||
Elements = new Element[x, y];
|
Elements = new Element[x, y];
|
||||||
for (int i = 0; i < sizeX; i++) {
|
for (int i = 0; i < sizeX; i++) {
|
||||||
for (int j = 0; j < sizeY; j++) {
|
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++) {
|
for (int j = -halfsize; j <= halfsize; j++) {
|
||||||
int X = Mathf.Clamp(x + i, 0, sizeX-1);
|
int X = Mathf.Clamp(x + i, 0, sizeX-1);
|
||||||
int Y = Mathf.Clamp(y + j, 0, sizeY-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)
|
if (Elements[X,Y].GetType() != type)
|
||||||
Elements[X,Y] = o as Element;
|
Elements[X,Y] = o as Element;
|
||||||
@@ -77,19 +76,27 @@ public class Chunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Element Get(Vector2I pos) {
|
public Element Get(Vector2I pos) {
|
||||||
if (pos.Y < 0)
|
Element e = null;
|
||||||
if (NeighborN != null) return NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y);
|
|
||||||
|
|
||||||
if (pos.Y >= sizeY)
|
if (pos.Y < 0) {
|
||||||
if (NeighborS != null) return NeighborS.Get(pos.X, sizeY - pos.Y);
|
if (NeighborN != null)
|
||||||
|
e = NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y);
|
||||||
|
|
||||||
if (pos.X < 0)
|
} else if (pos.Y >= sizeY) {
|
||||||
if (NeighborE != null) return NeighborE.Get(NeighborE.sizeX + pos.X, pos.Y);
|
if (NeighborS != null)
|
||||||
|
e = NeighborS.Get(pos.X, sizeY - pos.Y);
|
||||||
|
|
||||||
if (pos.X >= sizeX)
|
} else if (pos.X < 0) {
|
||||||
if (NeighborW != null) return NeighborW.Get(sizeX - pos.X, pos.Y);
|
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) {
|
private Element Get(int x, int y) {
|
||||||
@@ -104,21 +111,19 @@ public class Chunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void Swap(Element what, Element swapTo) {
|
private void Swap(Element what, Element swapTo) {
|
||||||
Element swap = new Element(what);
|
if (what == null || swapTo == null) {
|
||||||
|
GD.PrintErr("Trying to swap null");
|
||||||
GD.Print($"Swap: {what} -> {swapTo}");
|
return;
|
||||||
if (swapTo.Position.Y == 0) GD.Print($"");
|
}
|
||||||
|
Element temp = new Element(what);
|
||||||
if (what == null || swapTo == null) return;
|
|
||||||
|
|
||||||
what.Position = swapTo.Position;
|
what.Position = swapTo.Position;
|
||||||
what.Chunk = swapTo.Chunk;
|
what.Chunk = swapTo.Chunk;
|
||||||
Elements[swapTo.Position.X, swapTo.Position.Y] = what;
|
what.Chunk.Elements[what.Position.X, what.Position.Y] = what;
|
||||||
|
|
||||||
swapTo.Position = swap.Position;
|
|
||||||
swapTo.Chunk = swap.Chunk;
|
|
||||||
Elements[swap.Position.X, swap.Position.Y] = swapTo;
|
|
||||||
|
|
||||||
|
swapTo.Position = temp.Position;
|
||||||
|
swapTo.Chunk = temp.Chunk;
|
||||||
|
swapTo.Chunk.Elements[swapTo.Position.X, swapTo.Position.Y] = swapTo;
|
||||||
|
|
||||||
what.Active = true;
|
what.Active = true;
|
||||||
swapTo.Active = true;
|
swapTo.Active = true;
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public class Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return $"{GetType()} {Position}";
|
return $"{GetType()} {Position}[{Chunk.Index}]";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Tick() {
|
protected virtual void Tick() {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class Level {
|
|||||||
private int chunkResY;
|
private int chunkResY;
|
||||||
|
|
||||||
public Level(Main main, int sizeX, int sizeY) {
|
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);
|
Resolution = new Vector2I(sizeX, sizeY);
|
||||||
chunksX = main.ChunksPerAxis;
|
chunksX = main.ChunksPerAxis;
|
||||||
@@ -26,15 +26,14 @@ public class Level {
|
|||||||
chunkResX = sizeX / chunksX;
|
chunkResX = sizeX / chunksX;
|
||||||
chunkResY = sizeY / chunksY;
|
chunkResY = sizeY / chunksY;
|
||||||
|
|
||||||
GD.Print($"{chunksX}x{chunksY} chunks. {chunkResX}:{chunkResY} res each.");
|
|
||||||
|
|
||||||
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
||||||
|
|
||||||
chunks = new Chunk[chunksX, chunksY];
|
chunks = new Chunk[chunksX, chunksY];
|
||||||
|
int index = 0;
|
||||||
// create all chunks
|
// create all chunks
|
||||||
for (int x = 0; x < chunksX; x++) {
|
for (int x = 0; x < chunksX; x++) {
|
||||||
for (int y = 0; y < chunksY; y++) {
|
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 > 0) chunks[x, y].NeighborE = chunks[x-1, y];
|
||||||
if (x < chunksX-1) chunks[x, y].NeighborW = chunks[x+1, y];
|
if (x < chunksX-1) chunks[x, y].NeighborW = chunks[x+1, y];
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,5 +43,3 @@ public partial class Main : Node2D {
|
|||||||
} else base._UnhandledInput(@event);
|
} else base._UnhandledInput(@event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user