using FOU.Scripts.Elements; using Godot; namespace FOU.Scripts; public class Level { public int SizeX; public int SizeY; private Image mImage; private int _frame; private Element[,] _elements; private Level _this; public Level(int x, int y) { GD.Print($"Generating level ({x}:{y})"); _this = this; 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, 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, 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 < SizeX; x++) { for (int y = 0; y < SizeY; y++) { // if (_elements[x,y] == null) continue; 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; } }