added falling sand - dirt actually

This commit is contained in:
2023-10-16 15:41:33 +02:00
parent 8a6004af24
commit fe40e33d36
6 changed files with 113 additions and 19 deletions

View File

@@ -4,41 +4,83 @@ using Godot;
namespace FOU.Scripts;
public class Level {
private int mSizeX;
private int mSizeY;
public int SizeX;
public int SizeY;
private Image mImage;
private int _frame;
private Element[,] mPixels;
private Element[,] _elements;
private Level _this;
public Level(int x, int y) {
GD.Print($"Generating level ({x}:{y})");
_this = this;
mSizeX = x;
mSizeY = y;
SizeX = x;
SizeY = y;
mPixels = new Element[x, y];
mImage = Image.Create(mSizeX, mSizeY, false, Image.Format.Rgb8);
_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, ref _this);
}
}
mImage = Image.Create(SizeX, SizeY, false, Image.Format.Rgb8);
mImage.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(_frame);
}
}
_frame++;
}
public void WritePixel(int x, int y, int size) {
for (int i = -size/2; i < size/2; i++) {
for (int j = -size/2; j < size/2; j++) {
int X = Mathf.Clamp(x + i, 0, mSizeX);
int Y = Mathf.Clamp(y + j, 0, mSizeY);
mPixels[X,Y] = new Dirt();
int X = Mathf.Clamp(x + i, 0, SizeX);
int Y = Mathf.Clamp(y + j, 0, SizeY);
_elements[X,Y] = new Dirt(X, Y, ref _this);
}
}
}
public Image DrawLevel() {
for (int x = 0; x < mSizeX; x++) {
for (int y = 0; y < mSizeY; y++) {
if (mPixels[x,y] == null) continue;
for (int x = 0; x < SizeX; x++) {
for (int y = 0; y < SizeY; y++) {
// if (_elements[x,y] == null) continue;
mImage.SetPixel(x,y, mPixels[x,y].Color);
mImage.SetPixel(x,y, _elements[x,y].Color);
}
}
return mImage;
}
public void Swap(Element what, int toSwapX, int toSwapY) {
Element old = Get(toSwapX, toSwapY);
Set(old, what.X, what.Y);
Set(what, toSwapX, toSwapY);
}
public Element Get(int x, int y) {
if (x < 0 || x >= SizeX) return null;
if (y < 0 || y >= SizeY) return null;
return _elements[x,y];
}
private void Set(Element what, int x, int y) {
if (what == null) return;
what.X = x;
what.Y = y;
_elements[x,y] = what;
}
}