diffusion #12

Merged
ktyl merged 6 commits from diffusion into main 2022-12-09 01:36:48 +01:00
5 changed files with 75 additions and 6 deletions
Showing only changes of commit beb59aebbb - Show all commits

View File

@ -4,3 +4,4 @@
[node name="Clock" type="Node"] [node name="Clock" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )
_tickRate = 10.0

View File

@ -7,6 +7,7 @@ anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
script = ExtResource( 1 ) script = ExtResource( 1 )
TicksLabelPath = NodePath("Ticks") TicksLabelPath = NodePath("Ticks")
PauseLabelPath = NodePath("Paused")
[node name="Ticks" type="Label" parent="."] [node name="Ticks" type="Label" parent="."]
anchor_left = 1.0 anchor_left = 1.0
@ -14,9 +15,22 @@ anchor_top = 1.0
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
margin_right = -10.0 margin_right = -10.0
margin_bottom = -30.0
grow_horizontal = 0
grow_vertical = 0
text = "ticks: 0"
align = 2
valign = 2
[node name="Paused" type="Label" parent="."]
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_right = -10.0
margin_bottom = -10.0 margin_bottom = -10.0
grow_horizontal = 0 grow_horizontal = 0
grow_vertical = 0 grow_vertical = 0
text = "ticks: 0" text = "pause: false"
align = 2 align = 2
valign = 2 valign = 2

View File

@ -8,6 +8,7 @@
[node name="Root" type="Node"] [node name="Root" type="Node"]
[node name="Grid" parent="." instance=ExtResource( 1 )] [node name="Grid" parent="." instance=ExtResource( 1 )]
DiffusionCoefficient = 0.1
[node name="Cursor" parent="." instance=ExtResource( 2 )] [node name="Cursor" parent="." instance=ExtResource( 2 )]
Grid = NodePath("../Grid") Grid = NodePath("../Grid")
@ -15,6 +16,8 @@ Grid = NodePath("../Grid")
[node name="Debug UI" parent="." instance=ExtResource( 3 )] [node name="Debug UI" parent="." instance=ExtResource( 3 )]
[node name="Clock" parent="." instance=ExtResource( 4 )] [node name="Clock" parent="." instance=ExtResource( 4 )]
_autoTick = true
[connection signal="OnPauseChanged" from="Clock" to="Debug UI" method="_on_Clock_OnPauseChanged"]
[connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"] [connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"]
[connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"] [connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"]

View File

@ -5,8 +5,18 @@ public class Clock : Node
{ {
[Signal] [Signal]
delegate void OnTick(int ticks); delegate void OnTick(int ticks);
[Signal]
delegate void OnPauseChanged(bool paused);
[Export(PropertyHint.None, "Number of ticks per second")]
private float _tickRate;
private float TickPeriod => 1.0f / _tickRate;
[Export]
private bool _autoTick;
private int _ticks = 0; private int _ticks = 0;
private bool _paused = false;
public override void _Input(InputEvent @event) public override void _Input(InputEvent @event)
{ {
@ -17,12 +27,38 @@ public class Clock : Node
switch ((KeyList)keyEvent.Scancode) switch ((KeyList)keyEvent.Scancode)
{ {
case KeyList.Space: case KeyList.Space:
if (_autoTick)
{
_paused = !_paused;
EmitSignal(nameof(OnPauseChanged), _paused);
return;
}
Tick(); Tick();
break; break;
} }
} }
} }
private float _secondsSinceLastTick;
public override void _Process(float delta)
{
base._Process(delta);
if (!_autoTick)
return;
if (_paused)
return;
_secondsSinceLastTick += delta;
if (_secondsSinceLastTick < TickPeriod)
return;
_secondsSinceLastTick -= TickPeriod;
Tick();
}
private void Tick() private void Tick()
{ {
_ticks++; _ticks++;

View File

@ -6,17 +6,32 @@ public class DebugUI : Control
[Export] [Export]
public NodePath TicksLabelPath { private get; set; } public NodePath TicksLabelPath { private get; set; }
private Label _ticksLabel; private Label _ticksLabel;
[Export]
public NodePath PauseLabelPath { private get; set; }
private Label _pauseLabel;
public override void _Ready() public override void _Ready()
{ {
_ticksLabel = GetNode<Label>(TicksLabelPath); _ticksLabel = GetNode<Label>(TicksLabelPath);
_pauseLabel = GetNode<Label>(PauseLabelPath);
SetTicksText(0); SetTicksText(0);
SetPauseText(false);
} }
#region Signals
public void _on_Clock_OnPauseChanged(bool paused) => SetPauseText(paused);
public void _on_Clock_OnTick(int ticks) => SetTicksText(ticks); public void _on_Clock_OnTick(int ticks) => SetTicksText(ticks);
#endregion
private void SetTicksText(int ticks) private void SetTicksText(int ticks)
{ {
_ticksLabel.Text = $"ticks: {ticks}"; _ticksLabel.Text = $"ticks: {ticks}";
} }
private void SetPauseText(bool paused)
{
_pauseLabel.Text = $"pause: {paused}";
}
} }