diff --git a/Scenes/main.tscn b/Scenes/main.tscn
index 3fb7cd9..0c4b008 100644
--- a/Scenes/main.tscn
+++ b/Scenes/main.tscn
@@ -6,6 +6,7 @@
script = ExtResource("1_k1i8e")
BrushSize = 3
TextureResolution = 0.25
+RainAmount = 0.15
[node name="CanvasLayer" type="CanvasLayer" parent="."]
diff --git a/Scripts/Elements/Dirt.cs b/Scripts/Elements/Dirt.cs
index a2b139e..1a90177 100644
--- a/Scripts/Elements/Dirt.cs
+++ b/Scripts/Elements/Dirt.cs
@@ -6,9 +6,7 @@ public class Dirt : Solid {
public Dirt(int x, int y, ref Level level) : base(x, y, ref level) {
Color = Colors.Brown;
- Color.R += (GD.Randf() - 1) * MaxColorVariance;
- Color.G += (GD.Randf() - 1) * MaxColorVariance;
- Color.B += (GD.Randf() - 1) * MaxColorVariance;
+ Color = AddColorVariance(Color);
}
public override bool Update(int currentFrame) {
diff --git a/Scripts/Elements/Element.cs b/Scripts/Elements/Element.cs
index 1a266c0..29bcdf9 100644
--- a/Scripts/Elements/Element.cs
+++ b/Scripts/Elements/Element.cs
@@ -12,7 +12,7 @@ public class Element {
private int lastUpdate = -1;
- public Element(int x, int y, ref Level level) {
+ public Element(int x, int y, Level level) {
Position.X = x;
Position.Y = y;
Level = level;
@@ -52,4 +52,14 @@ public class Element {
/// free position or V2.zero if nothing was found
protected virtual Vector2I Check(Element source, Vector2I maxDirection) {
return Vector2I.Zero;
- }}
+ }
+
+ protected Color AddColorVariance(Color baseColor) {
+ Color c = baseColor;
+ c.R += (GD.Randf() - 1) * MaxColorVariance;
+ c.G += (GD.Randf() - 1) * MaxColorVariance;
+ c.B += (GD.Randf() - 1) * MaxColorVariance;
+ return c;
+ }
+
+}
diff --git a/Scripts/Elements/Liquid.cs b/Scripts/Elements/Liquid.cs
index 2c18957..302442f 100644
--- a/Scripts/Elements/Liquid.cs
+++ b/Scripts/Elements/Liquid.cs
@@ -3,24 +3,29 @@
namespace FOU.Scripts.Elements;
public abstract class Liquid : Element {
- protected Liquid(int x, int y, ref Level level) : base(x, y, ref level) { }
+ protected Liquid(int x, int y, ref Level level) : base(x, y, level) { }
+
+ // TODO: rework this!
protected override Vector2I Check(Element source, Vector2I maxDirection) {
if (GD.Randi() % 2 != 0)
maxDirection.X *= -1;
Vector2I freePos = Vector2I.Zero;
- for (int y = 0; y <= maxDirection.Y; y++) { // height
+ for (int y = maxDirection.Y; y > 0; y--) { // height
if (source.Position.Y + y >= Level.SizeY) return freePos; // bounds check
+ // check if we can further
if (Level.Get(source.Position.X, source.Position.Y + y).GetType() == typeof(Element)) {
freePos = new Vector2I(source.Position.X, source.Position.Y + y);
+ } else {
+ freePos = Vector2I.Zero;
}
for (int x = -maxDirection.X; x <= maxDirection.X; x++) { // width
if (freePos != Vector2I.Zero) break;
- if (source.Position.X + x >= Level.SizeX || source.Position.X + x < 0) return freePos; // bounds check
+ if (source.Position.X + x >= Level.SizeX || source.Position.X + x < 0) continue; // bounds check
if (Level.Get(source.Position.X + x, source.Position.Y + y).GetType() == typeof(Element)) {
freePos = new Vector2I(source.Position.X + x, source.Position.Y + y);
diff --git a/Scripts/Elements/Snow.cs b/Scripts/Elements/Snow.cs
new file mode 100644
index 0000000..0345400
--- /dev/null
+++ b/Scripts/Elements/Snow.cs
@@ -0,0 +1,23 @@
+using Godot;
+
+namespace FOU.Scripts.Elements;
+
+public class Snow : Solid {
+ public Snow(int x, int y, ref Level level) : base(x, y, ref level) {
+ Color = Colors.White;
+ Color = AddColorVariance(Color);
+ }
+
+ public override bool Update(int currentFrame) {
+ if (!base.Update(currentFrame)) return false;
+
+ Vector2I freePos = Check(this, Vector2I.Down);
+
+ if (freePos != Vector2I.Zero) { // diagonally right
+ Level.Swap(this, freePos);
+ return true;
+ }
+
+ return true; // not necessarily end, subclasses could do some more things
+ }
+}
diff --git a/Scripts/Elements/Solid.cs b/Scripts/Elements/Solid.cs
index 343ecfa..4627bb3 100644
--- a/Scripts/Elements/Solid.cs
+++ b/Scripts/Elements/Solid.cs
@@ -3,7 +3,7 @@
namespace FOU.Scripts.Elements;
public abstract class Solid : Element {
- protected Solid(int x, int y, ref Level level) : base(x, y, ref level) { }
+ protected Solid(int x, int y, ref Level level) : base(x, y, level) { }
protected Vector2I Check(Element source, Vector2I maxDirection) {
if (GD.Randi() % 2 != 0)
diff --git a/Scripts/Level.cs b/Scripts/Level.cs
index b14b5e0..068d4ef 100644
--- a/Scripts/Level.cs
+++ b/Scripts/Level.cs
@@ -12,10 +12,12 @@ public class Level {
private Element[,] _elements;
private Level _this;
+ private Main _main;
- public Level(int x, int y) {
+ public Level(int x, int y, Main main) {
GD.Print($"Generating level ({x}:{y})");
_this = this;
+ _main = main;
SizeX = x;
SizeY = y;
@@ -23,7 +25,7 @@ public class Level {
_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);
+ _elements[i,j] = new Element(i, j, _this);
}
}
@@ -39,16 +41,27 @@ public class Level {
}
}
_frame++;
+ MakeItRain(_main.RainAmount);
}
- public void WritePixel(int x, int y, int size) {
+ private void MakeItRain(float rainAmount) {
+ if (GD.Randf() < rainAmount) {
+ WritePixel((int)(GD.Randi() % SizeX), 0, 1);
+ }
+ }
+
+ public void WritePixel(int x, int y, int size) {
int halfsize = size/2;
+ Type type = typeof(T);
+
for (int i = -halfsize; i <= halfsize; i++) {
for (int j = -halfsize; j <= halfsize; j++) {
int X = Mathf.Clamp(x + i, 0, SizeX-1);
int Y = Mathf.Clamp(y + j, 0, SizeY-1);
- _elements[X,Y] = new Dirt(X, Y, ref _this);
+ object o = Activator.CreateInstance(type, X, Y, _this);
+
+ _elements[X,Y] = o as Element;
}
}
}
diff --git a/Scripts/Main.cs b/Scripts/Main.cs
index 0105cc5..8132101 100644
--- a/Scripts/Main.cs
+++ b/Scripts/Main.cs
@@ -1,16 +1,18 @@
using FOU.Scripts;
+using FOU.Scripts.Elements;
using Godot;
public partial class Main : Node2D {
[Export] public int BrushSize = 5;
[Export] public float TextureResolution = 0.5f;
+ [Export] public float RainAmount = 0.01f;
private TextureRect mLevelDrawer;
private Level mLevel;
public override void _Ready() {
mLevel = new Level((int)(GetViewportRect().Size.X * TextureResolution),
- (int)(GetViewportRect().Size.Y * TextureResolution)
+ (int)(GetViewportRect().Size.Y * TextureResolution), this
);
mLevelDrawer = GetNode("CanvasLayer/LevelDrawer");
}
@@ -29,7 +31,7 @@ public partial class Main : Node2D {
float mappedX = mouse.X / GetViewportRect().Size.X * mLevel.SizeX;
float mappedY = mouse.Y / GetViewportRect().Size.Y * mLevel.SizeY;
- mLevel.WritePixel((int)mappedX, (int)mappedY, BrushSize);
+ mLevel.WritePixel((int)mappedX, (int)mappedY, BrushSize);
}
}
}