2024-02-22 06:22:22 +11:00
|
|
|
class_name PlayerProfile extends Resource
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
signal display_name_changed(old_name: String, new_name: String)
|
|
|
|
signal preferred_class_changed(old_class: int, new_class: int)
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
const SAVE_PATH: String = "user://profile.tres"
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
@export var display_name: String = "Charlie"
|
|
|
|
@export var preferred_class: int = 0
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
func to_dict() -> Dictionary:
|
2024-02-22 06:22:22 +11:00
|
|
|
var dict: Dictionary = {}
|
2023-11-08 14:28:55 +11:00
|
|
|
dict["display_name"] = display_name
|
|
|
|
dict["preferred_class"] = preferred_class
|
|
|
|
return dict
|
2024-02-22 06:22:22 +11:00
|
|
|
static func from_dict(dict: Dictionary) -> PlayerProfile:
|
|
|
|
var output: PlayerProfile = PlayerProfile.new()
|
2023-11-08 14:28:55 +11:00
|
|
|
output.display_name = dict["display_name"]
|
|
|
|
output.preferred_class = dict["preferred_class"]
|
|
|
|
return output
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func set_display_name(new_display_name: String) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
if new_display_name == display_name:
|
|
|
|
return
|
2024-02-22 06:22:22 +11:00
|
|
|
var old_name: String = display_name
|
2023-11-08 14:28:55 +11:00
|
|
|
display_name = new_display_name
|
|
|
|
save_profile_to_disk()
|
|
|
|
display_name_changed.emit(old_name, display_name)
|
|
|
|
func get_display_name() -> String:
|
|
|
|
return display_name
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func set_preferred_class(new_preferred_class: int) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
if new_preferred_class == preferred_class:
|
|
|
|
return
|
2024-02-22 06:22:22 +11:00
|
|
|
var old_class: int = preferred_class
|
2023-11-08 14:28:55 +11:00
|
|
|
preferred_class = new_preferred_class
|
|
|
|
preferred_class_changed.emit(old_class, preferred_class)
|
|
|
|
func get_preferred_class() -> int:
|
|
|
|
return preferred_class
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func save_profile_to_disk() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
ResourceSaver.save(self, SAVE_PATH)
|
|
|
|
static func load_profile_from_disk() -> PlayerProfile:
|
|
|
|
if ResourceLoader.exists(SAVE_PATH):
|
|
|
|
return ResourceLoader.load(SAVE_PATH)
|
|
|
|
return PlayerProfile.new()
|