From d74bfda9a3cc297a10889f0288acfc37be387956 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 10 Mar 2024 07:50:26 -0500 Subject: [PATCH] Made building consume resources Now may build more than one if resources allow building placement is red/green for indicating success --- scripts/autoloads/grid.gd | 3 +++ scripts/autoloads/resource_manager.gd | 25 ++++++++++++++++++++++++- scripts/build_menu.gd | 4 +++- scripts/cell_data.gd | 5 ++++- scripts/gui.gd | 3 +++ scripts/interaction_bar.gd | 2 +- scripts/player.gd | 17 ++++++++++++++--- scripts/world.gd | 3 +-- 8 files changed, 53 insertions(+), 9 deletions(-) diff --git a/scripts/autoloads/grid.gd b/scripts/autoloads/grid.gd index 8d63c7a..da28e44 100644 --- a/scripts/autoloads/grid.gd +++ b/scripts/autoloads/grid.gd @@ -37,3 +37,6 @@ func get_location_data(pos: Vector2i) -> CellData: func change_location_resource(pos: Vector2i, data: GameResource) -> void: world_data[pos].change_resource(data) world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location) + +func change_location_building(pos: Vector2i, data: BuildingBase) -> void: + world_data[pos].change_building(data) diff --git a/scripts/autoloads/resource_manager.gd b/scripts/autoloads/resource_manager.gd index 8238c54..312a00c 100644 --- a/scripts/autoloads/resource_manager.gd +++ b/scripts/autoloads/resource_manager.gd @@ -1,4 +1,4 @@ - +extends Node signal changed_resource @export var resources := {} @@ -10,6 +10,29 @@ func _on_gained_resource(res: GameResource) -> void: } changed_resource.emit(ResourceChangedSignal.new(changed_resources)) +func pickup(resource: GameResource) -> void: + if not resources.has(resource): + resources[resource] = 0 + resources[resource] += resource.pickup_value + var changed_resources := { + resource: resource.pickup_value + } + changed_resource.emit(ResourceChangedSignal.new(changed_resources)) + +func has_amount(res: GameResource, amount: int) -> bool: + return resources[res] >= amount if resources.has(res) else false + +func has_resources(cost: Dictionary) -> bool: + return cost.keys().reduce(func(accum, res): return accum and has_amount(res, cost[res]), true) + +func use_resources(cost: Dictionary) -> void: + var changed_resources := { + } + for res in cost.keys(): + resources[res] -= cost[res] + changed_resources[res] = -cost[res] + changed_resource.emit(ResourceChangedSignal.new(changed_resources)) + #func _on_timer_timeout() -> void: #var corrupted_resources := Grid.get_corrupted_resources() #if corrupted_resources.size() > 0: diff --git a/scripts/build_menu.gd b/scripts/build_menu.gd index 5322cd4..7e3eb78 100644 --- a/scripts/build_menu.gd +++ b/scripts/build_menu.gd @@ -20,7 +20,7 @@ const BASIC_BUILDING_GROUP = preload("res://data/buildings/basic/basic_building_ func _ready() -> void: _add_building_group(BASIC_BUILDING_GROUP) _add_building_group(ADVANCED_BUILDING_GROUP) - building_groups.set_focus() + #building_groups.set_focus() func _add_building_group(group: BuildingGroup) -> void: building_groups.add_item(group.name, group.atlas_texture) @@ -55,6 +55,8 @@ func _on_buildings_item_selected(index: int) -> void: build_materials.add_child(image) var label = Label.new() label.text = str(selected_building.cost[res]) + if not ResourceManager.has_amount(res, selected_building.cost[res]): + label.add_theme_color_override("font_color", Color.RED) build_materials.add_child(label) description.show() diff --git a/scripts/cell_data.gd b/scripts/cell_data.gd index 2f48ebe..19d1b49 100644 --- a/scripts/cell_data.gd +++ b/scripts/cell_data.gd @@ -25,6 +25,9 @@ func change_layer(layer: int, data: Variant) -> bool: func change_resource(data: GameResource) -> void: layer_info[Constants.TilemapLayers.ENVIRONMENT] = data +func change_building(data: BuildingBase) -> void: + layer_info[Constants.TilemapLayers.BUILDINGS] = data + func get_resource() -> GameResource: return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource @@ -44,7 +47,7 @@ func is_interactable() -> bool: return has_resource() or has_building() func is_buildable() -> bool: - return not has_resource() or not has_building() + return not has_resource() and not has_building() func get_interaction_options() -> Array[Interaction]: var interactions: Array[Interaction] = [] diff --git a/scripts/gui.gd b/scripts/gui.gd index 231af5a..88a514b 100644 --- a/scripts/gui.gd +++ b/scripts/gui.gd @@ -6,6 +6,9 @@ const SCIFI_TILESHEET = preload("res://assets/scifi_tilesheet@2.png") var _resource_displays := {} +func _ready() -> void: + ResourceManager.changed_resource.connect(_on_resource_manager_changed_resource) + func _on_resource_manager_changed_resource(changed: ResourceChangedSignal) -> void: for resource: GameResource in changed.changed_resources.keys(): if not _resource_displays.has(resource): diff --git a/scripts/interaction_bar.gd b/scripts/interaction_bar.gd index f41a482..21e6436 100644 --- a/scripts/interaction_bar.gd +++ b/scripts/interaction_bar.gd @@ -15,5 +15,5 @@ func _ready() -> void: func _cleanup_gather() -> void: interaction_finished.emit() var res: GameResource = Grid.get_location_data(grid_position).get_resource() - res.gained_resource.emit(res) + ResourceManager.pickup(res) self.queue_free() diff --git a/scripts/player.gd b/scripts/player.gd index 0f58fae..ccdf334 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -35,13 +35,12 @@ func _physics_process(delta: float) -> void: if _build_placement: _build_placement.position = Grid.grid_to_world_center(interaction_location) + _build_placement.self_modulate = Color(0, 1, 0, 0.5) if Grid.get_location_data(interaction_location).is_buildable() else Color(1, 0, 0, 0.4) func _input(event: InputEvent) -> void: if event.is_action_pressed("interact"): if _attempting_build and Grid.get_location_data(interaction_location).is_buildable(): - var build: BuildingBase = BUILDING_BASE.instantiate() - build.initialize(_attempting_build, interaction_location) - add_sibling(build) + _build() elif not _interacting and Grid.get_location_data(interaction_location).is_interactable(): var interaction = INTERACTION_BAR.instantiate() interaction.interaction_finished.connect(_on_interaction_finished) @@ -64,6 +63,18 @@ func _on_build_menu_build(building: Building) -> void: _build_placement.self_modulate = Color(1, 0, 0, 0.4) add_sibling(_build_placement) +func _build() -> void: + ResourceManager.use_resources(_attempting_build.cost) + var build: BuildingBase = BUILDING_BASE.instantiate() + build.initialize(_attempting_build, interaction_location) + add_sibling(build) + Grid.change_location_building(interaction_location, build) + + if not ResourceManager.has_resources(_attempting_build.cost): + _attempting_build = null + _build_placement.queue_free() + _build_placement = null + func _on_interation_options_closed() -> void: _interaction_options = null diff --git a/scripts/world.gd b/scripts/world.gd index 4f7dc22..7c3431f 100644 --- a/scripts/world.gd +++ b/scripts/world.gd @@ -14,7 +14,6 @@ var noise_regions := [-999,0,999] var atlas_regions := [Vector2i(0,1),Vector2i(0,0)] @onready var world_grid: TileMap = $world_grid -@onready var resource_manager: Node2D = $ResourceManager func get_noise_value(x: int, y: int): return terrain_noise.get_noise_2d(x,y) * 500 @@ -62,7 +61,7 @@ func corrupt_location(loc: Vector2i): func add_resources_to_map() -> void: for res: GameResource in game_resources: - res.gained_resource.connect(resource_manager._on_gained_resource) + #res.gained_resource.connect(ResourceManager._on_gained_resource) var spawns = res.get_spawn_locations() for spawn in spawns: Grid.change_location_resource(spawn, res)