Skip to content

Commit

Permalink
[wip] handle factory_save in the_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Jan 2, 2025
1 parent f50cf8b commit 6488126
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func _ready():
elif SaveHelper.load_saved_file_name() == OK:
the_storage.load_storage_save()
the_bank.load_bank_save()
the_factory.load_factory_save()
the_builder.load_buildings_save()
else:
# save cannot be loaded
Expand Down Expand Up @@ -249,6 +250,7 @@ func _on_PauseMenu_ask_to_save() -> void:
var dicoToSave := {}
dicoToSave.merge(the_storage.get_storage_save())
dicoToSave.merge(the_bank.get_bank_save())
dicoToSave.merge(the_factory.get_factory_save())
dicoToSave.merge(the_builder.get_buildings_save())

pause_menu.save_this_please(dicoToSave)
34 changes: 32 additions & 2 deletions theLudovyc/TheFactory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class_name TheFactory
@onready var event_bus = $"../EventBus"

enum Production_Line { current_workers, production_rate, current_ticks }
var production_lines = {}
var production_lines := {}

var workers := 0:
set(value):
workers = value
event_bus.available_workers_updated.emit(game.population - workers)

enum Waiting_Lines { resource_type, needed_workers }
var waiting_lines = []
var waiting_lines := []


func get_production_rate_per_tick(resource_type: Resources.Types) -> int:
Expand Down Expand Up @@ -158,3 +158,33 @@ func _on_TheTicker_tick():
line[Production_Line.current_ticks] = 0

storage.add_resource(resource_type, line[Production_Line.production_rate])

func get_factory_save() -> Dictionary:
return {"Factory":[workers, production_lines, waiting_lines]}

func load_factory_save() -> Error:
if SaveHelper.last_loaded_data.is_empty():
return FAILED

var factory_data:Array = SaveHelper.last_loaded_data.get("Factory", [])

if factory_data.is_empty():
return FAILED

workers = factory_data[0]

waiting_lines = factory_data[2]

for line:String in factory_data[1]:
var resource_type:int = line.to_int()

if not Resources.Types.values().has(resource_type):
push_error("Cannot create a production line from an unknow resource type: " + line)

continue

production_lines[resource_type] = factory_data[1][line]

storage.update_global_production_rate(resource_type)

return OK

0 comments on commit 6488126

Please sign in to comment.