Refactor save/load functionality; improve error messages and add save data timer
This commit is contained in:
@@ -5,25 +5,14 @@ extends Node
|
||||
const SETTING_SECTION := "Settings"
|
||||
const SAVE_PATH = "user://settings.cfg"
|
||||
|
||||
|
||||
func ch():
|
||||
pass
|
||||
#print(Global.Owniverse)
|
||||
#print('h')
|
||||
#save_data('ow1', 'test', Global.Owniverse)
|
||||
#var d1: Dictionary = load_data('ow1', 'test1')
|
||||
#print("d1: ", d1)
|
||||
#var d2: Dictionary = load_data('ow1', 'test')
|
||||
#print("d2: ", d2)
|
||||
#print('d')
|
||||
#print(Global.Owniverse.datetime.WEEKDAYS)
|
||||
#region Save Load routines
|
||||
|
||||
|
||||
func save_data(filename: String, password: String, data: Dictionary) -> void:
|
||||
filename = "user://" + filename + ".adventure"
|
||||
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.WRITE, password)
|
||||
if file == null:
|
||||
push_error("Не удалось открыть файл для шифрованной записи")
|
||||
push_error("Can't write to the file: ", filename)
|
||||
return
|
||||
|
||||
file.store_var(data) # бинарно + быстрее + сериализация
|
||||
@@ -37,17 +26,15 @@ func load_data(filename: String, password: String) -> Dictionary:
|
||||
|
||||
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.READ, password)
|
||||
if file == null:
|
||||
push_error("Неверный пароль или повреждённый файл")
|
||||
push_error(filename, " couldn't be decrypted.")
|
||||
return {}
|
||||
|
||||
var data:Variant = file.get_var()
|
||||
var data: Variant = file.get_var()
|
||||
file.close()
|
||||
|
||||
#print("data")
|
||||
#print(data)
|
||||
|
||||
return data if data is Dictionary else {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Settings
|
||||
|
||||
+7
-1
@@ -93,7 +93,8 @@ func add_to_update_list(item_id: String, amount: int) -> void:
|
||||
|
||||
func set_owniverse_item_value() -> void:
|
||||
for item_id in owniverse.items_to_update:
|
||||
owniverse_items[item_id].set_label(owniverse.items_to_update[item_id])
|
||||
if owniverse_items.has(item_id):
|
||||
owniverse_items[item_id].set_label(owniverse.items_to_update[item_id])
|
||||
owniverse.items_to_update = {}
|
||||
|
||||
|
||||
@@ -235,6 +236,7 @@ func _input(event: InputEvent) -> void:
|
||||
|
||||
func _ready() -> void:
|
||||
owniverse.init_enitities()
|
||||
owniverse.load_data()
|
||||
_init_owniverse_items()
|
||||
|
||||
owniverse.unlock_item.connect(_unlock_item)
|
||||
@@ -298,3 +300,7 @@ func _change_speed(delta: int) -> void:
|
||||
|
||||
func _on_topmenu_button_pressed(ButtonName) -> void:
|
||||
print(ButtonName)
|
||||
|
||||
|
||||
func _on_save_data() -> void:
|
||||
owniverse.save_data()
|
||||
|
||||
@@ -20,8 +20,8 @@ var item_type = ""
|
||||
|
||||
|
||||
func set_label(value: float) -> void:
|
||||
#description.text = Global.format_number(value)
|
||||
description.text = str(value)
|
||||
description.text = Global.format_number(value)
|
||||
#description.text = str(value)
|
||||
|
||||
|
||||
func set_selection(flag: bool) -> void:
|
||||
|
||||
+65
-2
@@ -8,6 +8,8 @@ var current_speed: int = 1
|
||||
var current_speed_in_years:int = 1
|
||||
|
||||
var entities: Dictionary[String, OwniverseEntity] = {}
|
||||
var entity_groups: Dictionary[String, Array] = {}
|
||||
var entity_event_groups: Dictionary[String, Array] = {}
|
||||
|
||||
var items_to_update: Dictionary[String, float] = {}
|
||||
|
||||
@@ -28,6 +30,8 @@ func cycle(year_delta: float) -> void:
|
||||
_entity_evolution(year_delta)
|
||||
|
||||
_check_locked_items()
|
||||
|
||||
#print(_get_distribution(current_amount))
|
||||
|
||||
|
||||
#region auto production and evolution
|
||||
@@ -46,7 +50,18 @@ func _auto_production(year_delta: float) -> void:
|
||||
|
||||
func _entity_evolution(year_delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _get_distribution(source: Dictionary) -> Dictionary:
|
||||
var distribution: Dictionary = {}
|
||||
var total: float = 0
|
||||
for id in source:
|
||||
if source[id] != 0:
|
||||
total += source[id]
|
||||
distribution[id] = source[id]
|
||||
for id in distribution:
|
||||
distribution[id] /= total
|
||||
return distribution
|
||||
|
||||
#endregion
|
||||
|
||||
#region Production
|
||||
@@ -80,7 +95,6 @@ func get_available_to_produce(item_id: String) -> float:
|
||||
var amount: float = 1E50
|
||||
for elem in entity.cost:
|
||||
amount = min(floorf(current_amount[elem] / entity.cost[elem]), amount)
|
||||
#print(elem, amount)
|
||||
return amount
|
||||
|
||||
# add to all lists
|
||||
@@ -132,6 +146,38 @@ func change_speed(delta: int) -> int:
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Init save load routines
|
||||
|
||||
|
||||
func load_data() -> bool:
|
||||
var is_succesfull = false
|
||||
|
||||
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
|
||||
if game_data == {}:
|
||||
return false
|
||||
|
||||
if !game_data.has_all(["cumulative_amount", "current_amount", "entity_batches"]):
|
||||
return false
|
||||
|
||||
entity_batches = game_data["entity_batches"]
|
||||
cumulative_amount = game_data["cumulative_amount"]
|
||||
current_amount = game_data["current_amount"]
|
||||
|
||||
for item in current_amount:
|
||||
items_to_update[item] = current_amount[item]
|
||||
|
||||
return true
|
||||
|
||||
|
||||
func save_data() -> void:
|
||||
var game_data: Dictionary = {}
|
||||
game_data["cumulative_amount"] = cumulative_amount
|
||||
game_data["current_amount"] = current_amount
|
||||
game_data["entity_batches"] = entity_batches
|
||||
SaveManager.save_data("owniverse", "qwerty", game_data)
|
||||
|
||||
|
||||
func init_enitities() -> void:
|
||||
for entity_id in Global.OwniverseEntities:
|
||||
var entity = OwniverseEntity.new()
|
||||
@@ -149,3 +195,20 @@ func init_enitities() -> void:
|
||||
|
||||
if entity.product_per_year_type != "":
|
||||
auto_production.append(entity)
|
||||
|
||||
# groups
|
||||
if entity.group_as != "":
|
||||
if !entity_groups.has(entity.group_as):
|
||||
entity_groups[entity.group_as] = []
|
||||
entity_groups[entity.group_as].append(entity_id)
|
||||
|
||||
if entity.event_group != "":
|
||||
if !entity_event_groups.has(entity.event_group):
|
||||
entity_event_groups[entity.event_group] = []
|
||||
entity_event_groups[entity.event_group].append(entity_id)
|
||||
|
||||
#if entity.event_group != "":
|
||||
#entity_event_groups.append(entity.id)
|
||||
print(entity_groups)
|
||||
print(entity_event_groups)
|
||||
#endregion
|
||||
|
||||
+4
-10
@@ -45,14 +45,6 @@
|
||||
|
||||
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ifjjt"]
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_behgl"]
|
||||
script/source = "extends Node
|
||||
|
||||
func print_smth(smth: String):
|
||||
pass
|
||||
print(smth)
|
||||
"
|
||||
|
||||
[node name="World" type="Node2D" unique_id=1392592456]
|
||||
|
||||
[node name="Background" type="Node2D" parent="." unique_id=1988758765]
|
||||
@@ -880,8 +872,9 @@ layout_mode = 2
|
||||
wait_time = 0.2
|
||||
autostart = true
|
||||
|
||||
[node name="CommandProcessor" type="Node" parent="." unique_id=2069361293]
|
||||
script = SubResource("GDScript_behgl")
|
||||
[node name="SaveData" type="Timer" parent="Stage" unique_id=1208861837]
|
||||
wait_time = 5.0
|
||||
autostart = true
|
||||
|
||||
[connection signal="timeout" from="Background/Timer" to="Background" method="randomize_parallax"]
|
||||
[connection signal="pressed" from="Stage/StageRows/TopMenu/BurgerButton" to="Stage" method="_on_topmenu_button_pressed" binds= ["Burger"]]
|
||||
@@ -897,3 +890,4 @@ script = SubResource("GDScript_behgl")
|
||||
[connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/AggressionIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Aggression", 4]]
|
||||
[connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/TrendIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Trends", 4]]
|
||||
[connection signal="timeout" from="Stage/ItemAmountUpdate" to="Stage" method="set_owniverse_item_value"]
|
||||
[connection signal="timeout" from="Stage/SaveData" to="Stage" method="_on_save_data"]
|
||||
|
||||
Reference in New Issue
Block a user