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); protected int lastMove = 0; private bool active = false; private bool wasMovedThisTick = false; private int lastUpdate = -1; private Color originalColor; 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; wasMovedThisTick = true; } public bool Active { get => active; set { if (active == value) return; active = value; Chunk.SetElementActive(this, value); 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; if (lastUpdate == Engine.GetFramesDrawn()) return false; // already updated this frame lastUpdate = Engine.GetFramesDrawn(); return true; } public override string ToString() { return $"{GetType()} {Position}[{Chunk.Index}] {active}"; } protected virtual void Tick() { Vector2I randomDirection = RandomDirectionDown(); wasMovedThisTick = false; 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; } public void SetDebugColor(Color color) { if (!Main.Instance.DebugMode) return; this.color = color; wasMovedThisTick = true; } public void Moved() { lastMove = Engine.GetFramesDrawn(); wasMovedThisTick = true; } public bool WasMoved() { return wasMovedThisTick; } }