diff --git a/src/game/hud.tscn b/src/game/hud.tscn new file mode 100644 index 0000000..a4cd584 --- /dev/null +++ b/src/game/hud.tscn @@ -0,0 +1,48 @@ +[gd_scene load_steps=4 format=3 uid="uid://cdlbh3smpgg42"] + +[ext_resource type="Script" path="res://src/game/hud_controller.gd" id="1_ckegs"] + +[sub_resource type="LabelSettings" id="LabelSettings_pv71o"] +font_size = 40 + +[sub_resource type="LabelSettings" id="LabelSettings_cu1nb"] +font_size = 34 + +[node name="hud" type="Control" node_paths=PackedStringArray("speed", "distance")] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_ckegs") +speed = NodePath("Speedometer") +distance = NodePath("Distancemeter") + +[node name="Speedometer" type="Label" parent="."] +layout_mode = 1 +anchors_preset = -1 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -220.0 +offset_top = -103.0 +grow_horizontal = 0 +grow_vertical = 0 +text = "speed" +label_settings = SubResource("LabelSettings_pv71o") +horizontal_alignment = 2 +vertical_alignment = 2 + +[node name="Distancemeter" type="Label" parent="."] +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -20.0 +offset_right = 20.0 +offset_bottom = 23.0 +grow_horizontal = 2 +text = "distance" +label_settings = SubResource("LabelSettings_cu1nb") diff --git a/src/game/hud_controller.gd b/src/game/hud_controller.gd new file mode 100644 index 0000000..15264f8 --- /dev/null +++ b/src/game/hud_controller.gd @@ -0,0 +1,15 @@ +extends Control +#references +## reference to the speedometer label +@export var speed: Label +## reference to the distance meter label +@export var distance: Label + +func _on_player_speed_changed(speed_float:float): + var speed_string = "%.02f" % speed_float + print_debug("changing speed display to ", speed_string) + speed.set_text(speed_string) + +func _on_level_distance_changed(distance_float): + var distance_string ="%.f" % distance_float + distance.set_text(distance_string) diff --git a/src/game/level.gd b/src/game/level.gd index da41ae5..2242594 100644 --- a/src/game/level.gd +++ b/src/game/level.gd @@ -21,6 +21,8 @@ extends Node2D ## reference to the line node used to draw the finish line @export var finish: Line2D +signal distance_changed(new_distance) + #variables var width var running = true @@ -79,6 +81,7 @@ func _process(delta): print_debug("adding a lap") lap_distance += path.curve.get_baked_length() last_offset = offset + distance_changed.emit(get_total_distance()) # print_debug("player distance driven: ", get_total_distance(), " distance since last check: ", off_diff) diff --git a/src/game/level1.tscn b/src/game/level1.tscn index d4a52a9..7f24796 100644 --- a/src/game/level1.tscn +++ b/src/game/level1.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=6 format=3 uid="uid://c77xk0mywwt5f"] +[gd_scene load_steps=7 format=3 uid="uid://c77xk0mywwt5f"] [ext_resource type="Script" path="res://src/game/level.gd" id="1_hv75i"] [ext_resource type="PackedScene" uid="uid://ckau5s2tsb3oc" path="res://src/game/player.tscn" id="2_5nf51"] [ext_resource type="Texture2D" uid="uid://bd0jqkgq1gh7p" path="res://assets/track.png" id="2_knecs"] +[ext_resource type="PackedScene" uid="uid://cdlbh3smpgg42" path="res://src/game/hud.tscn" id="2_w64nx"] [ext_resource type="Texture2D" uid="uid://cd7bqj3v0k8yi" path="res://assets/finish.png" id="4_r6pvu"] [sub_resource type="Curve2D" id="Curve2D_ll8bj"] @@ -36,3 +37,11 @@ texture_mode = 1 [node name="player" parent="." instance=ExtResource("2_5nf51")] position = Vector2(42.7545, -14.9219) brake_strength = 800.0 + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="hud" parent="CanvasLayer" instance=ExtResource("2_w64nx")] +size_flags_horizontal = 3 + +[connection signal="distance_changed" from="." to="CanvasLayer/hud" method="_on_level_distance_changed"] +[connection signal="speed_changed" from="player" to="CanvasLayer/hud" method="_on_player_speed_changed"] diff --git a/src/game/player_controller.gd b/src/game/player_controller.gd index d9a2840..430953a 100644 --- a/src/game/player_controller.gd +++ b/src/game/player_controller.gd @@ -26,6 +26,8 @@ extends StaticBody2D var momentum: Vector2 #var direction = 0 # 1 for forward, 0 for standing still, -1 for reverse +signal speed_changed(new_speed) + # Called when the node enters the scene tree for the first time. func _ready(): pass @@ -73,4 +75,5 @@ func _process(delta): if momentum.length() > max_speed: momentum = momentum.normalized() * max_speed + speed_changed.emit(momentum.length()) move_and_collide(momentum * delta)