diffusion #12
@ -4,3 +4,4 @@
 | 
			
		||||
 | 
			
		||||
[node name="Clock" type="Node"]
 | 
			
		||||
script = ExtResource( 1 )
 | 
			
		||||
_tickRate = 10.0
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,7 @@ anchor_right = 1.0
 | 
			
		||||
anchor_bottom = 1.0
 | 
			
		||||
script = ExtResource( 1 )
 | 
			
		||||
TicksLabelPath = NodePath("Ticks")
 | 
			
		||||
PauseLabelPath = NodePath("Paused")
 | 
			
		||||
 | 
			
		||||
[node name="Ticks" type="Label" parent="."]
 | 
			
		||||
anchor_left = 1.0
 | 
			
		||||
@ -14,9 +15,22 @@ anchor_top = 1.0
 | 
			
		||||
anchor_right = 1.0
 | 
			
		||||
anchor_bottom = 1.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
 | 
			
		||||
grow_horizontal = 0
 | 
			
		||||
grow_vertical = 0
 | 
			
		||||
text = "ticks: 0"
 | 
			
		||||
text = "pause: false"
 | 
			
		||||
align = 2
 | 
			
		||||
valign = 2
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@
 | 
			
		||||
[node name="Root" type="Node"]
 | 
			
		||||
 | 
			
		||||
[node name="Grid" parent="." instance=ExtResource( 1 )]
 | 
			
		||||
DiffusionCoefficient = 0.1
 | 
			
		||||
 | 
			
		||||
[node name="Cursor" parent="." instance=ExtResource( 2 )]
 | 
			
		||||
Grid = NodePath("../Grid")
 | 
			
		||||
@ -15,6 +16,8 @@ Grid = NodePath("../Grid")
 | 
			
		||||
[node name="Debug UI" parent="." instance=ExtResource( 3 )]
 | 
			
		||||
 | 
			
		||||
[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="Debug UI" method="_on_Clock_OnTick"]
 | 
			
		||||
 | 
			
		||||
@ -5,8 +5,18 @@ public class Clock : Node
 | 
			
		||||
{
 | 
			
		||||
    [Signal]
 | 
			
		||||
    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 bool _paused = false;
 | 
			
		||||
 | 
			
		||||
    public override void _Input(InputEvent @event)
 | 
			
		||||
    {
 | 
			
		||||
@ -17,12 +27,38 @@ public class Clock : Node
 | 
			
		||||
            switch ((KeyList)keyEvent.Scancode)
 | 
			
		||||
            {
 | 
			
		||||
                case KeyList.Space:
 | 
			
		||||
                    if (_autoTick)
 | 
			
		||||
                    {
 | 
			
		||||
                        _paused = !_paused;
 | 
			
		||||
                        EmitSignal(nameof(OnPauseChanged), _paused);
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    Tick();
 | 
			
		||||
                    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()
 | 
			
		||||
    {
 | 
			
		||||
        _ticks++;
 | 
			
		||||
 | 
			
		||||
@ -6,17 +6,32 @@ public class DebugUI : Control
 | 
			
		||||
    [Export]
 | 
			
		||||
    public NodePath TicksLabelPath { private get; set; }
 | 
			
		||||
    private Label _ticksLabel;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    [Export]
 | 
			
		||||
    public NodePath PauseLabelPath { private get; set; }
 | 
			
		||||
    private Label _pauseLabel;
 | 
			
		||||
 | 
			
		||||
    public override void _Ready()
 | 
			
		||||
    {
 | 
			
		||||
        _ticksLabel = GetNode<Label>(TicksLabelPath);
 | 
			
		||||
        _pauseLabel = GetNode<Label>(PauseLabelPath);
 | 
			
		||||
 | 
			
		||||
        SetTicksText(0);
 | 
			
		||||
        SetPauseText(false);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    #region Signals
 | 
			
		||||
    public void _on_Clock_OnPauseChanged(bool paused) => SetPauseText(paused);
 | 
			
		||||
    public void _on_Clock_OnTick(int ticks) => SetTicksText(ticks);
 | 
			
		||||
    
 | 
			
		||||
    #endregion
 | 
			
		||||
 | 
			
		||||
    private void SetTicksText(int ticks)
 | 
			
		||||
    {
 | 
			
		||||
        _ticksLabel.Text = $"ticks: {ticks}";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SetPauseText(bool paused)
 | 
			
		||||
    {
 | 
			
		||||
        _pauseLabel.Text = $"pause: {paused}";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user