Made building consume resources
Now may build more than one if resources allow building placement is red/green for indicating success
This commit is contained in:
@@ -37,3 +37,6 @@ func get_location_data(pos: Vector2i) -> CellData:
|
|||||||
func change_location_resource(pos: Vector2i, data: GameResource) -> void:
|
func change_location_resource(pos: Vector2i, data: GameResource) -> void:
|
||||||
world_data[pos].change_resource(data)
|
world_data[pos].change_resource(data)
|
||||||
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location)
|
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)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
|
extends Node
|
||||||
signal changed_resource
|
signal changed_resource
|
||||||
|
|
||||||
@export var resources := {}
|
@export var resources := {}
|
||||||
@@ -10,6 +10,29 @@ func _on_gained_resource(res: GameResource) -> void:
|
|||||||
}
|
}
|
||||||
changed_resource.emit(ResourceChangedSignal.new(changed_resources))
|
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:
|
#func _on_timer_timeout() -> void:
|
||||||
#var corrupted_resources := Grid.get_corrupted_resources()
|
#var corrupted_resources := Grid.get_corrupted_resources()
|
||||||
#if corrupted_resources.size() > 0:
|
#if corrupted_resources.size() > 0:
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ const BASIC_BUILDING_GROUP = preload("res://data/buildings/basic/basic_building_
|
|||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
_add_building_group(BASIC_BUILDING_GROUP)
|
_add_building_group(BASIC_BUILDING_GROUP)
|
||||||
_add_building_group(ADVANCED_BUILDING_GROUP)
|
_add_building_group(ADVANCED_BUILDING_GROUP)
|
||||||
building_groups.set_focus()
|
#building_groups.set_focus()
|
||||||
|
|
||||||
func _add_building_group(group: BuildingGroup) -> void:
|
func _add_building_group(group: BuildingGroup) -> void:
|
||||||
building_groups.add_item(group.name, group.atlas_texture)
|
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)
|
build_materials.add_child(image)
|
||||||
var label = Label.new()
|
var label = Label.new()
|
||||||
label.text = str(selected_building.cost[res])
|
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)
|
build_materials.add_child(label)
|
||||||
|
|
||||||
description.show()
|
description.show()
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ func change_layer(layer: int, data: Variant) -> bool:
|
|||||||
func change_resource(data: GameResource) -> void:
|
func change_resource(data: GameResource) -> void:
|
||||||
layer_info[Constants.TilemapLayers.ENVIRONMENT] = data
|
layer_info[Constants.TilemapLayers.ENVIRONMENT] = data
|
||||||
|
|
||||||
|
func change_building(data: BuildingBase) -> void:
|
||||||
|
layer_info[Constants.TilemapLayers.BUILDINGS] = data
|
||||||
|
|
||||||
func get_resource() -> GameResource:
|
func get_resource() -> GameResource:
|
||||||
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
|
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
|
||||||
|
|
||||||
@@ -44,7 +47,7 @@ func is_interactable() -> bool:
|
|||||||
return has_resource() or has_building()
|
return has_resource() or has_building()
|
||||||
|
|
||||||
func is_buildable() -> bool:
|
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]:
|
func get_interaction_options() -> Array[Interaction]:
|
||||||
var interactions: Array[Interaction] = []
|
var interactions: Array[Interaction] = []
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ const SCIFI_TILESHEET = preload("res://assets/scifi_tilesheet@2.png")
|
|||||||
|
|
||||||
var _resource_displays := {}
|
var _resource_displays := {}
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
ResourceManager.changed_resource.connect(_on_resource_manager_changed_resource)
|
||||||
|
|
||||||
func _on_resource_manager_changed_resource(changed: ResourceChangedSignal) -> void:
|
func _on_resource_manager_changed_resource(changed: ResourceChangedSignal) -> void:
|
||||||
for resource: GameResource in changed.changed_resources.keys():
|
for resource: GameResource in changed.changed_resources.keys():
|
||||||
if not _resource_displays.has(resource):
|
if not _resource_displays.has(resource):
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ func _ready() -> void:
|
|||||||
func _cleanup_gather() -> void:
|
func _cleanup_gather() -> void:
|
||||||
interaction_finished.emit()
|
interaction_finished.emit()
|
||||||
var res: GameResource = Grid.get_location_data(grid_position).get_resource()
|
var res: GameResource = Grid.get_location_data(grid_position).get_resource()
|
||||||
res.gained_resource.emit(res)
|
ResourceManager.pickup(res)
|
||||||
self.queue_free()
|
self.queue_free()
|
||||||
|
|||||||
@@ -35,13 +35,12 @@ func _physics_process(delta: float) -> void:
|
|||||||
|
|
||||||
if _build_placement:
|
if _build_placement:
|
||||||
_build_placement.position = Grid.grid_to_world_center(interaction_location)
|
_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:
|
func _input(event: InputEvent) -> void:
|
||||||
if event.is_action_pressed("interact"):
|
if event.is_action_pressed("interact"):
|
||||||
if _attempting_build and Grid.get_location_data(interaction_location).is_buildable():
|
if _attempting_build and Grid.get_location_data(interaction_location).is_buildable():
|
||||||
var build: BuildingBase = BUILDING_BASE.instantiate()
|
_build()
|
||||||
build.initialize(_attempting_build, interaction_location)
|
|
||||||
add_sibling(build)
|
|
||||||
elif not _interacting and Grid.get_location_data(interaction_location).is_interactable():
|
elif not _interacting and Grid.get_location_data(interaction_location).is_interactable():
|
||||||
var interaction = INTERACTION_BAR.instantiate()
|
var interaction = INTERACTION_BAR.instantiate()
|
||||||
interaction.interaction_finished.connect(_on_interaction_finished)
|
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)
|
_build_placement.self_modulate = Color(1, 0, 0, 0.4)
|
||||||
add_sibling(_build_placement)
|
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:
|
func _on_interation_options_closed() -> void:
|
||||||
_interaction_options = null
|
_interaction_options = null
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ var noise_regions := [-999,0,999]
|
|||||||
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
|
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
|
||||||
|
|
||||||
@onready var world_grid: TileMap = $world_grid
|
@onready var world_grid: TileMap = $world_grid
|
||||||
@onready var resource_manager: Node2D = $ResourceManager
|
|
||||||
|
|
||||||
func get_noise_value(x: int, y: int):
|
func get_noise_value(x: int, y: int):
|
||||||
return terrain_noise.get_noise_2d(x,y) * 500
|
return terrain_noise.get_noise_2d(x,y) * 500
|
||||||
@@ -62,7 +61,7 @@ func corrupt_location(loc: Vector2i):
|
|||||||
|
|
||||||
func add_resources_to_map() -> void:
|
func add_resources_to_map() -> void:
|
||||||
for res: GameResource in game_resources:
|
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()
|
var spawns = res.get_spawn_locations()
|
||||||
for spawn in spawns:
|
for spawn in spawns:
|
||||||
Grid.change_location_resource(spawn, res)
|
Grid.change_location_resource(spawn, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user