added chunks

This commit is contained in:
2024-07-14 17:22:37 +02:00
parent 5d07477de5
commit 31edd511e0
8 changed files with 213 additions and 124 deletions

View File

@@ -4,7 +4,7 @@ namespace FOU.Scripts.Elements;
public class Dirt : Solid {
public Dirt(int x, int y, ref Level level) : base(x, y, ref level) {
public Dirt(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
Color = AddColorVariance(Colors.Brown);
Density = 10;
DiffuseSpeed = 50;

View File

@@ -6,6 +6,7 @@ public class Element {
public Color Color = Colors.Black;
public Vector2I Position;
public Chunk Chunk;
public int Density;
public int LastUpdate = -1;
public int LastMove = 0;
@@ -16,20 +17,19 @@ public class Element {
protected const float MAX_COLOR_VARIANCE = 0.1f;
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
protected readonly Level Level;
private bool active = true;
private Color originalColor;
public Element(Element e) {
Position = e.Position;
Level = e.Level;
Chunk = e.Chunk;
}
public Element(int x, int y, Level level) {
public Element(int x, int y, Chunk chunk) {
Position.X = x;
Position.Y = y;
Level = level;
Chunk = chunk;
}
public bool Active {
@@ -65,19 +65,19 @@ public class Element {
protected virtual void Tick() {
Vector2I randomDirection = RandomDirectionDown();
if (Level.IsEmpty(Position + Vector2I.Down))
Level.Swap(this, Position + Vector2I.Down);
if (Chunk.IsEmpty(Position + Vector2I.Down))
Chunk.Swap(this, Position + Vector2I.Down);
else if (Level.IsEmpty(Position + randomDirection))
Level.Swap(this, Position + randomDirection);
else if (Chunk.IsEmpty(Position + randomDirection))
Chunk.Swap(this, Position + randomDirection);
else if (Level.IsEmpty(Position + randomDirection * VERTICAL_OPPOSITE))
Level.Swap(this, Position + randomDirection * VERTICAL_OPPOSITE);
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 (Level.Get(Position + randomDirection)?.Density < Density)
Level.Swap(this, Position + Vector2I.Down);
if (Chunk.Get(Position + randomDirection)?.Density < Density)
Chunk.Swap(this, Position + Vector2I.Down);
}
protected Vector2I RandomDirectionDown() {

View File

@@ -5,7 +5,7 @@ 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) { }
protected Liquid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { }
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false;
@@ -21,13 +21,13 @@ public abstract class Liquid : Element {
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);
if (Chunk.IsEmpty(Position + Vector2I.Down))
Chunk.Swap(this, Position + Vector2I.Down);
else if (Level.IsEmpty(Position + randomDirection))
Level.Swap(this, Position + Vector2I.Right * randomDirection);
else if (Chunk.IsEmpty(Position + randomDirection))
Chunk.Swap(this, Position + Vector2I.Right * randomDirection);
else if (Level.IsEmpty(Position + randomDirection))
Level.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE);
else if (Chunk.IsEmpty(Position + randomDirection))
Chunk.Swap(this, Position + Vector2I.Right * randomDirection * VERTICAL_OPPOSITE);
}
}

View File

@@ -3,7 +3,7 @@
namespace FOU.Scripts.Elements;
public abstract class Solid : Element {
protected Solid(int x, int y, ref Level level) : base(x, y, level) { }
protected Solid(int x, int y, ref Chunk chunk) : base(x, y, chunk) { }
public override bool Update(int currentFrame) {
if (!base.Update(currentFrame)) return false;

View File

@@ -4,7 +4,7 @@ namespace FOU.Scripts.Elements;
public class Water : Liquid {
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
public Water(int x, int y, ref Chunk chunk) : base(x, y, ref chunk) {
Color = AddColorVariance(Colors.Blue);
Density = 1;
}