From 6ffa6e643b5e574db76ad6bea2b9ead513d02a67 Mon Sep 17 00:00:00 2001 From: gotfishmakesticks <80163046+gotfishmakesticks@users.noreply.github.com> Date: Sat, 11 Nov 2023 01:08:43 +0300 Subject: [PATCH] New profiles system + auto version label --- scenes/MainMenu.tscn | 17 +++++++++ scenes/MainShip.tscn | 4 +- scripts/game.gd | 16 ++++---- scripts/menu/MainMenu.gd | 34 ++++++++++++----- scripts/menu/MainMenuButton.gd | 68 +++++++++++++++++++--------------- scripts/misc/VersionLabel.gd | 6 +++ scripts/profile.gd | 32 ++++++++++++++++ 7 files changed, 128 insertions(+), 49 deletions(-) create mode 100644 scripts/misc/VersionLabel.gd create mode 100644 scripts/profile.gd diff --git a/scenes/MainMenu.tscn b/scenes/MainMenu.tscn index 168abb2..a56ca7b 100644 --- a/scenes/MainMenu.tscn +++ b/scenes/MainMenu.tscn @@ -38,6 +38,17 @@ anchors_preset = 0 offset_right = 1280.0 offset_bottom = 720.0 +[node name="Input" type="LineEdit" parent="Control"] +visible = false +layout_mode = 0 +offset_left = 9.0 +offset_top = 209.0 +offset_right = 382.0 +offset_bottom = 261.0 +placeholder_text = "Enter your profile name here!" +alignment = 1 +max_length = 128 + [node name="MenuButton1" type="Button" parent="Control"] layout_mode = 0 offset_left = 9.0 @@ -77,3 +88,9 @@ offset_top = 488.0 offset_right = 381.0 offset_bottom = 541.0 script = ExtResource("5_q6x10") + +[node name="ProfileStatus" type="Label" parent="Control"] +layout_mode = 0 +offset_right = 199.0 +offset_bottom = 26.0 +text = "Current profile: {PROFILE}" diff --git a/scenes/MainShip.tscn b/scenes/MainShip.tscn index 25e2762..5cb37fa 100644 --- a/scenes/MainShip.tscn +++ b/scenes/MainShip.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://ccrs28h3b2tfy"] +[gd_scene load_steps=13 format=3 uid="uid://ccrs28h3b2tfy"] [ext_resource type="Script" path="res://scripts/objects/MainShip.gd" id="1_h7kne"] [ext_resource type="PackedScene" uid="uid://bbho4h6tg4jca" path="res://scenes/hulls/starterhull.tscn" id="2_r634y"] @@ -8,6 +8,7 @@ [ext_resource type="PackedScene" uid="uid://66m5gj2ufsop" path="res://scenes/shields/startershield.tscn" id="6_nihas"] [ext_resource type="Script" path="res://scripts/misc/CameraTweaks.gd" id="7_5jx81"] [ext_resource type="Script" path="res://scripts/misc/Counter.gd" id="8_udmcu"] +[ext_resource type="Script" path="res://scripts/misc/VersionLabel.gd" id="10_eil8s"] [ext_resource type="Script" path="res://scripts/misc/PlayerPauseController.gd" id="13_8y0ow"] [ext_resource type="PackedScene" uid="uid://dsmwg1rxedi3x" path="res://scenes/Minimap.tscn" id="14_o544g"] @@ -192,6 +193,7 @@ grow_horizontal = 0 text = "GammaCosmicRays version Ictar 1.1 unbuilt This is a debug version for internal usage." horizontal_alignment = 2 +script = ExtResource("10_eil8s") [node name="CanvasLayer" type="CanvasLayer" parent="."] layer = 2 diff --git a/scripts/game.gd b/scripts/game.gd index 71e84a1..149b8eb 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -10,11 +10,12 @@ const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn const gameversion = "Ictar 1.1" -## Creates new profile or resaves it if it exists +static var profile : Profile = Profile.new() + +## Creates a new profile if it doesn't exist static func profile_create(profile_name : String) -> void: var path = "user://"+profile_name+".cosmic" if FileAccess.file_exists(path): - profile_save(profile_name, "menu") return var profile_meta = { 'hash' : {}, @@ -51,8 +52,8 @@ static func profile_save_from_meta(profile_meta : Dictionary) -> void: var profile_meta_keys = profile_meta['meta'].keys() for i in range(len(profile_meta_keys)): profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode() - var file = FileAccess.open(path, FileAccess.WRITE) var json_string = JSON.stringify(profile_meta, "\t") + var file = FileAccess.open(path, FileAccess.WRITE) file.store_string(json_string) ## Returns profile meta of profile if it exists @@ -63,8 +64,6 @@ static func profile_load(profile_name : String) -> Dictionary: var file = FileAccess.open(path, FileAccess.READ) var string = file.get_as_text() var profile_meta = JSON.parse_string(string) - for meta in profile_meta: - print(meta, ": ", profile_meta[meta]) return profile_meta ## Deletes profile file if it exists @@ -74,8 +73,8 @@ static func profile_delete(profile_name : String) -> void: var dir = DirAccess.open("user://") dir.remove(path) -## Compares existing hash and calculated hash of file and if they are not equal resaves file with legit = false. -static func profile_legit_check(profile_meta : Dictionary) -> void: +## Compares existing hash and calculated hash of file and if they are not equal resaves file with legit = false. Either way it returns profile meta. +static func profile_legit_check(profile_meta : Dictionary) -> Dictionary: var old_hash = [] var new_hash = [] var profile_meta_keys = profile_meta['meta'].keys() @@ -85,9 +84,10 @@ static func profile_legit_check(profile_meta : Dictionary) -> void: old_hash.sort() new_hash.sort() if old_hash == new_hash and profile_meta['meta']['legit']: - return + return profile_meta profile_meta['meta']['legit'] = false profile_save_from_meta(profile_meta) + return profile_meta ## Returns copy of loaded item resource in directory if it exists or DEFAULT_ITEM instead. static func get_item(id : String) -> Item: diff --git a/scripts/menu/MainMenu.gd b/scripts/menu/MainMenu.gd index 2e7d359..1c285c1 100644 --- a/scripts/menu/MainMenu.gd +++ b/scripts/menu/MainMenu.gd @@ -8,30 +8,44 @@ var menu_id = 0 @onready var b3 = $Control/MenuButton3 @onready var b4 = $Control/MenuButton4 @onready var b5 = $Control/MenuButton5 +@onready var profile_status = $Control/ProfileStatus +@onready var input = $Control/Input func _ready(): change_menu(0) + profile_status.text = "Current profile: [{profile}]".format({"profile": Game.profile.profile_meta['meta']['profile_name']}) func change_menu(id): + input.visible = false match id: - 0: + 0: # Main b1.id = "NewGame" b2.id = "Profiles" b3.id = "Settings" b4.id = "Credits" b5.id = "ExitGame" - 1: + 1: # Profiles b1.id = "CreateProfile" b2.id = "LoadProfile" - b3.id = "DeleteProfile" - b4.id = "Back" + b3.id = "Back" + b4.id = "Null" b5.id = "Null" - 2: - b1.id = "" - b2.id = "" - b3.id = "" - b4.id = "" - b5.id = "" + 2: # Create Profile + b1.id = "Null" + b2.id = "CreateProfileConfirm" + b3.id = "Back" + b4.id = "Null" + b5.id = "Null" + input.visible = true + input.text = "" + 3: # Load Profile + b1.id = "Null" + b2.id = "LoadProfileConfirm" + b3.id = "Back" + b4.id = "Null" + b5.id = "Null" + input.visible = true + input.text = "" b1.change_name() b2.change_name() b3.change_name() diff --git a/scripts/menu/MainMenuButton.gd b/scripts/menu/MainMenuButton.gd index ab7db96..532ba7b 100644 --- a/scripts/menu/MainMenuButton.gd +++ b/scripts/menu/MainMenuButton.gd @@ -1,9 +1,25 @@ extends Button +var texts = { + "NewGame" : "New Game", + "CreateProfile" : "Create Profile", + "CreateProfileConfirm" : "Confirm", + "LoadProfile" : "Load Profile", + "LoadProfileConfirm" : "Confirm", + "DeleteProfile" : "Delete Profile", + "ExitGame" : "Exit Game", + "Settings" : "Settings", + "AudioSettings" : "Audio", + "VideoSettings" : "Video", + "Profiles" : "Profiles", + "Credits" : "Credits", + "Back" : "Back", + "Null" : Game.gameversion +} + @export var id = "Null" @onready var controller = $"../.." - func _ready(): button_up.connect(_on_button_up) change_name() @@ -12,10 +28,16 @@ func _on_button_up(): match id: "NewGame": get_tree().change_scene_to_file("res://scenes/Space.tscn") - "CreateProfile": - Game.profile_create("1") - "LoadProfile": - Game.profile_legit_check(Game.profile_load("1")) + "CreateProfileConfirm": + var profile_name = controller.input.text + Game.profile_create(profile_name) + Game.profile_load(profile_name) + "LoadProfileConfirm": + var profile_name = controller.input.text + var profile_meta = Game.profile_legit_check(Game.profile_load(profile_name)) + Game.profile = Profile.create(profile_meta) + var format = {"profile": Game.profile.profile_meta['meta']['profile_name']} + controller.profile_status.text = "Current profile: [{profile}]".format(format) "DeleteProfile": Game.profile_delete("1") "ExitGame": @@ -24,31 +46,17 @@ func _on_button_up(): controller.change_menu(1) "Back": var new_menu_id = 0 + match controller.menu_id: + 2: + new_menu_id = 1 + 3: + new_menu_id = 1 controller.change_menu(new_menu_id) + "CreateProfile": + controller.change_menu(2) + "LoadProfile": + controller.change_menu(3) func change_name(): - visible = true - match id: - "NewGame": - text = "New Game" - "CreateProfile": - text = "Create Profile" - "LoadProfile": - text = "Load Profile" - "DeleteProfile": - text = "Delete Profile" - "ExitGame": - text = "Exit Game" - "Settings": - text = "Settings (WIP)" - "AudioSettings": - text = "Audio (WIP)" - "VideoSettings": - text = "Video (WIP)" - "Profiles": - text = "Profiles (WIP)" - "Back": - text = "Back" - "Null": - text = Game.gameversion - visible = false + visible = id != "Null" + text = texts[id] diff --git a/scripts/misc/VersionLabel.gd b/scripts/misc/VersionLabel.gd new file mode 100644 index 0000000..0e5776e --- /dev/null +++ b/scripts/misc/VersionLabel.gd @@ -0,0 +1,6 @@ +extends Label + +func _ready(): + text = "GammaCosmicRays Version {version}" + if OS.has_feature("editor"): + text += " uncompiled\nThis is a debug/prerelease version." diff --git a/scripts/profile.gd b/scripts/profile.gd new file mode 100644 index 0000000..d03c2f7 --- /dev/null +++ b/scripts/profile.gd @@ -0,0 +1,32 @@ +class_name Profile + +var profile_meta = { + 'hash' : {}, + 'meta' : { + 'created_in_version' : Game.gameversion, + 'creation_date' : Time.get_datetime_string_from_system(), + 'last_version' : Game.gameversion, + 'last_updated' : Time.get_datetime_string_from_system(), + 'profile_name' : "", + 'legit' : true + } + } + +var path : set = _set_path + +var is_root : set = _check_root + +func _check_root(file_path): + return file_path == "user://.cosmic" + +func _set_path(name): + return "user://{name}.cosmic".format({"name" : name}) + +static func create(meta): + var profile = Profile.new() + profile.profile_meta = meta + profile.path = profile.profile_meta['meta']['profile_name'] + profile.is_root = profile.path + if profile.is_root: + profile.profile_meta['profiles'] = {} + return profile