Finish heads_up_display of first 2D game

This commit is contained in:
Daniel Siepmann 2024-03-26 13:50:08 +01:00
parent dc649047bc
commit b433db6095
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
6 changed files with 109 additions and 2 deletions

34
first_2d_game/HUD.gd Normal file
View file

@ -0,0 +1,34 @@
extends CanvasLayer
# Notifies `Main` node that the button has been pressed
signal start_game
func show_message(text):
$Message.text = text
$Message.show()
$MessageTimer.start()
func show_game_over():
show_message('Game Over')
await $MessageTimer.timeout
$Message.text = 'Dodge the Creeps!'
$Message.show()
await get_tree().create_timer(1.0).timeout
$StartButton.show()
func update_score(score):
$ScoreLabel.text = str(score)
func _on_start_button_pressed():
$StartButton.hide()
start_game.emit()
func _on_message_timer_timeout():
$Message.hide()

63
first_2d_game/HUD.tscn Normal file
View file

@ -0,0 +1,63 @@
[gd_scene load_steps=3 format=3 uid="uid://caxwerqlguyo7"]
[ext_resource type="Script" path="res://HUD.gd" id="1_cp6uc"]
[ext_resource type="FontFile" uid="uid://dgjjxafsbuvej" path="res://fonts/Xolonium-Regular.ttf" id="1_pbe0f"]
[node name="HUD" type="CanvasLayer"]
script = ExtResource("1_cp6uc")
[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 = 78.0
grow_horizontal = 2
theme_override_fonts/font = ExtResource("1_pbe0f")
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 = -79.5
offset_right = 240.0
offset_bottom = 79.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("1_pbe0f")
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("1_pbe0f")
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

@ -36,6 +36,7 @@ func _on_mob_timer_timeout():
func _on_score_timer_timeout(): func _on_score_timer_timeout():
score += 1 score += 1
$HUD.update_score(score)
func _on_start_timer_timeout(): func _on_start_timer_timeout():
@ -46,9 +47,13 @@ func _on_start_timer_timeout():
func game_over(): func game_over():
$ScoreTimer.stop() $ScoreTimer.stop()
$MobTimer.stop() $MobTimer.stop()
$HUD.show_game_over()
func new_game(): func new_game():
score = 0 score = 0
$Player.start($StartPosition.position) $Player.start($StartPosition.position)
$StartTimer.start() $StartTimer.start()
$HUD.update_score(score)
$HUD.show_message('Get Ready')
get_tree().call_group('mobs', 'queue_free')

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://dmuja863phygg"] [gd_scene load_steps=6 format=3 uid="uid://dmuja863phygg"]
[ext_resource type="PackedScene" uid="uid://ckfhsdod4ghug" path="res://Player.tscn" id="1_4p8ah"] [ext_resource type="PackedScene" uid="uid://ckfhsdod4ghug" path="res://Player.tscn" id="1_4p8ah"]
[ext_resource type="Script" path="res://Main.gd" id="1_ftc8b"] [ext_resource type="Script" path="res://Main.gd" id="1_ftc8b"]
[ext_resource type="PackedScene" uid="uid://df0p8vv7unjvu" path="res://Mob.tscn" id="2_sjjbm"] [ext_resource type="PackedScene" uid="uid://df0p8vv7unjvu" path="res://Mob.tscn" id="2_sjjbm"]
[ext_resource type="PackedScene" uid="uid://caxwerqlguyo7" path="res://HUD.tscn" id="4_5inpl"]
[sub_resource type="Curve2D" id="Curve2D_7kfby"] [sub_resource type="Curve2D" id="Curve2D_7kfby"]
_data = { _data = {
@ -33,7 +34,10 @@ curve = SubResource("Curve2D_7kfby")
[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"] [node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
[node name="HUD" parent="." instance=ExtResource("4_5inpl")]
[connection signal="hit" from="Player" to="." method="game_over"] [connection signal="hit" from="Player" to="." method="game_over"]
[connection signal="timeout" from="MobTimer" to="." method="_on_mob_timer_timeout"] [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="ScoreTimer" to="." method="_on_score_timer_timeout"]
[connection signal="timeout" from="StartTimer" to="." method="_on_start_timer_timeout"] [connection signal="timeout" from="StartTimer" to="." method="_on_start_timer_timeout"]
[connection signal="start_game" from="HUD" to="." method="new_game"]

View file

@ -48,7 +48,7 @@ animations = [{
radius = 36.0 radius = 36.0
height = 96.0 height = 96.0
[node name="Mob" type="RigidBody2D"] [node name="Mob" type="RigidBody2D" groups=["mobs"]]
collision_mask = 0 collision_mask = 0
gravity_scale = 0.0 gravity_scale = 0.0
script = ExtResource("1_ruw5w") script = ExtResource("1_ruw5w")

View file

@ -11,6 +11,7 @@ config_version=5
[application] [application]
config/name="First 2d Game" config/name="First 2d Game"
run/main_scene="res://Main.tscn"
config/features=PackedStringArray("4.2", "GL Compatibility") config/features=PackedStringArray("4.2", "GL Compatibility")
config/icon="res://icon.svg" config/icon="res://icon.svg"