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

@@ -10,11 +10,4 @@ public class Dirt : Solid {
DiffuseSpeed = 50;
}
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false;
Tick();
return true; // not necessarily end, subclasses could do some more things
}
}

View File

@@ -7,15 +7,19 @@ public class Element {
public Vector2I Position;
public int Density;
public int LastUpdate = -1;
public int LastMove = 0;
public int DiffuseSpeed = 10;
public const int MAX_DIFFUSE_SPEED = 100;
public const int STEPS_UNTIL_INACTIVE = 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;
private bool active = true;
private Color originalColor;
public Element(Element e) {
Position = e.Position;
@@ -28,14 +32,28 @@ public class Element {
Level = level;
}
public bool Active {
get => active;
set {
active = value;
if (active)
Color = originalColor;
else if (Main.Instance.DebugVisualization)
Color = new Color(0.2f, 0.2f, 0.2f);
}
}
/// <summary>
/// base update method, checks if anything is to do at all
/// </summary>
/// <param name="currentFrame"></param>
/// <returns>false if there is nothing to do</returns>
public virtual bool Update(int currentFrame) {
if (lastUpdate == currentFrame) return false; // already updated this frame
lastUpdate = currentFrame;
if (!Active) return false;
if (LastUpdate == currentFrame) return false; // already updated this frame
LastUpdate = currentFrame;
return true;
}
@@ -74,6 +92,8 @@ public class Element {
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;
}

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);
}
}

View File

@@ -5,4 +5,12 @@ namespace FOU.Scripts.Elements;
public abstract class Solid : Element {
protected Solid(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
}
}

View File

@@ -2,34 +2,11 @@
namespace FOU.Scripts.Elements;
public class Water : Solid {
public const int MAX_VERTICAL_SPREAD = 3;
public class Water : Liquid {
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
Color = AddColorVariance(Colors.Blue);
Density = 1;
}
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) 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 (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);
}
}