added falling sand - dirt actually
This commit is contained in:
@@ -3,7 +3,18 @@
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public class Dirt : Solid {
|
||||
public Dirt() {
|
||||
|
||||
public Dirt(int x, int y, ref Level level) : base(x, y, ref level) {
|
||||
Color = Colors.Brown;
|
||||
}
|
||||
|
||||
public override bool Update(int currentFrame) {
|
||||
if (!base.Update(currentFrame)) return false;
|
||||
|
||||
if (CheckBelow(X, Y)) {
|
||||
_level.Swap(this, X, Y+1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,44 @@
|
||||
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public abstract class Element {
|
||||
public class Element {
|
||||
public Color Color = Colors.Black;
|
||||
|
||||
public int X;
|
||||
public int Y;
|
||||
|
||||
protected Level _level;
|
||||
|
||||
private int _lastUpdate = -1;
|
||||
|
||||
public Element(int x, int y, ref Level level) {
|
||||
X = x;
|
||||
Y = y;
|
||||
_level = level;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// base update method, checks if anything is to do at all
|
||||
/// </summary>
|
||||
/// <param name="currentFrame"></param>
|
||||
/// <returns>false if there is nothing to do</returns>
|
||||
public virtual bool Update(int currentFrame) {
|
||||
if (_lastUpdate == currentFrame) return false; // already updated this frame
|
||||
_lastUpdate = currentFrame;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"{X}:{Y}";
|
||||
}
|
||||
|
||||
protected bool CheckBelow(int x, int y) {
|
||||
if (y+1 >= _level.SizeY) return false;
|
||||
|
||||
if (_level.Get(x, y+1).GetType() == GetType())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public abstract class Solid : Element {
|
||||
|
||||
protected Solid(int x, int y, ref Level level) : base(x, y, ref level) { }
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ using FOU.Scripts;
|
||||
using Godot;
|
||||
|
||||
public partial class Main : Node2D {
|
||||
[Export] public int BrushSize = 10;
|
||||
[Export] public int BrushSize = 5;
|
||||
|
||||
private TextureRect mLevelDrawer;
|
||||
private Level mLevel;
|
||||
@@ -13,6 +13,8 @@ public partial class Main : Node2D {
|
||||
}
|
||||
|
||||
public override void _Process(double delta) {
|
||||
|
||||
mLevel.Update();
|
||||
mLevelDrawer.Texture = ImageTexture.CreateFromImage(mLevel.DrawLevel());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user