From 90c65dbcf9809885368a6b86a430d7e8bb7a6d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 18 Nov 2021 20:44:53 +0100 Subject: [PATCH] Add wrong way indicator --- scenes/test_level.tscn | 32 ++++++++++++++++++++++++++------ scenes/track.gd | 18 +++++++++++++----- scenes/trackgui.gd | 12 +++++++++--- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/scenes/test_level.tscn b/scenes/test_level.tscn index 0cde694..382c80c 100644 --- a/scenes/test_level.tscn +++ b/scenes/test_level.tscn @@ -123,11 +123,15 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="HBoxContainer" type="HBoxContainer" parent="TrackGUI"] +[node name="VBoxContainer" type="VBoxContainer" parent="TrackGUI"] margin_right = 984.0 margin_bottom = 560.0 -[node name="TimeLabel" type="Label" parent="TrackGUI/HBoxContainer"] +[node name="HBoxContainer" type="HBoxContainer" parent="TrackGUI/VBoxContainer"] +margin_right = 984.0 +margin_bottom = 41.0 + +[node name="TimeLabel" type="Label" parent="TrackGUI/VBoxContainer/HBoxContainer"] margin_right = 289.0 margin_bottom = 41.0 size_flags_horizontal = 3 @@ -138,7 +142,7 @@ text = "Current lap -" align = 1 valign = 1 -[node name="TimeValue" type="Label" parent="TrackGUI/HBoxContainer"] +[node name="TimeValue" type="Label" parent="TrackGUI/VBoxContainer/HBoxContainer"] margin_left = 297.0 margin_right = 489.0 margin_bottom = 41.0 @@ -151,7 +155,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Divider" type="Label" parent="TrackGUI/HBoxContainer"] +[node name="Divider" type="Label" parent="TrackGUI/VBoxContainer/HBoxContainer"] margin_left = 497.0 margin_right = 562.0 margin_bottom = 41.0 @@ -163,7 +167,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="BestTimeLabel" type="Label" parent="TrackGUI/HBoxContainer"] +[node name="BestTimeLabel" type="Label" parent="TrackGUI/VBoxContainer/HBoxContainer"] margin_left = 570.0 margin_right = 784.0 margin_bottom = 41.0 @@ -173,7 +177,7 @@ custom_colors/font_color = Color( 0.482353, 0, 0, 1 ) custom_fonts/font = SubResource( 8 ) text = "Best Lap -" -[node name="BestTimeValue" type="Label" parent="TrackGUI/HBoxContainer"] +[node name="BestTimeValue" type="Label" parent="TrackGUI/VBoxContainer/HBoxContainer"] margin_left = 792.0 margin_right = 984.0 margin_bottom = 41.0 @@ -186,5 +190,21 @@ __meta__ = { "_edit_use_anchors_": false } +[node name="WrongWayLabel" type="Label" parent="TrackGUI/VBoxContainer"] +visible = false +margin_top = 49.0 +margin_right = 984.0 +margin_bottom = 90.0 +size_flags_horizontal = 3 +size_flags_vertical = 0 +custom_colors/font_color = Color( 0.482353, 0, 0, 1 ) +custom_fonts/font = SubResource( 8 ) +text = "Wrong Way! Turn Around!" +align = 1 +__meta__ = { +"_edit_use_anchors_": false +} + [connection signal="lap_complete" from="Track" to="TrackGUI" method="_on_lap_complete"] [connection signal="time_updated" from="Track" to="TrackGUI" method="_on_time_updated"] +[connection signal="wrong_way" from="Track" to="TrackGUI" method="_on_wrong_way_detected"] diff --git a/scenes/track.gd b/scenes/track.gd index 7d9075f..3d0ce12 100644 --- a/scenes/track.gd +++ b/scenes/track.gd @@ -3,6 +3,7 @@ extends Spatial signal time_updated(new_time) signal lap_complete(lap_time) +signal wrong_way() export (NodePath) var track_path = null export (int, 10, 50) var checkpoint_count = 20 @@ -11,6 +12,7 @@ export (Material) var debug_material = null onready var checkpoints = $Checkpoints onready var path: Path = get_node(track_path) +var furthest_checkpoint = -1 var last_checkpoint = -1 var start_time = 0 @@ -47,15 +49,21 @@ func _process(delta: float) -> void: func _on_body_entered_area(body: Node, area: Area) -> void: if body.get_groups().has("car"): - # We got the correct checkpoint - if last_checkpoint == area.get_index() - 1: - last_checkpoint += 1 - if last_checkpoint == checkpoints.get_child_count() - 1: + if area.get_index() < last_checkpoint || abs(area.get_index() - last_checkpoint) > 1: + emit_signal("wrong_way") + + last_checkpoint = area.get_index() + + # We got the correct checkpoint + if furthest_checkpoint == area.get_index() - 1: + furthest_checkpoint += 1 + + if furthest_checkpoint == checkpoints.get_child_count() - 1: emit_signal("lap_complete", current_time) start_time = OS.get_ticks_msec() _update_time() - last_checkpoint = -1 + furthest_checkpoint = -1 func _build_checkpoint_collision(): diff --git a/scenes/trackgui.gd b/scenes/trackgui.gd index 97f2b92..6acae4f 100644 --- a/scenes/trackgui.gd +++ b/scenes/trackgui.gd @@ -2,8 +2,9 @@ extends MarginContainer var best_time = -1 -onready var time_value = $HBoxContainer/TimeValue -onready var best_time_value = $HBoxContainer/BestTimeValue +onready var time_value = $VBoxContainer/HBoxContainer/TimeValue +onready var best_time_value = $VBoxContainer/HBoxContainer/BestTimeValue +onready var wrong_way_label = $VBoxContainer/WrongWayLabel func _ready() -> void: time_value.text = "NaN" @@ -15,12 +16,17 @@ func _on_time_updated(new_time: float) -> void: func _on_lap_complete(lap_time: float) -> void: - print(lap_time) if lap_time < best_time or best_time < 0: best_time = lap_time best_time_value.text = _format_time(best_time) +func _on_wrong_way_detected() -> void: + wrong_way_label.visible = true + yield(get_tree().create_timer(3.0), "timeout") + wrong_way_label.visible = false + + func _format_time(time: float) -> String: var minute = floor(time / 1000.0 / 60.0) var second = floor((time / 1000.0) - minute * 60)