This commit is contained in:
2023-10-16 13:46:42 +02:00
commit 8a6004af24
13 changed files with 552 additions and 0 deletions

9
Scripts/Elements/Dirt.cs Normal file
View File

@@ -0,0 +1,9 @@
using Godot;
namespace FOU.Scripts.Elements;
public class Dirt : Solid {
public Dirt() {
Color = Colors.Brown;
}
}

View File

@@ -0,0 +1,7 @@
using Godot;
namespace FOU.Scripts.Elements;
public abstract class Element {
public Color Color = Colors.Black;
}

View File

@@ -0,0 +1,5 @@
namespace FOU.Scripts.Elements;
public abstract class Solid : Element {
}

44
Scripts/Level.cs Normal file
View File

@@ -0,0 +1,44 @@
using FOU.Scripts.Elements;
using Godot;
namespace FOU.Scripts;
public class Level {
private int mSizeX;
private int mSizeY;
private Image mImage;
private Element[,] mPixels;
public Level(int x, int y) {
GD.Print($"Generating level ({x}:{y})");
mSizeX = x;
mSizeY = y;
mPixels = new Element[x, y];
mImage = Image.Create(mSizeX, mSizeY, false, Image.Format.Rgb8);
mImage.Fill(Colors.Black);
}
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();
}
}
}
public Image DrawLevel() {
for (int x = 0; x < mSizeX; x++) {
for (int y = 0; y < mSizeY; y++) {
if (mPixels[x,y] == null) continue;
mImage.SetPixel(x,y, mPixels[x,y].Color);
}
}
return mImage;
}
}

26
Scripts/Main.cs Normal file
View File

@@ -0,0 +1,26 @@
using FOU.Scripts;
using Godot;
public partial class Main : Node2D {
[Export] public int BrushSize = 10;
private TextureRect mLevelDrawer;
private Level mLevel;
public override void _Ready() {
mLevel = new Level((int)GetViewportRect().Size.X, (int)GetViewportRect().Size.Y);
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
}
public override void _Process(double delta) {
mLevelDrawer.Texture = ImageTexture.CreateFromImage(mLevel.DrawLevel());
}
public override void _Input(InputEvent @event) {
if (@event is InputEventMouseButton eventMouseButton) {
Vector2 mouse = GetViewport().GetMousePosition();
mLevel.WritePixel((int)mouse.X, (int)mouse.Y, BrushSize);
}
}
}