diff --git a/scenes/MainShip.tscn b/scenes/MainShip.tscn index 5cb37fa..33919f6 100644 --- a/scenes/MainShip.tscn +++ b/scenes/MainShip.tscn @@ -171,7 +171,7 @@ offset_left = 312.0 offset_top = 378.0 offset_right = 983.0 offset_bottom = 492.0 -text = "Quit to main menu" +text = "Save and quit to main menu" [node name="Label" type="Label" parent="GUI/Interface/PauseController"] layout_mode = 0 diff --git a/scripts/game.gd b/scripts/game.gd index 149b8eb..c0df877 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -8,50 +8,42 @@ enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS} const DEFAULT_ITEM = preload("res://items/test_item.tres") const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn") +const salt = "2ndbeam" const gameversion = "Ictar 1.1" 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): - return - var profile_meta = { - 'hash' : {}, - 'meta' : { - 'created_in_version' : gameversion, - 'creation_date' : Time.get_datetime_string_from_system(), - 'last_version' : gameversion, - 'last_updated' : Time.get_datetime_string_from_system(), - 'profile_name' : profile_name, - 'legit' : true - } - } - 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") - file.store_string(json_string) + profile = Profile.new() + profile.profile_meta['meta']['profile_name'] = profile_name + profile_save_from_meta(profile.profile_meta) -## Saves profile with new last_time and last_version keys or creates one if it doesn't exist -static func profile_save(profile_name : String, _gamestate : String) -> void: - var path = "user://"+profile_name+".cosmic" - if not FileAccess.file_exists(path): - profile_create(profile_name) - return - var profile_meta = profile_load(profile_name) - profile_save_from_meta(profile_meta) +## Saves profile meta and includes game data if there is an active session. +static func profile_save(scene : Node) -> void: + if scene.name != "MainMenu": + profile.profile_meta['game'] = { + "ammo" : scene.ship.hull.ammunition, + "hp" : scene.ship.hull.hp, + "fuel" : scene.ship.hull.fuel, + "money" : scene.ship.money + } + if scene.ship.primary_slot.get_child_count() > 0: + profile.profile_meta['game']['primaryweapon'] = scene.ship.primary_slot.get_child(0).id + if scene.ship.secondary_slot.get_child_count() > 0: + profile.profile_meta['game']['secondaryweapon'] = scene.ship.secondary_slot.get_child(0).id + profile_save_from_meta(profile.profile_meta) ## Saves profile with provided profile meta static func profile_save_from_meta(profile_meta : Dictionary) -> void: var path = "user://"+profile_meta['meta']['profile_name']+".cosmic" profile_meta['meta']['last_version'] = gameversion profile_meta['meta']['last_updated'] = Time.get_datetime_string_from_system() - 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() + for key in profile_meta['meta']: + profile_meta['hash'][key] = (str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode() + if 'game' in profile_meta: + for key in profile_meta['game']: + profile_meta['hash'][key] = (str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode() var json_string = JSON.stringify(profile_meta, "\t") var file = FileAccess.open(path, FileAccess.WRITE) file.store_string(json_string) @@ -77,10 +69,13 @@ static func profile_delete(profile_name : String) -> void: static func profile_legit_check(profile_meta : Dictionary) -> Dictionary: var old_hash = [] var new_hash = [] - var profile_meta_keys = profile_meta['meta'].keys() - for i in range(len(profile_meta_keys)): - old_hash.append(profile_meta['hash'][str(i)]) - new_hash.append(str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()) + for key in profile_meta['meta']: + old_hash.append(profile_meta['hash'][key]) + new_hash.append((str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode()) + if 'game' in profile_meta: + for key in profile_meta['game']: + old_hash.append(profile_meta['hash'][key]) + new_hash.append((str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode()) old_hash.sort() new_hash.sort() if old_hash == new_hash and profile_meta['meta']['legit']: @@ -101,8 +96,7 @@ static func get_item(id : String) -> Item: ## Returns weapon packed scene if it exists or DEFAULT_WEAPON instead static func get_weapon(id : String) -> PackedScene: - var path_name = id.replace(" ", "") - var path = "res://weapons/presets/{name}.tscn".format({"name": path_name}) + var path = "res://scenes/weapons/presets/{name}.tscn".format({"name": id}) var res = load(path) if res != null: return res diff --git a/scripts/menu/BuyMenuButton.gd b/scripts/menu/BuyMenuButton.gd index 2a79518..a78e6b9 100644 --- a/scripts/menu/BuyMenuButton.gd +++ b/scripts/menu/BuyMenuButton.gd @@ -49,19 +49,14 @@ func bought_action(): var primary_slot = ship.get_node("PrimaryWeapon") var secondary_slot = ship.get_node("SecondaryWeapon") var bought_weapon = get_tree().current_scene.bought_weapon - var weapon_dict = get_tree().current_scene.weapon_dict var slot = primary_slot if data["slot"] == "primary" else secondary_slot if !bought_weapon[data["weapon"]]: bought_weapon[data["weapon"]] = true else: ship.money += price - if slot.get_child_count() == 0: - var weapon_inst = load(weapon_dict[data["weapon"]]).instantiate() - slot.add_child(weapon_inst) - slot.position = data["position"] - else: - for node in slot.get_children(): - node.queue_free() - var weapon_inst = load(weapon_dict[data["weapon"]]).instantiate() - slot.add_child(weapon_inst) - slot.position = data["position"] + for node in slot.get_children(): + node.queue_free() + var weapon_inst = Game.get_weapon(data['weapon']).instantiate() + weapon_inst.id = data['weapon'] + weapon_inst.position = data["position"] + slot.add_child(weapon_inst) diff --git a/scripts/misc/PlayerPauseController.gd b/scripts/misc/PlayerPauseController.gd index 3da1f68..67f9209 100644 --- a/scripts/misc/PlayerPauseController.gd +++ b/scripts/misc/PlayerPauseController.gd @@ -8,4 +8,5 @@ func _on_unpause_button_button_up(): func _on_exit_button_button_up(): get_tree().current_scene.unpause() + Game.profile_save(get_tree().current_scene) get_tree().change_scene_to_file("res://scenes/MainMenu.tscn") diff --git a/scripts/misc/VersionLabel.gd b/scripts/misc/VersionLabel.gd index 0e5776e..83a0a6f 100644 --- a/scripts/misc/VersionLabel.gd +++ b/scripts/misc/VersionLabel.gd @@ -1,6 +1,6 @@ extends Label func _ready(): - text = "GammaCosmicRays Version {version}" + text = "GammaCosmicRays version {version}".format({"version" : Game.gameversion}) if OS.has_feature("editor"): text += " uncompiled\nThis is a debug/prerelease version." diff --git a/scripts/objects/MainShip.gd b/scripts/objects/MainShip.gd index 1779756..aaf419f 100644 --- a/scripts/objects/MainShip.gd +++ b/scripts/objects/MainShip.gd @@ -8,6 +8,8 @@ class_name MainShip @onready var pause_controller = $GUI/Interface/PauseController @onready var minimap = $CanvasLayer/Control/Minimap @onready var camera = $Camera +@onready var primary_slot = $PrimaryWeapon +@onready var secondary_slot = $SecondaryWeapon var allow_shooting = true var faction = "player" diff --git a/scripts/objects/weapon.gd b/scripts/objects/weapon.gd index 1907278..7865e4a 100644 --- a/scripts/objects/weapon.gd +++ b/scripts/objects/weapon.gd @@ -9,7 +9,7 @@ class_name Weapon @export var shoot_timer : Timer @export var shoot_action : String = "" @export var spawner_points : Array[Node2D] - +var id : String = "SingleLaserMk1" @onready var ship = $"../.." @onready var slot = $".."