added simple sleep state for elements

refactoring
This commit is contained in:
2024-03-23 18:47:24 +01:00
parent 015f86cec1
commit d44412c666
9 changed files with 76 additions and 41 deletions

View File

@@ -1,9 +1,33 @@
using System;
using Godot;
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 Level level) : base(x, y, level) { }
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false;
if (LastMove + STEPS_UNTIL_INACTIVE < currentFrame) Active = 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 (Level.IsEmpty(Position + Vector2I.Down))
Level.Swap(this, Position + Vector2I.Down);
else if (Level.IsEmpty(Position + randomDirection))
Level.Swap(this, Position + Vector2I.Right * randomDirection);
else if (Level.IsEmpty(Position + randomDirection))
Level.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE);
}
}