From 02e05b95b2f8d5328b1b8a7348ae7db3c14a52a5 Mon Sep 17 00:00:00 2001 From: Ategon Date: Mon, 23 Sep 2024 20:36:55 -0400 Subject: [PATCH] Add the ability to destroy buildings --- BuildingLevel.gd | 25 ++++++++++-- InfoWindow.tscn | 51 ++++++++++++++++++++++-- info_window.gd | 43 +++++++++++++++++++- main.gd | 8 ++++ parts/buildings/images/ruins.png | Bin 0 -> 215 bytes parts/buildings/images/ruins.png.import | 34 ++++++++++++++++ parts/buildings/ruins.txt | 2 + parts/globals.txt | 4 +- tiles_ruined_houses.png.import | 2 +- ui/building_menu.gd | 2 +- 10 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 parts/buildings/images/ruins.png create mode 100644 parts/buildings/images/ruins.png.import create mode 100644 parts/buildings/ruins.txt diff --git a/BuildingLevel.gd b/BuildingLevel.gd index 1ecf20f..cd9842e 100644 --- a/BuildingLevel.gd +++ b/BuildingLevel.gd @@ -212,7 +212,7 @@ func _on_flood_level(data): if building.has("workers"): Persister.change_value("avail_population", building.workers) elif build_data.type == "housing": - Persister.change_value("avail_population", -build_data.amount) + Persister.change_value("max_population", -build_data.amount) ruins_spots.push_back(coords) set_cell(coords, source, Vector2i(atlas,building.rand), 0) @@ -243,17 +243,30 @@ func pick_block(): Persister.persist_data("building_key", building_data[tile].key) +func destroy(tile: Vector2i): + if building_data.has(tile): + var building = building_data[tile] + var build_data = Data.data.buildings[building.key] + if building.has("workers"): + Persister.change_value("avail_population", building.workers) + elif build_data.type == "housing": + Persister.change_value("max_population", -build_data.amount) + building_data.erase(tile) + ruins_spots = ruins_spots.filter(func(coords): return coords != tile) + + func place_building(): if Persister.get_value("win") or Persister.get_value("lose"): return var tile = local_to_map(get_global_mouse_position()) - if building_spots.has(tile) and not building_data.has(tile) and not tree_spots.has(tile) and not rock_spots.has(tile) and not th_spots.has(tile) and not ruins_spots.has(tile): + if building_spots.has(tile) and not building_data.has(tile) and not tree_spots.has(tile) and not rock_spots.has(tile) and not th_spots.has(tile): Persister.persist_data("building_mode", false) building_data[tile] = { "key": Persister.get_value("building_key"), "rand": rand, - "level": level + "level": level, + "coords": tile } var build_data = Data.data.buildings[Persister.get_value("building_key")] @@ -282,3 +295,9 @@ func show_info_building(): var tile = local_to_map(get_global_mouse_position()) if building_data.has(tile): Triggerer.trigger("show_info", building_data[tile]) + elif ruins_spots.has(tile): + Triggerer.trigger("show_info", { + "key": "ruins", + "coords": tile, + "level": level + }) diff --git a/InfoWindow.tscn b/InfoWindow.tscn index db433b8..c44f38e 100644 --- a/InfoWindow.tscn +++ b/InfoWindow.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=7 format=3 uid="uid://byyyhq3x8t2d8"] +[gd_scene load_steps=8 format=3 uid="uid://byyyhq3x8t2d8"] [ext_resource type="Theme" uid="uid://d035h7upxrw3h" path="res://theme.tres" id="1_emusj"] [ext_resource type="Script" path="res://info_window.gd" id="1_oy31s"] @@ -9,6 +9,9 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_fnpfc"] size = Vector2(15, 16) +[sub_resource type="RectangleShape2D" id="RectangleShape2D_2wrr4"] +size = Vector2(41, 16) + [node name="InfoWindow" type="Control"] layout_mode = 3 anchors_preset = 6 @@ -148,9 +151,9 @@ anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 offset_left = 3.0 -offset_top = 26.0 +offset_top = 16.0 offset_right = 69.0 -offset_bottom = 66.0 +offset_bottom = 56.0 grow_horizontal = 2 grow_vertical = 2 theme = ExtResource("1_emusj") @@ -304,6 +307,45 @@ offset_right = 33.5 offset_bottom = 80.0 grow_horizontal = 2 +[node name="ColorRect6" type="ColorRect" parent="."] +self_modulate = Color(1, 1, 1, 0.647059) +layout_mode = 1 +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -20.5 +offset_top = -28.0 +offset_right = 20.5 +offset_bottom = -12.0 +grow_horizontal = 2 +grow_vertical = 0 +pivot_offset = Vector2(8, 9) +color = Color(0, 0, 0, 1) + +[node name="RichTextLabel" type="RichTextLabel" parent="ColorRect6"] +clip_contents = false +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -11.0 +offset_top = 5.0 +offset_right = 14.0 +offset_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_constants/outline_size = 3 +bbcode_enabled = true +text = "[center]Destroy" + +[node name="DestroyHandler" parent="ColorRect6" instance=ExtResource("5_38jqb")] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="ColorRect6/DestroyHandler"] +position = Vector2(20.5, 8) +shape = SubResource("RectangleShape2D_2wrr4") + [connection signal="clicked" from="ColorRect5/MouseHandler3" to="." method="_on_mouse_handler_3_clicked"] [connection signal="hovered" from="ColorRect5/MouseHandler3" to="." method="_on_mouse_handler_3_hovered"] [connection signal="unhovered" from="ColorRect5/MouseHandler3" to="." method="_on_mouse_handler_3_unhovered"] @@ -313,3 +355,6 @@ grow_horizontal = 2 [connection signal="clicked" from="ResourceAllocation/ColorRect3/MouseHandler2" to="." method="_on_mouse_handler_2_clicked"] [connection signal="hovered" from="ResourceAllocation/ColorRect3/MouseHandler2" to="." method="_on_mouse_handler_2_hovered"] [connection signal="unhovered" from="ResourceAllocation/ColorRect3/MouseHandler2" to="." method="_on_mouse_handler_2_unhovered"] +[connection signal="clicked" from="ColorRect6/DestroyHandler" to="." method="_on_destroy_handler_clicked"] +[connection signal="hovered" from="ColorRect6/DestroyHandler" to="." method="_on_destroy_handler_hovered"] +[connection signal="unhovered" from="ColorRect6/DestroyHandler" to="." method="_on_destroy_handler_unhovered"] diff --git a/info_window.gd b/info_window.gd index 309af33..6c1fbba 100644 --- a/info_window.gd +++ b/info_window.gd @@ -14,12 +14,14 @@ var window_tween @onready var color_rect_3: ColorRect = $ResourceAllocation/ColorRect3 @onready var color_rect_4: ColorRect = $ResourceAllocation/ColorRect4 @onready var description_2: RichTextLabel = $ResourceAllocation/Description2 +@onready var color_rect_6: ColorRect = $ColorRect6 var recent_data var close_tween var minus_tween var plus_tween +var destroy_tween func _ready() -> void: Triggerer.listen("show_info", _show_info_window) @@ -51,7 +53,7 @@ func _show_info_window(data): window_tween.tween_callback(func(): title.text = "[center]%s" % [building_data.name] icon.texture = Data.data.images[data.key] - subtitle.text = "[center]%s" % [building_data.short] + subtitle.text = "[center]%s" % [building_data.short if building_data.has("short") else ""] if data.has("workers"): rich_text_label_2.text = "[center]%d/%d" % [data.workers, data.max_workers] description_3.text = "[center]EFFICIENCY: %d%%" % [data.efficiency] @@ -192,3 +194,42 @@ func _on_mouse_handler_3_unhovered() -> void: close_tween.set_ease(Tween.EASE_OUT) close_tween.set_trans(Tween.TRANS_QUAD) close_tween.tween_property(color_rect_5, "scale", Vector2.ONE, 0.25) + + +func _on_destroy_handler_clicked() -> void: + Triggerer.trigger("destroy", {"tile": recent_data.coords}) + Triggerer.trigger("hide_info") + + var data = Data.data.buildings[recent_data.key] + + if data.has("cost"): + for key in data.cost: + Persister.change_value(key, ceil(data.cost[key]/2)) + + if destroy_tween: + destroy_tween.kill() + + destroy_tween = create_tween() + destroy_tween.set_trans(Tween.TRANS_QUAD) + destroy_tween.tween_property(color_rect_6, "scale", Vector2.ONE * 0.95, 0.125) + destroy_tween.tween_property(color_rect_6, "scale", Vector2.ONE, 0.125) + + +func _on_destroy_handler_hovered() -> void: + if destroy_tween: + destroy_tween.kill() + + destroy_tween = create_tween() + destroy_tween.set_ease(Tween.EASE_OUT) + destroy_tween.set_trans(Tween.TRANS_BACK) + destroy_tween.tween_property(color_rect_6, "scale", Vector2.ONE * 1.05, 0.25) + + +func _on_destroy_handler_unhovered() -> void: + if close_tween: + close_tween.kill() + + destroy_tween = create_tween() + destroy_tween.set_ease(Tween.EASE_OUT) + destroy_tween.set_trans(Tween.TRANS_QUAD) + destroy_tween.tween_property(color_rect_6, "scale", Vector2.ONE, 0.25) diff --git a/main.gd b/main.gd index 44942e1..3a4ae02 100644 --- a/main.gd +++ b/main.gd @@ -14,6 +14,7 @@ var timer2 = 0 func _ready() -> void: Triggerer.listen("game_started", _on_game_started) + Triggerer.listen("destroy", _on_destroy) func _on_game_started(_data): @@ -90,6 +91,13 @@ func _process(delta: float) -> void: timer -= 5 +func _on_destroy(data): + var tile = data.tile + + for build_level in building_levels: + build_level.destroy(tile) + + func _kill_citizen(): for build_level in building_levels: var val = build_level.kill_citizen() diff --git a/parts/buildings/images/ruins.png b/parts/buildings/images/ruins.png new file mode 100644 index 0000000000000000000000000000000000000000..e5adf4a8a419572830252b9979ce6abc2b62482c GIT binary patch literal 215 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|CVIL!hFJ6_ zCrGevVLY}=>3?sj>%Yg3l$q6XW?c<=YACX5FUn|Z1X4zG_Wog8 z*UviLe8Wzrn*nR$I(mB#uHsmB^XO5z{fsZ#?(F(2&{xPP!pwC_?ZDr%*WaAmZrqUA zvDQH9=6@cZh(ik&gTe~DWM4fD2-F% literal 0 HcmV?d00001 diff --git a/parts/buildings/images/ruins.png.import b/parts/buildings/images/ruins.png.import new file mode 100644 index 0000000..dd76bad --- /dev/null +++ b/parts/buildings/images/ruins.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bq1pxqo5u270n" +path="res://.godot/imported/ruins.png-d04f8883378a9d2a2942e00c3b80056b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://parts/buildings/images/ruins.png" +dest_files=["res://.godot/imported/ruins.png-d04f8883378a9d2a2942e00c3b80056b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/parts/buildings/ruins.txt b/parts/buildings/ruins.txt new file mode 100644 index 0000000..2d221bd --- /dev/null +++ b/parts/buildings/ruins.txt @@ -0,0 +1,2 @@ +name: Ruins +short: A ruined building diff --git a/parts/globals.txt b/parts/globals.txt index 3add872..12e0ef5 100644 --- a/parts/globals.txt +++ b/parts/globals.txt @@ -1,7 +1,7 @@ -time_to_flood_change: 25 +time_to_flood_change: 10 flood_levels[] 0 - 1 + 4 0 1 2 diff --git a/tiles_ruined_houses.png.import b/tiles_ruined_houses.png.import index 8df4ccb..c46c32c 100644 --- a/tiles_ruined_houses.png.import +++ b/tiles_ruined_houses.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://cps3axbh7qgc0" +uid="uid://dok8p5jnxckak" path="res://.godot/imported/tiles_ruined_houses.png-11ac51b49ecd60169c9343b644bb8b82.ctex" metadata={ "vram_texture": false diff --git a/ui/building_menu.gd b/ui/building_menu.gd index d5d1893..9d6a976 100644 --- a/ui/building_menu.gd +++ b/ui/building_menu.gd @@ -37,7 +37,7 @@ func swap_to_tab(index): else: tabs[i].unselect() - var filtered_buildings = buildings.filter(func(building): return building_data[building].type == tabs[index].key and (not building_data[building].has("disabled") or not building_data[building].disabled)) + var filtered_buildings = buildings.filter(func(building): return building_data[building].has("type") and building_data[building].type == tabs[index].key and (not building_data[building].has("disabled") or not building_data[building].disabled)) for i in range(0, icons.size()): if i < filtered_buildings.size():