fuzzyworld

godot-shooter / commit

Moved scores UI into its own scene.

a2c953357dc6f9ce7dd3af75e35ba8872a319d6f · Pavel Safronov · 2023-11-29 13:58:51 -0800 · parents ddc650198d8b

Moved scores UI into its own scene.
 actors/Player.gd   | 24 +--------------------
 actors/Player.tscn | 63 ++++++++++++++++++++++--------------------------------
 scenes/Scores.gd   | 26 ++++++++++++++++++++++
 scenes/Scores.tscn | 28 ++++++++++++++++++++++++
 scenes/World.gd    |  1 -
 5 files changed, 81 insertions(+), 61 deletions(-)

diff --git a/actors/Player.gd b/actors/Player.gd
index 9eae39b..95d8e59 100644
--- a/actors/Player.gd
+++ b/actors/Player.gd
@@ -41,7 +41,7 @@ const PopupControl = preload("res://utils/Popup.tscn")
 
 var weapon: Weapon = create_default_weapon()
 var dead = false
-var velocity: Vector2
+var velocity: Vector2 = Vector2.UP * move_speed
 var can_fire = true
 var settings: Settings
 
@@ -49,9 +49,6 @@ func create_default_weapon() -> Weapon:
 	return Weapons.create_basic_gun()
 
 func _ready() -> void:
-	velocity= Vector2.UP * move_speed
-	weapon = Weapons.create_basic_gun()
-
 #	Utils.remove_user_settings()
 	settings = Utils.load_user_settings()
 	settings.save_user_settings()
@@ -248,7 +245,6 @@ func show_message(message: String, duration_seconds: int = 2) -> void:
 
 func _on_HUD_paused() -> void:
 	get_tree().paused = true
-	set_pause_menu_high_scores()
 	$UI/PauseMenu.show()
 	clear_destination()
 
@@ -259,24 +255,6 @@ func _on_ResumeButton_pressed() -> void:
 func _on_HUD_debug_signal() -> void:
 	emit_signal("debug")
 
-func sort_high_scores(a, b):
-	return (a as Settings.HighScore).score > (b as Settings.HighScore).score
-
-func set_pause_menu_high_scores() -> void:
-	if settings.high_scores.size() > 0:
-		Utils.remove_all_children(scores_list)
-		settings.high_scores.sort_custom(self, "sort_high_scores")
-		var index = 0
-		var now = Time.get_datetime_dict_from_system()
-		for hs in settings.high_scores:
-			index += 1
-			var label = Label.new()
-			var date_dict = hs.date
-			var date_diff_secs = Utils.time_diff_secs(date_dict, now)
-			var date_diff = Utils.timespan_to_english(date_diff_secs)
-			label.text = "%s - %s points, %s" % [index, hs.score, date_diff]
-			scores_list.add_child(label)
-
 func set_destination(pos: Vector2) -> void:
 	destination = pos
 	is_destination_set = true
diff --git a/actors/Player.tscn b/actors/Player.tscn
index a8bea0d..f4b9b28 100644
--- a/actors/Player.tscn
+++ b/actors/Player.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=18 format=2]
+[gd_scene load_steps=19 format=2]
 
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/ship_sidesA.png" type="Texture" id=1]
 [ext_resource path="res://effects/Explosion.tscn" type="PackedScene" id=2]
@@ -6,6 +6,7 @@
 [ext_resource path="res://assets/kenney_simple-space/PNG/Retina/ship_B.png" type="Texture" id=4]
 [ext_resource path="res://themes/CharacterTheme.tres" type="Theme" id=5]
 [ext_resource path="res://effects/Trails.tscn" type="PackedScene" id=6]
+[ext_resource path="res://scenes/Scores.tscn" type="PackedScene" id=7]
 [ext_resource path="res://assets/neuropolitical rg.otf" type="DynamicFontData" id=8]
 [ext_resource path="res://themes/Theme.tres" type="Theme" id=9]
 [ext_resource path="res://scenes/HUD.tscn" type="PackedScene" id=10]
@@ -148,8 +149,9 @@ align = 1
 valign = 1
 
 [node name="FinalScoreLabel" type="Label" parent="UI/DeathScreen/CenterContainer/PanelContainer/VBoxContainer"]
-margin_right = 40.0
-margin_bottom = 25.0
+margin_top = 33.0
+margin_right = 272.0
+margin_bottom = 58.0
 
 [node name="StartNewGameButton" type="Button" parent="UI/DeathScreen/CenterContainer/PanelContainer/VBoxContainer"]
 margin_top = 135.0
@@ -242,7 +244,7 @@ rect_min_size = Vector2( 450, 235 )
 margin_left = 14.0
 margin_top = 14.0
 margin_right = 436.0
-margin_bottom = 277.0
+margin_bottom = 231.0
 
 [node name="Label" type="Label" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer"]
 margin_right = 422.0
@@ -254,30 +256,9 @@ align = 1
 [node name="TabContainer" type="TabContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer"]
 margin_top = 45.0
 margin_right = 422.0
-margin_bottom = 218.0
-
-[node name="Scores" type="VBoxContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
-anchor_right = 1.0
-anchor_bottom = 1.0
-margin_left = 8.0
-margin_top = 61.0
-margin_right = -8.0
-margin_bottom = -8.0
-
-[node name="Label" type="Label" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Scores"]
-margin_right = 406.0
-margin_bottom = 25.0
-text = "High scores:"
-
-[node name="ScoresList" type="VBoxContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Scores"]
-margin_top = 33.0
-margin_right = 406.0
-margin_bottom = 58.0
+margin_bottom = 172.0
 
-[node name="Label" type="Label" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Scores/ScoresList"]
-margin_right = 406.0
-margin_bottom = 25.0
-text = "No high scores yet!"
+[node name="Scores" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer" instance=ExtResource( 7 )]
 
 [node name="Pref" type="VBoxContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
 visible = false
@@ -323,9 +304,9 @@ focus_mode = 2
 text = "Source code"
 
 [node name="ResumeButton" type="Button" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer"]
-margin_top = 226.0
+margin_top = 180.0
 margin_right = 422.0
-margin_bottom = 263.0
+margin_bottom = 217.0
 theme = ExtResource( 5 )
 text = "Resume"
 
@@ -335,7 +316,6 @@ visible = false
 anchor_right = 1.0
 anchor_bottom = 1.0
 mouse_filter = 2
-theme = ExtResource( 9 )
 
 [node name="CenterContainer" type="CenterContainer" parent="UI/GameFinishedMenu"]
 anchor_right = 1.0
@@ -350,22 +330,31 @@ margin_bottom = 414.0
 [node name="VBoxContainer" type="VBoxContainer" parent="UI/GameFinishedMenu/CenterContainer/PanelContainer"]
 margin_left = 14.0
 margin_top = 14.0
-margin_right = 182.0
-margin_bottom = 208.0
+margin_right = 220.0
+margin_bottom = 217.0
 
 [node name="Label" type="Label" parent="UI/GameFinishedMenu/CenterContainer/PanelContainer/VBoxContainer"]
-margin_right = 168.0
-margin_bottom = 118.0
+margin_right = 206.0
+margin_bottom = 80.0
 theme = ExtResource( 9 )
 text = "You've won!"
 align = 1
 valign = 1
 autowrap = true
 
+[node name="Scores" parent="UI/GameFinishedMenu/CenterContainer/PanelContainer/VBoxContainer" instance=ExtResource( 7 )]
+anchor_right = 0.0
+anchor_bottom = 0.0
+margin_left = 0.0
+margin_top = 88.0
+margin_right = 206.0
+margin_bottom = 146.0
+
 [node name="Button" type="Button" parent="UI/GameFinishedMenu/CenterContainer/PanelContainer/VBoxContainer"]
-margin_top = 126.0
-margin_right = 168.0
-margin_bottom = 194.0
+margin_top = 154.0
+margin_right = 206.0
+margin_bottom = 203.0
+theme = ExtResource( 9 )
 text = "Replay!"
 
 [node name="Camera2D" type="Camera2D" parent="."]
diff --git a/scenes/Scores.gd b/scenes/Scores.gd
new file mode 100644
index 0000000..6ad2e20
--- /dev/null
+++ b/scenes/Scores.gd
@@ -0,0 +1,26 @@
+extends VBoxContainer
+
+const Utils = preload("res://utils/Utils.gd")
+const Weapons = preload("res://weapons/Weapons.gd")
+const PowerUp = preload("res://items/PowerUp.tscn")
+const Settings = preload("res://utils/Settings.gd")
+onready var scores_list = $ScoresList
+
+func _ready() -> void:
+	var settings = Utils.load_user_settings()
+	if settings.high_scores.size() > 0:
+		Utils.remove_all_children(scores_list)
+		settings.high_scores.sort_custom(self, "sort_high_scores")
+		var index = 0
+		var now = Time.get_datetime_dict_from_system()
+		for hs in settings.high_scores:
+			index += 1
+			var label = Label.new()
+			var date_dict = hs.date
+			var date_diff_secs = Utils.time_diff_secs(date_dict, now)
+			var date_diff = Utils.timespan_to_english(date_diff_secs)
+			label.text = " %s points, %s" % [hs.score, date_diff]
+			scores_list.add_child(label)
+
+func sort_high_scores(a, b):
+	return (a as Settings.HighScore).score > (b as Settings.HighScore).score
diff --git a/scenes/Scores.tscn b/scenes/Scores.tscn
new file mode 100644
index 0000000..9df778f
--- /dev/null
+++ b/scenes/Scores.tscn
@@ -0,0 +1,28 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://scenes/Scores.gd" type="Script" id=1]
+
+[node name="Scores" type="VBoxContainer"]
+anchor_right = 1.0
+anchor_bottom = 1.0
+margin_left = 8.0
+margin_top = 61.0
+margin_right = -8.0
+margin_bottom = -8.0
+script = ExtResource( 1 )
+
+[node name="Label" type="Label" parent="."]
+margin_right = 484.0
+margin_bottom = 25.0
+text = "High scores:"
+
+[node name="ScoresList" type="VBoxContainer" parent="."]
+margin_top = 33.0
+margin_right = 484.0
+margin_bottom = 58.0
+
+[node name="Label" type="Label" parent="ScoresList"]
+margin_top = 10.0
+margin_right = 484.0
+margin_bottom = 25.0
+text = "No high scores yet!"
diff --git a/scenes/World.gd b/scenes/World.gd
index 8765773..16da310 100644
--- a/scenes/World.gd
+++ b/scenes/World.gd
@@ -215,7 +215,6 @@ func _on_Player_destination_set() -> void:
 	$Destination.global_position = player.destination
 	pass
 
-
 func _on_UpdateTimer_timeout() -> void:
 	var enemies = get_tree().get_nodes_in_group("enemies")
 	var filteredEnemies = Utils.filter(funcref(self,"filter_invalid_enemies"), enemies)