finalized dirt
added base for liquids
This commit is contained in:
@@ -11,10 +11,14 @@ public class Dirt : Solid {
|
||||
public override bool Update(int currentFrame) {
|
||||
if (!base.Update(currentFrame)) return false;
|
||||
|
||||
if (CheckBelow(X, Y)) {
|
||||
_level.Swap(this, X, Y+1);
|
||||
Vector2I freePos = Check(this, Vector2I.Down + Vector2I.Right);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,16 +5,15 @@ namespace FOU.Scripts.Elements;
|
||||
public class Element {
|
||||
public Color Color = Colors.Black;
|
||||
|
||||
public int X;
|
||||
public int Y;
|
||||
public Vector2I Position;
|
||||
|
||||
protected Level _level;
|
||||
|
||||
private int _lastUpdate = -1;
|
||||
|
||||
public Element(int x, int y, ref Level level) {
|
||||
X = x;
|
||||
Y = y;
|
||||
Position.X = x;
|
||||
Position.Y = y;
|
||||
_level = level;
|
||||
}
|
||||
|
||||
@@ -31,15 +30,25 @@ public class Element {
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"{GetType()} {X}:{Y}";
|
||||
return $"{GetType()} {Position}";
|
||||
}
|
||||
|
||||
protected bool CheckBelow(int x, int y) {
|
||||
if (y+1 >= _level.SizeY) return false;
|
||||
// OBSOLETE:
|
||||
// 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())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Checks from source to maxDirection (also X-mirrored) and returns a free position
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}}
|
||||
|
||||
34
Scripts/Elements/Liquid.cs
Normal file
34
Scripts/Elements/Liquid.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,36 @@
|
||||
namespace FOU.Scripts.Elements;
|
||||
using Godot;
|
||||
|
||||
namespace FOU.Scripts.Elements;
|
||||
|
||||
public abstract class Solid : Element {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user