hierarchy rework

fixed swap
This commit is contained in:
2023-11-05 20:09:18 +01:00
parent 7670b2982a
commit 8278312aee
9 changed files with 75 additions and 120 deletions

View File

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

View File

@@ -12,6 +12,11 @@ public class Element {
private int lastUpdate = -1;
public Element(Element e) {
Position = e.Position;
Level = e.Level;
}
public Element(int x, int y, Level level) {
Position.X = x;
Position.Y = y;
@@ -34,24 +39,20 @@ public class Element {
return $"{GetType()} {Position}";
}
// 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;
// }
protected virtual void Tick() {
int randomDirection = 1;
if (GD.Randi() % 2 != 0)
randomDirection *= -1;
/// <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;
if (Level.IsEmpty(Position + Vector2I.Down)) {
Level.Swap(this, Position + Vector2I.Down);
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection)) {
Level.Swap(this, Position + Vector2I.Right * randomDirection);
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection * -1)) {
Level.Swap(this, Position + Vector2I.Right*randomDirection);
}
}
protected Color AddColorVariance(Color baseColor) {

View File

@@ -1,39 +1,9 @@
using Godot;
using System;
using Godot;
namespace FOU.Scripts.Elements;
public abstract class Liquid : Element {
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 = 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) 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);
break;
}
}
}
return freePos;
}
}

View File

@@ -1,23 +0,0 @@
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

@@ -5,32 +5,4 @@ namespace FOU.Scripts.Elements;
public abstract class Solid : Element {
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)
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;
}
}

35
Scripts/Elements/Water.cs Normal file
View File

@@ -0,0 +1,35 @@
using Godot;
namespace FOU.Scripts.Elements;
public class Water : Solid {
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
Color = Colors.Blue;
Color = AddColorVariance(Color);
}
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false;
Tick();
return true; // not necessarily end, subclasses could do some more things
}
protected override void Tick() {
int randomDirection = 1;
if (GD.Randi() % 2 != 0)
randomDirection *= -1;
if (Level.IsEmpty(Position + Vector2I.Down)) {
Level.Swap(this, Position + Vector2I.Down);
return;
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection)) {
Level.Swap(this, Position + Vector2I.Right * randomDirection);
return;
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection * -1)) {
Level.Swap(this, Position + Vector2I.Right*randomDirection);
return;
}
}
}