Add HUD and functionality to actually be able to play

This commit is contained in:
James Skemp 2023-10-10 20:52:11 -05:00
parent b50b099141
commit 0dbdc834ca
4 changed files with 114 additions and 2 deletions

41
HUD.gd Normal file
View File

@ -0,0 +1,41 @@
extends CanvasLayer
# Will notify Main node that start button has been pressed.
signal start_game
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func show_message(text):
$Message.text = text
$Message.show()
$MessageTimer.start()
func show_game_over():
show_message("Game Over")
# Wait until timer has counted down.
await $MessageTimer.timeout
$Message.text = "Dodge the\nCreeps!"
$Message.show()
# Make a one-shot timer and wait for it to finish.
await get_tree().create_timer(1.0).timeout
$StartButton.show()
func update_score(score):
$ScoreLabel.text = str(score)
func _on_message_timer_timeout():
$Message.hide()
func _on_start_button_pressed():
$StartButton.hide()
start_game.emit()

63
hud.tscn Normal file
View File

@ -0,0 +1,63 @@
[gd_scene load_steps=3 format=3 uid="uid://da1pa0jmrsvrn"]
[ext_resource type="Script" path="res://HUD.gd" id="1_7sw6g"]
[ext_resource type="FontFile" uid="uid://bxnsxbkp1qyba" path="res://fonts/Xolonium-Regular.ttf" id="2_rm0d0"]
[node name="HUD" type="CanvasLayer"]
script = ExtResource("1_7sw6g")
[node name="ScoreLabel" type="Label" parent="."]
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -22.5
offset_right = 22.5
offset_bottom = 81.0
grow_horizontal = 2
theme_override_fonts/font = ExtResource("2_rm0d0")
theme_override_font_sizes/font_size = 64
text = "0"
horizontal_alignment = 1
vertical_alignment = 1
[node name="Message" type="Label" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -240.0
offset_top = -121.5
offset_right = 240.0
offset_bottom = 121.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2_rm0d0")
theme_override_font_sizes/font_size = 64
text = "Dodge the creeps!"
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="StartButton" type="Button" parent="."]
anchors_preset = 7
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -100.0
offset_top = -140.0
offset_right = 100.0
offset_bottom = -40.0
grow_horizontal = 2
grow_vertical = 0
theme_override_fonts/font = ExtResource("2_rm0d0")
theme_override_font_sizes/font_size = 64
text = "Start"
[node name="MessageTimer" type="Timer" parent="."]
wait_time = 2.0
one_shot = true
[connection signal="pressed" from="StartButton" to="." method="_on_start_button_pressed"]
[connection signal="timeout" from="MessageTimer" to="." method="_on_message_timer_timeout"]

View File

@ -6,20 +6,24 @@ var score
# Called when the node enters the scene tree for the first time.
func _ready():
new_game()
pass
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
$HUD.show_game_over()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
$HUD.update_score(score)
$HUD.show_message("Get Ready")
func _on_score_timer_timeout():
score += 1
$HUD.update_score(score)
func _on_start_timer_timeout():

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://q3yqxe6odx72"]
[gd_scene load_steps=6 format=3 uid="uid://q3yqxe6odx72"]
[ext_resource type="PackedScene" uid="uid://8jqt5klugv5b" path="res://player.tscn" id="1_qfr2y"]
[ext_resource type="Script" path="res://main.gd" id="1_sbeq7"]
[ext_resource type="PackedScene" uid="uid://bco33bjwfjfk4" path="res://mob.tscn" id="2_k78is"]
[ext_resource type="PackedScene" uid="uid://da1pa0jmrsvrn" path="res://hud.tscn" id="4_ie066"]
[sub_resource type="Curve2D" id="Curve2D_w0qsk"]
_data = {
@ -33,7 +34,10 @@ curve = SubResource("Curve2D_w0qsk")
[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
[node name="HUD" parent="." instance=ExtResource("4_ie066")]
[connection signal="hit" from="Player" to="." method="game_over"]
[connection signal="timeout" from="MobTimer" to="." method="_on_mob_timer_timeout"]
[connection signal="timeout" from="ScoreTimer" to="." method="_on_score_timer_timeout"]
[connection signal="timeout" from="StartTimer" to="." method="_on_start_timer_timeout"]
[connection signal="start_game" from="HUD" to="." method="new_game"]