From beb59aebbb8c7f4e5e77b78a29410d2776dfaad0 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Thu, 8 Dec 2022 19:37:10 +0000 Subject: [PATCH] auto-tick, pausable clock #4 --- half-earth/nodes/clock.tscn | 1 + half-earth/nodes/debug_ui.tscn | 16 +++++++++++++- half-earth/nodes/game.tscn | 3 +++ half-earth/scripts/Clock.cs | 40 ++++++++++++++++++++++++++++++++-- half-earth/scripts/DebugUI.cs | 21 +++++++++++++++--- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/half-earth/nodes/clock.tscn b/half-earth/nodes/clock.tscn index 7d4c9fe..2c5bc42 100644 --- a/half-earth/nodes/clock.tscn +++ b/half-earth/nodes/clock.tscn @@ -4,3 +4,4 @@ [node name="Clock" type="Node"] script = ExtResource( 1 ) +_tickRate = 10.0 diff --git a/half-earth/nodes/debug_ui.tscn b/half-earth/nodes/debug_ui.tscn index 1cf603b..4f2ab7f 100644 --- a/half-earth/nodes/debug_ui.tscn +++ b/half-earth/nodes/debug_ui.tscn @@ -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 diff --git a/half-earth/nodes/game.tscn b/half-earth/nodes/game.tscn index a23d717..9b75583 100644 --- a/half-earth/nodes/game.tscn +++ b/half-earth/nodes/game.tscn @@ -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"] diff --git a/half-earth/scripts/Clock.cs b/half-earth/scripts/Clock.cs index 9050073..3fb7456 100644 --- a/half-earth/scripts/Clock.cs +++ b/half-earth/scripts/Clock.cs @@ -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++; diff --git a/half-earth/scripts/DebugUI.cs b/half-earth/scripts/DebugUI.cs index f8a8bdb..8adc836 100644 --- a/half-earth/scripts/DebugUI.cs +++ b/half-earth/scripts/DebugUI.cs @@ -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