added simple sleep state for elements
refactoring
This commit is contained in:
@@ -14,10 +14,10 @@ public partial class SettingsController : VBoxContainer
|
|||||||
slRain = GetNode<Slider>("weatherSettings/slRain");
|
slRain = GetNode<Slider>("weatherSettings/slRain");
|
||||||
|
|
||||||
slSize.ValueChanged += OnSizeValueChanged;
|
slSize.ValueChanged += OnSizeValueChanged;
|
||||||
OnSizeValueChanged(main.BrushSize);
|
slSize.Value = main.BrushSize;
|
||||||
|
|
||||||
slRain.ValueChanged += OnRainValueChanged;
|
slRain.ValueChanged += OnRainValueChanged;
|
||||||
OnRainValueChanged(main.RainAmount);
|
slRain.Value = main.RainAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnSizeValueChanged(double value) {
|
private void OnSizeValueChanged(double value) {
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
[node name="Main" type="Node2D"]
|
[node name="Main" type="Node2D"]
|
||||||
script = ExtResource("1_k1i8e")
|
script = ExtResource("1_k1i8e")
|
||||||
BrushSize = 1
|
BrushSize = 1
|
||||||
RainAmount = 1.0
|
TextureResolution = 0.25
|
||||||
|
RainAmount = 0.0
|
||||||
|
|
||||||
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,4 @@ public class Dirt : Solid {
|
|||||||
DiffuseSpeed = 50;
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,19 @@ public class Element {
|
|||||||
|
|
||||||
public Vector2I Position;
|
public Vector2I Position;
|
||||||
public int Density;
|
public int Density;
|
||||||
|
public int LastUpdate = -1;
|
||||||
|
public int LastMove = 0;
|
||||||
|
|
||||||
public int DiffuseSpeed = 10;
|
public int DiffuseSpeed = 10;
|
||||||
public const int MAX_DIFFUSE_SPEED = 100;
|
public const int MAX_DIFFUSE_SPEED = 100;
|
||||||
|
public const int STEPS_UNTIL_INACTIVE = 100;
|
||||||
|
|
||||||
protected const float MAX_COLOR_VARIANCE = 0.1f;
|
protected const float MAX_COLOR_VARIANCE = 0.1f;
|
||||||
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
protected static readonly Vector2I VERTICAL_OPPOSITE = new Vector2I(-1, 1);
|
||||||
protected readonly Level Level;
|
protected readonly Level Level;
|
||||||
|
|
||||||
private int lastUpdate = -1;
|
private bool active = true;
|
||||||
|
private Color originalColor;
|
||||||
|
|
||||||
public Element(Element e) {
|
public Element(Element e) {
|
||||||
Position = e.Position;
|
Position = e.Position;
|
||||||
@@ -28,14 +32,28 @@ public class Element {
|
|||||||
Level = level;
|
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>
|
/// <summary>
|
||||||
/// base update method, checks if anything is to do at all
|
/// base update method, checks if anything is to do at all
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="currentFrame"></param>
|
/// <param name="currentFrame"></param>
|
||||||
/// <returns>false if there is nothing to do</returns>
|
/// <returns>false if there is nothing to do</returns>
|
||||||
public virtual bool Update(int currentFrame) {
|
public virtual bool Update(int currentFrame) {
|
||||||
if (lastUpdate == currentFrame) return false; // already updated this frame
|
if (!Active) return false;
|
||||||
lastUpdate = currentFrame;
|
|
||||||
|
if (LastUpdate == currentFrame) return false; // already updated this frame
|
||||||
|
LastUpdate = currentFrame;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -74,6 +92,8 @@ public class Element {
|
|||||||
c.R += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
c.R += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
||||||
c.G += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
c.G += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
||||||
c.B += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
c.B += (GD.Randf() - 1) * MAX_COLOR_VARIANCE;
|
||||||
|
|
||||||
|
originalColor = c;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,33 @@
|
|||||||
using System;
|
using Godot;
|
||||||
using Godot;
|
|
||||||
|
|
||||||
namespace FOU.Scripts.Elements;
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
public abstract class Liquid : Element {
|
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,4 +5,12 @@ namespace FOU.Scripts.Elements;
|
|||||||
public abstract class Solid : Element {
|
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 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,34 +2,11 @@
|
|||||||
|
|
||||||
namespace FOU.Scripts.Elements;
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
public class Water : Solid {
|
public class Water : Liquid {
|
||||||
public const int MAX_VERTICAL_SPREAD = 3;
|
|
||||||
|
|
||||||
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
|
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
|
||||||
Color = AddColorVariance(Colors.Blue);
|
Color = AddColorVariance(Colors.Blue);
|
||||||
Density = 1;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ public class Level {
|
|||||||
int Y = Mathf.Clamp(y + j, 0, SizeY-1);
|
int Y = Mathf.Clamp(y + j, 0, SizeY-1);
|
||||||
object o = Activator.CreateInstance(type, X, Y, _this);
|
object o = Activator.CreateInstance(type, X, Y, _this);
|
||||||
|
|
||||||
|
if (_elements[X,Y].GetType() != type)
|
||||||
_elements[X,Y] = o as Element;
|
_elements[X,Y] = o as Element;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +76,7 @@ public class Level {
|
|||||||
return mImage;
|
return mImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Swap(Element what, Element swapTo) {
|
private void Swap(Element what, Element swapTo) {
|
||||||
Element swap = new Element(what);
|
Element swap = new Element(what);
|
||||||
|
|
||||||
if (what == null || swapTo == null) return;
|
if (what == null || swapTo == null) return;
|
||||||
@@ -85,6 +86,11 @@ public class Level {
|
|||||||
|
|
||||||
swapTo.Position = swap.Position;
|
swapTo.Position = swap.Position;
|
||||||
_elements[swap.Position.X, swap.Position.Y] = swapTo;
|
_elements[swap.Position.X, swap.Position.Y] = swapTo;
|
||||||
|
|
||||||
|
what.Active = true;
|
||||||
|
swapTo.Active = true;
|
||||||
|
what.LastMove = _frame;
|
||||||
|
swapTo.LastMove = _frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Swap(Element what, Vector2I pos) {
|
public void Swap(Element what, Vector2I pos) {
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ using FOU.Scripts.Elements;
|
|||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
public partial class Main : Node2D {
|
public partial class Main : Node2D {
|
||||||
|
[Export] public bool DebugVisualization = false;
|
||||||
|
|
||||||
[Export] public int BrushSize = 5;
|
[Export] public int BrushSize = 5;
|
||||||
[Export] public float TextureResolution = 0.5f;
|
[Export] public float TextureResolution = 0.5f;
|
||||||
[Export] public float RainAmount = 0.01f;
|
[Export] public float RainAmount = 0.01f;
|
||||||
|
|
||||||
|
public static Main Instance;
|
||||||
|
|
||||||
private TextureRect mLevelDrawer;
|
private TextureRect mLevelDrawer;
|
||||||
private Level mLevel;
|
private Level mLevel;
|
||||||
|
|
||||||
@@ -15,6 +19,8 @@ public partial class Main : Node2D {
|
|||||||
(int)(GetViewportRect().Size.Y * TextureResolution), this
|
(int)(GetViewportRect().Size.Y * TextureResolution), this
|
||||||
);
|
);
|
||||||
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
|
mLevelDrawer = GetNode<TextureRect>("CanvasLayer/LevelDrawer");
|
||||||
|
|
||||||
|
Instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(double delta) {
|
public override void _Process(double delta) {
|
||||||
|
|||||||
Reference in New Issue
Block a user