init
This commit is contained in:
9
Scripts/Elements/Dirt.cs
Normal file
9
Scripts/Elements/Dirt.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public class Dirt : Solid {
|
||||
public Dirt() {
|
||||
Color = Colors.Brown;
|
||||
}
|
||||
}
|
||||
7
Scripts/Elements/Element.cs
Normal file
7
Scripts/Elements/Element.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Godot;
|
||||
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public abstract class Element {
|
||||
public Color Color = Colors.Black;
|
||||
}
|
||||
5
Scripts/Elements/Solid.cs
Normal file
5
Scripts/Elements/Solid.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public abstract class Solid : Element {
|
||||
|
||||
}
|
||||
44
Scripts/Level.cs
Normal file
44
Scripts/Level.cs
Normal 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
26
Scripts/Main.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user