finalized dirt

added base for liquids
This commit is contained in:
2023-10-17 14:29:42 +02:00
parent 9145a6a3f9
commit 31dbbbf070
6 changed files with 113 additions and 29 deletions

View File

@@ -4,8 +4,8 @@
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_k1i8e") script = ExtResource("1_k1i8e")
BrushSize = 10 BrushSize = 3
TextureResolution = 0.33 TextureResolution = 0.25
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]

View File

@@ -11,10 +11,14 @@ public class Dirt : Solid {
public override bool Update(int currentFrame) { public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false; if (!base.Update(currentFrame)) return false;
if (CheckBelow(X, Y)) { Vector2I freePos = Check(this, Vector2I.Down + Vector2I.Right);
_level.Swap(this, X, Y+1);
if (freePos != Vector2I.Zero) { // diagonally right
_level.Swap(this, freePos);
return true;
} }
return true;
return true; // not necessarily end, subclasses could do some more things
} }
} }

View File

@@ -5,16 +5,15 @@ namespace FOU.Scripts.Elements;
public class Element { public class Element {
public Color Color = Colors.Black; public Color Color = Colors.Black;
public int X; public Vector2I Position;
public int Y;
protected Level _level; protected Level _level;
private int _lastUpdate = -1; private int _lastUpdate = -1;
public Element(int x, int y, ref Level level) { public Element(int x, int y, ref Level level) {
X = x; Position.X = x;
Y = y; Position.Y = y;
_level = level; _level = level;
} }
@@ -31,15 +30,25 @@ public class Element {
} }
public override string ToString() { public override string ToString() {
return $"{GetType()} {X}:{Y}"; return $"{GetType()} {Position}";
} }
protected bool CheckBelow(int x, int y) { // OBSOLETE:
if (y+1 >= _level.SizeY) return false; // protected bool CheckBelow(int sourceX, int sourceY) {
// if (sourceY+1 >= _level.SizeY) return false;
//
// if (_level.Get(sourceX, sourceY+1).GetType() == GetType())
// return false;
//
// return true;
// }
if (_level.Get(x, y+1).GetType() == GetType()) /// <summary>
return false; /// Checks from source to maxDirection (also X-mirrored) and returns a free position
/// </summary>
return true; /// <param name="source">from where to check</param>
} /// <param name="maxDirection">where to go to (max). X is treated as [-X..X]</param>
} /// <returns>free position or V2.zero if nothing was found</returns>
protected virtual Vector2I Check(Element source, Vector2I maxDirection) {
return Vector2I.Zero;
}}

View File

@@ -0,0 +1,34 @@
using Godot;
namespace FOU.Scripts.Elements;
public abstract class Liquid : Element {
protected Liquid(int x, int y, ref Level level) : base(x, y, ref level) { }
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
if (source.Position.Y + y >= _level.SizeY) return freePos; // bounds check
if (_level.Get(source.Position.X, source.Position.Y + y).GetType() == typeof(Element)) {
freePos = new Vector2I(source.Position.X, source.Position.Y + y);
}
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 (_level.Get(source.Position.X + x, source.Position.Y + y).GetType() == typeof(Element)) {
freePos = new Vector2I(source.Position.X + x, source.Position.Y + y);
break;
}
}
}
return freePos;
}
}

View File

@@ -1,5 +1,36 @@
namespace FOU.Scripts.Elements; using Godot;
namespace FOU.Scripts.Elements;
public abstract class Solid : Element { 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, ref level) { }
protected Vector2I Check(Element source, Vector2I maxDirection) {
if (GD.Randi() % 2 != 0)
maxDirection.X *= -1;
Vector2I freePos = Vector2I.Zero;
for (int y = maxDirection.Y; y > 0; y--) { // height
if (source.Position.Y + y >= _level.SizeY) return freePos; // bounds check
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 (_level.Get(source.Position.X + x, source.Position.Y + y).GetType() == typeof(Element)) {
freePos = new Vector2I(source.Position.X + x, source.Position.Y + y);
break;
}
}
}
return freePos;
}
} }

View File

@@ -1,4 +1,5 @@
using FOU.Scripts.Elements; using System;
using FOU.Scripts.Elements;
using Godot; using Godot;
namespace FOU.Scripts; namespace FOU.Scripts;
@@ -63,13 +64,20 @@ public class Level {
return mImage; return mImage;
} }
public void Swap(Element what, int toSwapX, int toSwapY) { public void Swap(Element what, Vector2I swapTo) {
Element old = Get(toSwapX, toSwapY); Element old = Get(swapTo);
Set(old, what.X, what.Y); Set(old, what.Position);
Set(what, toSwapX, toSwapY); Set(what, swapTo);
} }
public Element Get(Vector2I where) {
if (where.X < 0 || where.X >= SizeX) return null;
if (where.Y < 0 || where.Y >= SizeY) return null;
return _elements[where.X, where.Y];
}
public Element Get(int x, int y) { public Element Get(int x, int y) {
if (x < 0 || x >= SizeX) return null; if (x < 0 || x >= SizeX) return null;
if (y < 0 || y >= SizeY) return null; if (y < 0 || y >= SizeY) return null;
@@ -77,12 +85,10 @@ public class Level {
return _elements[x,y]; return _elements[x,y];
} }
private void Set(Element what, int x, int y) { private void Set(Element what, Vector2I where) {
if (what == null) return; if (what == null) return;
what.X = x; what.Position = where;
what.Y = y; _elements[where.X, where.Y] = what;
_elements[x,y] = what;
} }
} }