diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 9ae3504..2b6958e 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -2,8 +2,9 @@ [ext_resource type="Script" path="res://Scripts/Main.cs" id="1_k1i8e"] -[node name="Node2D" type="Node2D"] +[node name="Main" type="Node2D"] script = ExtResource("1_k1i8e") +BrushSize = 10 [node name="CanvasLayer" type="CanvasLayer" parent="."] diff --git a/Scripts/Elements/Dirt.cs b/Scripts/Elements/Dirt.cs index d061c8e..b2ee35f 100644 --- a/Scripts/Elements/Dirt.cs +++ b/Scripts/Elements/Dirt.cs @@ -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; + } + } diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs index 5df907f..3933e02 100644 --- a/Scripts/Elements/Element.cs +++ b/Scripts/Elements/Element.cs @@ -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; + } + + /// + /// base update method, checks if anything is to do at all + /// + /// + /// false if there is nothing to do + 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; + } } diff --git a/Scripts/Elements/Solid.cs b/Scripts/Elements/Solid.cs index 02ccf3f..ae91f76 100644 --- a/Scripts/Elements/Solid.cs +++ b/Scripts/Elements/Solid.cs @@ -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) { } } diff --git a/Scripts/Level.cs b/Scripts/Level.cs index a5d724f..8903527 100644 --- a/Scripts/Level.cs +++ b/Scripts/Level.cs @@ -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; + } + } diff --git a/Scripts/Main.cs b/Scripts/Main.cs index 3d17374..d409459 100644 --- a/Scripts/Main.cs +++ b/Scripts/Main.cs @@ -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()); }