added second element type: snow

added simple snow weather
This commit is contained in:
2023-10-17 15:11:07 +02:00
parent 43d121d4da
commit 7670b2982a
8 changed files with 67 additions and 15 deletions

View File

@@ -6,6 +6,7 @@
script = ExtResource("1_k1i8e")
BrushSize = 3
TextureResolution = 0.25
RainAmount = 0.15
[node name="CanvasLayer" type="CanvasLayer" parent="."]

View File

@@ -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) {

View File

@@ -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 {
/// <returns>free position or V2.zero if nothing was found</returns>
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;
}
}

View File

@@ -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);

23
Scripts/Elements/Snow.cs Normal file
View File

@@ -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
}
}

View File

@@ -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)

View File

@@ -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<Snow>((int)(GD.Randi() % SizeX), 0, 1);
}
}
public void WritePixel<T>(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;
}
}
}

View File

@@ -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<TextureRect>("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<Dirt>((int)mappedX, (int)mappedY, BrushSize);
}
}
}