Skip to content

Commit

Permalink
[wip] start handle save in Game2D
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Dec 24, 2024
1 parent f67bbba commit fa89d94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Script/TheBuilder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ func get_buildings_save() -> Dictionary:
var datas:Array

for child:Building2D in node_buildings.get_children():
datas.append([child.building_id, child.position])
datas.append([child.building_id, child.position.x, child.position.y])

return {"Buildings":datas}
45 changes: 30 additions & 15 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,51 @@ var current_selected_building: Building2D = null
# Called when the node enters the scene tree for the first time.
func _ready():
tm.create_island("res://theLudovyc/singularity_40.json")

# set camera limits
var pos_limits = tm.get_pos_limits()

cam.pos_limit_top_left = pos_limits[0]
cam.pos_limit_bot_right = pos_limits[1]

var warehouse_pos = Vector2.ZERO

if SaveHelper.save_file_name_to_load.is_empty():
warehouse_pos = Vector2(704, 320)

# add some initial resources
the_bank.money = 100

the_storage.add_resource(Resources.Types.Wood, 2)
the_storage.add_resource(Resources.Types.Textile, 16)

elif SaveHelper.load_saved_file_name() == OK:
for building_data in SaveHelper.last_loaded_data["Buildings"]:
if building_data[0] == 0:
warehouse_pos = Vector2(building_data[1], building_data[2])


the_bank.load_bank_save()
else:
# save cannot be loaded
# TODO show popup and return to main menu
return

# spawn the warehouse
warehouse = instantiate_building(Buildings.Ids.Warehouse)

var warehouse_center_tile = tm.ground_layer.local_to_map(Vector2(704, 320))
var warehouse_center_tile = tm.ground_layer.local_to_map(warehouse_pos)

warehouse.position = tm.ground_layer.map_to_local(warehouse_center_tile)

tm.build_entityStatic(warehouse, warehouse_center_tile)

warehouse.build()

prints(name, warehouse.position)

# set camera limits
var pos_limits = tm.get_pos_limits()

cam.pos_limit_top_left = pos_limits[0]
cam.pos_limit_bot_right = pos_limits[1]

# force camera initial pos on warehouse
cam.position = warehouse.global_position
cam.reset_smoothing()

# add some initial resources
the_bank.money = 100

the_storage.add_resource(Resources.Types.Wood, 2)
the_storage.add_resource(Resources.Types.Textile, 16)

pass # Replace with function body.


Expand Down
14 changes: 14 additions & 0 deletions theLudovyc/TheBank.gd
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ func recalculate_orders_cost():

func get_bank_save() -> Dictionary:
return {"Money": [money, money_production_rate]}

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

var bank_data:Array = SaveHelper.last_loaded_data.get("Money", [])

if bank_data.is_empty():
return FAILED

money = bank_data[0]
money_production_rate = bank_data[1]

return OK

0 comments on commit fa89d94

Please sign in to comment.