using Godot; namespace FOU.Scripts.Elements; public class Element { public Color Color = Colors.Black; public Vector2I Position; public int Density; public int DiffuseSpeed = 10; public const int MAX_DIFFUSE_SPEED = 100; protected const float MAX_COLOR_VARIANCE = 0.1f; protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1); 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; } /// /// base update method, checks if anything is to do at all /// /// /// false if there is nothing to do 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() { Vector2I randomDirection = RandomDirectionDown(); if (Level.IsEmpty(Position + Vector2I.Down)) Level.Swap(this, Position + Vector2I.Down); else if (Level.IsEmpty(Position + randomDirection)) Level.Swap(this, Position + randomDirection); else if (Level.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE)) Level.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE); if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // ascend slower if (Level.Get(Position + randomDirection)?.Density < Density) Level.Swap(this, Position + Vector2I.Down); } protected Vector2I RandomDirectionDown() { int randomDirection = GD.Randi() % 2 != 0 ? 1 : -1; return Vector2I.Down // maybe also side + (GD.Randi() % 2 != 0 ? Vector2I.Zero : Vector2I.Right * randomDirection); } protected Color AddColorVariance(Color baseColor) { Color c = baseColor; c.R += (GD.Randf() - 1) * MAX_COLOR_VARIANCE; c.G += (GD.Randf() - 1) * MAX_COLOR_VARIANCE; c.B += (GD.Randf() - 1) * MAX_COLOR_VARIANCE; return c; } }