Skip to content

Commit

Permalink
[wip] handle buildings_save in the_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Jan 2, 2025
1 parent 6d7c28b commit 2856883
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
51 changes: 51 additions & 0 deletions Script/TheBuilder.gd
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
44 changes: 6 additions & 38 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class_name Game2D
@onready var gui := $GUI
@onready var pause_menu := %PauseMenu

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")
}

const Trees_Destroy_Cost = 1

# if not null follow the cursor
Expand All @@ -38,8 +32,6 @@ var population := 0:
event_bus.population_updated.emit(value)
event_bus.available_workers_updated.emit(population - the_factory.workers)

var warehouse: Building2D

var current_selected_building: Building2D = null


Expand All @@ -56,7 +48,7 @@ func _ready():
var warehouse_pos = Vector2.ZERO

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

# add some initial resources
the_bank.money = 100
Expand All @@ -65,31 +57,16 @@ func _ready():
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_storage.load_storage_save()
the_bank.load_bank_save()
the_builder.load_buildings_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(warehouse_pos)

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

tm.build_entityStatic(warehouse, warehouse_center_tile)

warehouse.build()

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

pass # Replace with function body.
Expand Down Expand Up @@ -219,17 +196,8 @@ func _process(delta):
cursor_entity = null


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 _on_EventBus_ask_create_building(building_id: Buildings.Ids):
var entity := instantiate_building(building_id)
cursor_entity = entity
cursor_entity = the_builder.instantiate_building(building_id)
cursor_entity_wait_release = true
cursor_entity.modulate = Color(Color.RED, 0.6)

Expand All @@ -245,9 +213,9 @@ func _on_EventBus_ask_deselect_building():


func _on_EventBus_ask_select_warehouse():
current_selected_building = warehouse
current_selected_building = the_builder.warehouse

warehouse.select()
the_builder.warehouse.select()


func _on_EventBus_ask_demolish_current_building():
Expand Down
7 changes: 3 additions & 4 deletions theLudovyc/TileMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func build_entityStatic(entity:EntityStatic, tile_center:Vector2i):
trees_layer.erase_cell(tile_coord)

minimap_set_cell_vec(tile_coord, Minimap_Cell_Type.Building)

func conclude_building_construction(building:Building2D):
build_entityStatic(building, ground_layer.local_to_map(building.position))

func demolish_building(building:Building2D):
var top_left_tile = entityStatic_get_top_left_tile(building,
Expand Down Expand Up @@ -138,7 +141,3 @@ func get_pos_limits() -> PackedVector2Array:

func local_to_map_to_local(position:Vector2) -> Vector2:
return map_to_local(local_to_map(position))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass

0 comments on commit 2856883

Please sign in to comment.