Skip to content

Commit

Permalink
Add high scores feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanito committed Nov 25, 2023
1 parent a192390 commit 19c53fd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
19 changes: 19 additions & 0 deletions HighScores.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends Node

var high_scores = {}


func add_time(level, score):
if not high_scores.has(level):
high_scores[level] = score
return

if score < high_scores[level]:
high_scores[level] = score


func get_best_time(level):
if not high_scores.has(level):
return null

return high_scores[level]
7 changes: 7 additions & 0 deletions level_completed.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ signal next_level

@onready var restart_button = %RestartButton
@onready var next_level_button = %NextLevelButton
@onready var your_time_label = %YourTimeLabel
@onready var best_time_label = %BestTimeLabel


func _ready():
next_level_button.grab_focus()


func set_times(your_time, best_time):
your_time_label.text = "Your Time: " + str(your_time / 1000.0)
best_time_label.text = "Best Time: " + str(best_time / 1000.0)


func _on_restart_button_pressed():
restart.emit()

Expand Down
28 changes: 23 additions & 5 deletions level_completed.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,38 @@ layout_mode = 2
text = "Level Completed!"
horizontal_alignment = 1

[node name="HBoxContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
[node name="TimeLabelContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
theme_override_constants/separation = 12

[node name="NextLevelButton" type="Button" parent="CenterContainer/VBoxContainer/HBoxContainer"]
[node name="YourTimeLabel" type="Label" parent="CenterContainer/VBoxContainer/TimeLabelContainer"]
unique_name_in_owner = true
layout_mode = 2
theme_override_font_sizes/font_size = 6
text = "Your Time: 17.281"

[node name="BestTimeLabel" type="Label" parent="CenterContainer/VBoxContainer/TimeLabelContainer"]
unique_name_in_owner = true
layout_mode = 2
theme_override_font_sizes/font_size = 6
text = "Best Time: 12.381"

[node name="ButtonContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4

[node name="NextLevelButton" type="Button" parent="CenterContainer/VBoxContainer/ButtonContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(64, 16)
layout_mode = 2
text = "Next Level"

[node name="RestartButton" type="Button" parent="CenterContainer/VBoxContainer/HBoxContainer"]
[node name="RestartButton" type="Button" parent="CenterContainer/VBoxContainer/ButtonContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(64, 16)
layout_mode = 2
text = "Restart"

[connection signal="pressed" from="CenterContainer/VBoxContainer/HBoxContainer/NextLevelButton" to="." method="_on_next_level_button_pressed"]
[connection signal="pressed" from="CenterContainer/VBoxContainer/HBoxContainer/RestartButton" to="." method="_on_restart_button_pressed"]
[connection signal="pressed" from="CenterContainer/VBoxContainer/ButtonContainer/NextLevelButton" to="." method="_on_next_level_button_pressed"]
[connection signal="pressed" from="CenterContainer/VBoxContainer/ButtonContainer/RestartButton" to="." method="_on_restart_button_pressed"]
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config/icon="res://icon.svg"

Events="*res://Events.gd"
LevelTransition="*res://level_transition.tscn"
HighScores="*res://HighScores.gd"

[display]

Expand Down
6 changes: 6 additions & 0 deletions world.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func _process(_delta):

func show_level_completed():
level_completed.show()

var level_name = get_tree().current_scene.name
HighScores.add_time(level_name, level_time)
level_completed.set_times(level_time, HighScores.get_best_time(level_name))
level_time_label.visible = false

get_tree().paused = true


Expand Down

0 comments on commit 19c53fd

Please sign in to comment.