Files
FOU/Scripts/Level.cs
2024-08-25 18:45:38 +02:00

86 lines
2.4 KiB
C#

using Godot;
namespace FOU.Scripts;
public class Level {
public Vector2I Resolution;
private Chunk[,] chunks;
private Image image;
private int chunksX;
private int chunksY;
// per chunk:
private int chunkResX;
private int chunkResY;
public Level(Main main, int sizeX, int sizeY) {
GD.Print($"Generating level ({sizeX}:{sizeY}) with {chunksX*chunksY} chunks ({chunkResX} * {chunkResY})");
Resolution = new Vector2I(sizeX, sizeY);
chunksX = main.ChunksPerAxis;
chunksY = main.ChunksPerAxis;
chunkResX = sizeX / chunksX;
chunkResY = sizeY / chunksY;
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, index++);
}
}
// assign neighbors
for (int x = 0; x < chunksX; x++) {
for (int y = 0; y < chunksY; y++) {
if (y > 0) chunks[x, y].NeighborN = chunks[x, y-1];
if (y < chunksY-1) chunks[x, y].NeighborS = chunks[x, y+1];
if (x > 0) chunks[x, y].NeighborE = chunks[x-1, y];
if (x < chunksX-1) chunks[x, y].NeighborW = chunks[x+1, y];
}
}
}
public void Update() {
foreach (Chunk c in chunks)
c.Update();
}
// TODO!
private void MakeItRain(float rainAmount) {
// int rainDrops = (int)Math.Round(rainAmount);
//
// for (int i = 0; i <= rainDrops; i++) {
// if (GD.Randf() < rainAmount)
// WritePixel<Water>((int)(GD.Randi() % SizeX), 0, 1);
// }
}
public void WritePixel<T>(int x, int y, int size) {
chunks[x/chunkResX, y/chunkResY].WritePixel<T>(x % chunkResX, y % chunkResY, size);
}
public Image DrawLevel() {
for (int cx = 0; cx < chunksX; cx++) {
for (int cy = 0; cy < chunksY; cy++) {
for (int x = 0; x < chunkResX; x++) {
for (int y = 0; y < chunkResY; y++) {
image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx,cy].Elements[x, y].Color);
}
}
}
}
return image;
}
}