using Godot; namespace FOU.Scripts.Elements; public class Element{ public Chunk Chunk; public Vector2I Position; protected int Density; protected int DiffuseSpeed = 10; protected Color color = Colors.Black; protected const int MAX_DIFFUSE_SPEED = 100; protected const int STEPS_UNTIL_INACTIVE = 500; protected const float MAX_COLOR_VARIANCE = 0.1f; protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1); private bool active = false; private int lastUpdate = -1; private int lastMove = 0; private Color originalColor; private bool markedForUpdate = false; public Element(Element e) { Position = e.Position; Chunk = e.Chunk; lastMove = Engine.GetFramesDrawn(); } public Element(int x, int y, Chunk chunk) { Position.X = x; Position.Y = y; Chunk = chunk; lastMove = Engine.GetFramesDrawn(); Active = false; } public bool Active { get => active; set { if (active == value) return; active = value; Chunk.SetElementActive(this, value); if (!active) ResetColor(); else Moved(); } } public Color Color => color; /// /// base update method, checks if anything is to do at all /// /// /// false if there is nothing to do public virtual bool Update() { if (!Active) return false; int frame = Engine.GetFramesDrawn(); if (lastMove + STEPS_UNTIL_INACTIVE < frame) Active = false; if (lastUpdate == frame) return false; // already updated this frame lastUpdate = frame; return true; } public override string ToString() { return $"{GetType()} {Position}[{Chunk.Index}] {active}"; } protected virtual void Tick() { Vector2I randomDirection = RandomDirectionDown(); if (Chunk.IsEmpty(Position + Vector2I.Down)) Chunk.Swap(this, Position + Vector2I.Down); else if (Chunk.IsEmpty(Position + randomDirection)) Chunk.Swap(this, Position + randomDirection); else if (Chunk.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE)) Chunk.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE); if (GD.Randi() % MAX_DIFFUSE_SPEED > DiffuseSpeed) return; // ascend slower if (Chunk.Get(Position + randomDirection)?.Density < Density) Chunk.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; originalColor = c; return c; } public void ResetColor() { color = originalColor; MarkForUpdate(); } public void SetDebugColor(Color color) { if (!Main.Instance.DebugMode) return; this.color = color; } public void Moved() { lastMove = Engine.GetFramesDrawn(); MarkForUpdate(); } public bool MarkedForUpdate() { return markedForUpdate; } public void MarkForUpdate(bool mark = true) { markedForUpdate = true; } }