fuzzyworld

godot-shooter / commit

Added game settings for controlling some preferences, like showing the intro dialog.

2464bc929e14ad4f361daa01f3326e29232d3b88 · Pavel Safronov · 2023-11-14 10:49:28 -0800 · parents 180b363c579b

Added game settings for controlling some preferences, like showing the intro dialog.
 Settings.gd   | 17 +++++++++++++++++
 Utils.gd      | 21 +++++++++++++++++++++
 World.gd      | 27 ++++++++++++++++++++++++---
 World.tscn    | 52 +++++++++++++++++++++++++++++++++++++---------------
 project.godot |  6 ++++++
 5 files changed, 105 insertions(+), 18 deletions(-)

diff --git a/Settings.gd b/Settings.gd
new file mode 100644
index 0000000..71e0da0
--- /dev/null
+++ b/Settings.gd
@@ -0,0 +1,17 @@
+class_name Settings
+
+var show_intro = true
+
+const settings_path = "user://settings.json"
+
+func to_json() -> Dictionary:
+	return {
+		"show_intro": show_intro
+	}
+
+func save_user_settings(path = settings_path) -> void:
+	var save_game: File = File.new()
+	save_game.open(path, File.WRITE)
+	var json_data = JSON.print(self.to_json())
+	save_game.store_line(json_data)
+	save_game.close()
diff --git a/Utils.gd b/Utils.gd
index 9ffae69..8458c2a 100644
--- a/Utils.gd
+++ b/Utils.gd
@@ -1,6 +1,27 @@
 # Note: `tool` is optional but without it there are no error reporting in the editor
 tool
 
+const SettingsClass = preload("res://Settings.gd")
+
+static func remove_user_settings(path = SettingsClass.settings_path) -> void:
+	var dir = Directory.new()
+	dir.remove(path)
+
+static func load_user_settings(path = SettingsClass.settings_path) -> Settings:
+	var save_game: File = File.new()
+	var settings_exists = save_game.file_exists(path)
+	if settings_exists:
+		save_game.open(path, File.READ)
+		var current_line = save_game.get_line()
+		save_game.close()
+		var json = parse_json(current_line)
+		var data: Settings = Settings.new()
+		data.show_intro = json["show_intro"]
+		return data
+	
+	var data: Settings = Settings.new()
+	return data
+
 static func log(arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null, arg8 = null, arg9 = null):
 	var text = ""
 	var now = Time.get_datetime_string_from_system(false, true)
diff --git a/World.gd b/World.gd
index b4c97f2..80275f8 100644
--- a/World.gd
+++ b/World.gd
@@ -4,6 +4,7 @@ const Utils = preload("res://Utils.gd")
 const Weapons = preload("res://Weapons.gd")
 const Enemy = preload("res://Enemy.tscn")
 const PowerUp = preload("res://PowerUp.tscn")
+const Settings = preload("res://Settings.gd")
 var width = ProjectSettings.get("display/window/size/width")
 var height = ProjectSettings.get("display/window/size/height")
 
@@ -22,6 +23,7 @@ var completed_objectives = 0
 var max_objectives = 5
 var item_replacement_distance = 2000
 var is_dragging = false
+var settings: Settings
 
 func enemy_killed(_position: Vector2):
 	$UI/HUD.score += 5
@@ -88,8 +90,12 @@ func create_shield() -> PowerUp:
 	return shield
 
 func _ready() -> void:
-	get_tree().paused = true
-	$UI/IntroMenu.visible = true
+	settings = Utils.load_user_settings()
+	var pause_menu_show_intro_check = $UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Settings/CheckBox
+	pause_menu_show_intro_check.pressed = settings.show_intro
+	if settings.show_intro:
+		get_tree().paused = true
+		$UI/IntroMenu.visible = true
 
 func get_new_item_pos(dist_ratio: float = 1.0) -> Vector2:
 	var pos_angle = randi() % 360
@@ -234,7 +240,9 @@ func restart() -> void:
 	var _err = get_tree().reload_current_scene()
 
 func _on_HUD_debug_signal() -> void:
-#	completed_objectives = 5
+	var p = $Player.global_position
+	
+	set_destination(Vector2(p.x, p.y - 1000))
 	pass
 
 func _on_LinkButton_pressed() -> void:
@@ -246,4 +254,17 @@ func _on_LinkButton_pressed() -> void:
 func _on_StartButton_pressed() -> void:
 	get_tree().paused = false
 	$UI/IntroMenu.visible = false
+	settings.show_intro = $UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer/CheckBox.pressed
+	settings.save_user_settings()
+	var pause_menu_show_intro_check = $UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Settings/CheckBox
+	pause_menu_show_intro_check.pressed = settings.show_intro
 	get_tree().set_input_as_handled()
+
+func _on_CheckBox_pressed() -> void:
+	var show_intro_check = $UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer/CheckBox
+	var show_intro = show_intro_check.pressed
+	set_intro_dialog(show_intro)
+
+func set_intro_dialog(enabled: bool) -> void:
+	settings.show_intro = enabled
+	settings.save_user_settings()
diff --git a/World.tscn b/World.tscn
index 8b621c7..bc211d9 100644
--- a/World.tscn
+++ b/World.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=13 format=2]
+[gd_scene load_steps=14 format=2]
 
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/icon_crossLarge.png" type="Texture" id=1]
 [ext_resource path="res://Player.tscn" type="PackedScene" id=2]
@@ -25,6 +25,11 @@ font_data = ExtResource( 8 )
 [sub_resource type="Theme" id=4]
 default_font = SubResource( 3 )
 
+[sub_resource type="DynamicFont" id=5]
+use_mipmaps = true
+use_filter = true
+font_data = ExtResource( 8 )
+
 [node name="World" type="Node2D"]
 script = ExtResource( 4 )
 
@@ -113,7 +118,21 @@ margin_top = 45.0
 margin_right = 372.0
 margin_bottom = 168.0
 
+[node name="Settings" 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="CheckBox" type="CheckBox" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Settings"]
+margin_right = 48.0
+margin_bottom = 48.0
+text = "Show intro dialog"
+
 [node name="About" type="VBoxContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
+visible = false
 anchor_right = 1.0
 anchor_bottom = 1.0
 margin_left = 8.0
@@ -128,16 +147,10 @@ rect_min_size = Vector2( 200, 0 )
 text = "Welcome to my Godot experiment."
 fit_content_height = true
 
-[node name="Src" type="VBoxContainer" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer"]
-visible = false
-margin_right = 40.0
-margin_bottom = 40.0
-
-[node name="LinkButton" type="LinkButton" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Src"]
-margin_left = 8.0
-margin_top = 150.0
-margin_right = 208.0
-margin_bottom = 175.0
+[node name="LinkButton" type="LinkButton" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/About"]
+margin_top = 89.0
+margin_right = 200.0
+margin_bottom = 114.0
 text = "Source code"
 
 [node name="Button" type="Button" parent="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer"]
@@ -205,7 +218,7 @@ margin_bottom = 414.0
 margin_left = 14.0
 margin_top = 14.0
 margin_right = 447.0
-margin_bottom = 395.0
+margin_bottom = 451.0
 theme = ExtResource( 7 )
 alignment = 1
 
@@ -233,10 +246,18 @@ Your objective count is in the lower-right corner.
 The radar shows enemies and objectives."
 fit_content_height = true
 
-[node name="StartButton" type="Button" parent="UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer"]
+[node name="CheckBox" type="CheckBox" parent="UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer"]
 margin_top = 332.0
 margin_right = 433.0
-margin_bottom = 381.0
+margin_bottom = 380.0
+custom_fonts/font = SubResource( 5 )
+pressed = true
+text = "Always show intro"
+
+[node name="StartButton" type="Button" parent="UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer"]
+margin_top = 388.0
+margin_right = 433.0
+margin_bottom = 437.0
 mouse_default_cursor_shape = 2
 text = "Start Game"
 
@@ -261,7 +282,8 @@ antialiased = true
 [connection signal="debug_signal" from="UI/HUD" to="." method="_on_HUD_debug_signal"]
 [connection signal="paused" from="UI/HUD" to="." method="_on_HUD_paused"]
 [connection signal="position_set" from="UI/HUD" to="." method="set_destination"]
-[connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Src/LinkButton" to="." method="_on_LinkButton_pressed"]
+[connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/Settings/CheckBox" to="." method="_on_CheckBox_pressed"]
+[connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/TabContainer/About/LinkButton" to="." method="_on_LinkButton_pressed"]
 [connection signal="pressed" from="UI/PauseMenu/CenterContainer/PanelContainer/VBoxContainer/Button" to="." method="_on_Button_pressed"]
 [connection signal="pressed" from="UI/IntroMenu/CenterContainer/PanelContainer/VBoxContainer/StartButton" to="." method="_on_StartButton_pressed"]
 [connection signal="timeout" from="ShootTimer" to="." method="_on_ShootTimer_timeout"]
diff --git a/project.godot b/project.godot
index 037f0d4..e6478f0 100644
--- a/project.godot
+++ b/project.godot
@@ -44,6 +44,11 @@ _global_script_classes=[ {
 "language": "GDScript",
 "path": "res://Rocket.gd"
 }, {
+"base": "Reference",
+"class": "Settings",
+"language": "GDScript",
+"path": "res://Settings.gd"
+}, {
 "base": "Node2D",
 "class": "Trails",
 "language": "GDScript",
@@ -62,6 +67,7 @@ _global_script_class_icons={
 "Player": "",
 "PowerUp": "",
 "Rocket": "",
+"Settings": "",
 "Trails": "",
 "Weapon": ""
 }