Files
FOU/Scripts/Elements/Element.cs
rogo eb94c3bef9 added density
code improvements
2023-12-02 00:45:09 +01:00

75 lines
2.2 KiB
C#

using Godot;
namespace FOU.Scripts.Elements;
public class Element {
public Color Color = Colors.Black;
public Vector2I Position;
public int Density;
protected readonly float MaxColorVariance = 0.1f;
protected readonly Level Level;
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;
Level = level;
}
/// <summary>
/// base update method, checks if anything is to do at all
/// </summary>
/// <param name="currentFrame"></param>
/// <returns>false if there is nothing to do</returns>
public virtual bool Update(int currentFrame) {
if (lastUpdate == currentFrame) return false; // already updated this frame
lastUpdate = currentFrame;
return true;
}
public override string ToString() {
return $"{GetType()} {Position}";
}
protected virtual void Tick() {
int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1;
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);
}
if (GD.Randi() % 10 > 1) return; // ascend slower
if (Level.Get(Position + Vector2I.Down
// maybe also side
+ (GD.Randi() % 2 != 0 ?
Vector2I.Zero : Vector2I.Right * randomDirection)
)?.Density < Density)
Level.Swap(this, Position + Vector2I.Down);
}
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;
}
}