forked from unknown-horizons/godot-port
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] handle buildings_save in the_builder
- Loading branch information
theludovyc
committed
Jan 2, 2025
1 parent
6d7c28b
commit 2856883
Showing
3 changed files
with
60 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,62 @@ | ||
extends Node | ||
|
||
const Buildings_Scenes = { | ||
Buildings.Ids.Warehouse: preload("res://theLudovyc/Building/Warehouse.tscn"), | ||
Buildings.Ids.Tent: preload("res://theLudovyc/Building/Residential.tscn"), | ||
Buildings.Ids.Lumberjack: preload("res://theLudovyc/Building/Lumberjack.tscn") | ||
} | ||
|
||
var warehouse: Building2D | ||
|
||
@onready var node_buildings:Node = %Buildings | ||
|
||
@onready var tilemap:TileMap = %TileMap | ||
|
||
func instantiate_building(building_id: Buildings.Ids) -> Building2D: | ||
var instance = Buildings_Scenes[building_id].instantiate() as Building2D | ||
|
||
node_buildings.add_child(instance) | ||
|
||
return instance | ||
|
||
func build(building_id:Buildings.Ids, pos:Vector2) -> Building2D: | ||
if not Buildings.Ids.has(building_id): | ||
push_error("Cannot create building with this Id: " + str(building_id)) | ||
|
||
return null | ||
|
||
var building = instantiate_building(building_id) | ||
building.position = pos | ||
|
||
tilemap.conclude_building_construction(building) | ||
|
||
building.build() | ||
return building | ||
|
||
func build_warehouse(pos:Vector2): | ||
warehouse = build(Buildings.Ids.Warehouse, pos) | ||
|
||
func get_buildings_save() -> Dictionary: | ||
var datas:Array | ||
|
||
for child:Building2D in node_buildings.get_children(): | ||
datas.append([child.building_id, child.position.x, child.position.y]) | ||
|
||
return {"Buildings":datas} | ||
|
||
func load_buildings_save() -> Error: | ||
if SaveHelper.last_loaded_data.is_empty(): | ||
return FAILED | ||
|
||
var buildings_data:Array = SaveHelper.last_loaded_data.get("Buildings", []) | ||
|
||
if buildings_data.is_empty(): | ||
return FAILED | ||
|
||
for building_data in SaveHelper.last_loaded_data["Buildings"]: | ||
if building_data[0] == 0: | ||
build_warehouse(Vector2(building_data[1], building_data[2])) | ||
else: | ||
build(building_data[0], Vector2(building_data[1], building_data[2])) | ||
|
||
return OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters