35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using Godot;
|
|
|
|
namespace FOU.Scripts.Elements;
|
|
|
|
public abstract class Liquid : Element {
|
|
public const int MAX_VERTICAL_SPREAD = 3;
|
|
|
|
protected Liquid(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 override void Tick() {
|
|
Vector2I randomDirection = RandomDirectionDown();
|
|
if (randomDirection.Y != 0)
|
|
randomDirection *= Vector2I.Left * (int)(GD.Randi() % MAX_VERTICAL_SPREAD);
|
|
|
|
if (Chunk.IsEmpty(Position + Vector2I.Down))
|
|
Chunk.Swap(this, Position + Vector2I.Down);
|
|
|
|
else if (Chunk.IsEmpty(Position + randomDirection))
|
|
Chunk.Swap(this, Position + Vector2I.Right * randomDirection);
|
|
|
|
else if (Chunk.IsEmpty(Position + randomDirection))
|
|
Chunk.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE);
|
|
}
|
|
}
|