43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using Godot;
|
|
|
|
namespace FOU.Scripts.Elements;
|
|
|
|
public abstract class Solid : Element {
|
|
protected Solid(int x, int y, ref Chunk chunk) : base(x, y, chunk) {
|
|
MarkForUpdate();
|
|
}
|
|
|
|
public override bool Update() {
|
|
if (!base.Update()) return false;
|
|
|
|
Tick();
|
|
|
|
return true; // not necessarily end, subclasses could do some more things
|
|
}
|
|
|
|
protected virtual void Tick() {
|
|
Vector2I randomDirection = RandomDirectionDown();
|
|
|
|
// fall speed reduction:
|
|
if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // descend slower
|
|
|
|
if (Chunk.IsEmpty(Position + Vector2I.Down))
|
|
Chunk.Swap(this, Position + Vector2I.Down);
|
|
|
|
else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection))
|
|
Chunk.Swap(this, Position + Vector2I.Down + randomDirection);
|
|
|
|
else if (Chunk.IsEmpty(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE))
|
|
Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE);
|
|
|
|
// density check
|
|
if (Chunk.Get(Position + Vector2I.Down)?.Density < Density)
|
|
Chunk.Swap(this, Position + Vector2I.Down);
|
|
else if (Chunk.Get(Position + Vector2I.Down + randomDirection)?.Density < Density)
|
|
Chunk.Swap(this, Position + Vector2I.Down + randomDirection);
|
|
else if (Chunk.Get(Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE)?.Density < Density)
|
|
Chunk.Swap(this, Position + Vector2I.Down + randomDirection * VERTICAL_OPPOSITE);
|
|
}
|
|
|
|
}
|