From b38a492bcc9c48dee962922984e619393d970c76 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Thu, 21 Dec 2023 18:35:43 +0100 Subject: [PATCH] implement way for adding and removing corners --- content/system/house/house.gd | 21 ++++++ .../{ui/menu => system/house}/room/edge.tres | 0 content/system/house/room/room.gd | 4 ++ content/system/house/room/states/edit.gd | 64 ++++++++++++++----- content/system/house/room/wall_corner.tscn | 2 +- content/system/house/room/wall_edge.tscn | 21 ++++-- content/system/house/room/walls.tres | 4 +- content/system/house/room/walls_selected.tres | 3 - content/ui/menu/room/room_menu.gd | 17 +++-- content/ui/menu/room/room_selected.tres | 3 + content/ui/menu/room/room_unselected.tres | 3 + 11 files changed, 108 insertions(+), 34 deletions(-) rename content/{ui/menu => system/house}/room/edge.tres (100%) delete mode 100644 content/system/house/room/walls_selected.tres create mode 100644 content/ui/menu/room/room_selected.tres create mode 100644 content/ui/menu/room/room_unselected.tres diff --git a/content/system/house/house.gd b/content/system/house/house.gd index 991b3a8..dd154a5 100644 --- a/content/system/house/house.gd +++ b/content/system/house/house.gd @@ -35,6 +35,27 @@ func edit_room(room_name): room.editable = true editing_room = room +func is_valid_room(room_name): + var room = find_room(room_name) + + if room == null: + return + + return room.wall_corners.get_child_count() >= 3 + + +func delete_room(room_name): + var room = find_room(room_name) + + if room == null: + return + + if editing_room == room: + editing_room = null + + room.get_parent().remove_child(room) + room.queue_free() + func is_editiong(room_name): return editing_room != null && editing_room.name == room_name diff --git a/content/ui/menu/room/edge.tres b/content/system/house/room/edge.tres similarity index 100% rename from content/ui/menu/room/edge.tres rename to content/system/house/room/edge.tres diff --git a/content/system/house/room/room.gd b/content/system/house/room/room.gd index 1cae35c..c0dbc7c 100644 --- a/content/system/house/room/room.gd +++ b/content/system/house/room/room.gd @@ -29,6 +29,10 @@ func get_edge(index: int) -> MeshInstance3D: func has_point(point: Vector3) -> bool: return wall_mesh.get_aabb().has_point(point) +func remove_corner(index: int): + get_corner(index).queue_free() + get_edge(index).queue_free() + func _save(): return { "corners": wall_corners.get_children().map(func(corner): return corner.position), diff --git a/content/system/house/room/states/edit.gd b/content/system/house/room/states/edit.gd index 22f124a..152a557 100644 --- a/content/system/house/room/states/edit.gd +++ b/content/system/house/room/states/edit.gd @@ -5,9 +5,10 @@ const wall_edge_scene = preload("../wall_edge.tscn") const RoomState = preload("./room_state.gd") var moving = null +var deleting: bool = false var floor_corner: StaticBody3D = null var height_corner: StaticBody3D = null -var height_edge: MeshInstance3D = null +var height_edge: StaticBody3D = null func _on_enter(): room.wall_corners.visible = true @@ -50,7 +51,7 @@ func _on_click_floor(event): room.room_ceiling.get_node("CollisionShape3D").disabled = false func _on_click_ceiling(event): - if floor_corner == null || height_corner == null: + if floor_corner == null || height_corner == null || event.target != room.room_ceiling: return var pos = event.ray.get_collision_point() @@ -66,7 +67,7 @@ func add_floor_corner(position: Vector3): height_edge.transform = corners_to_edge_transform(position, position + Vector3.UP * room.room_ceiling.global_position.y) floor_corner.get_node("Clickable").on_grab_down.connect(func(event): - if !is_active(): + if !is_active() || moving != null: return moving = event.target @@ -76,15 +77,14 @@ func add_floor_corner(position: Vector3): if moving == null: return + var moving_index = height_corner.get_index() var direction = -event.ray.global_transform.basis.z var new_position = room.room_floor.get_node("CollisionShape3D").shape.plane.intersects_ray(event.ray.global_position, direction) if new_position == null: - # mark for deletion return moving.position = new_position - var moving_index = height_corner.get_index() height_edge.transform = corners_to_edge_transform(new_position, new_position + Vector3.UP * room.room_ceiling.global_position.y) @@ -111,7 +111,7 @@ func add_height_corner(position: Vector3): height_corner.position.z = position.z height_corner.get_node("Clickable").on_grab_down.connect(func(event): - if !is_active(): + if !is_active() || moving != null: return moving = event.target @@ -144,13 +144,13 @@ func add_height_corner(position: Vector3): room.wall_corners.add_child(height_corner) -func add_corner(position: Vector3): +func add_corner(position: Vector3, index: int = -1): var corner = wall_corner_scene.instantiate() corner.position.x = position.x corner.position.z = position.z corner.get_node("Clickable").on_grab_down.connect(func(event): - if !is_active(): + if !is_active() || moving != null: return moving = event.target @@ -160,17 +160,33 @@ func add_corner(position: Vector3): if moving == null: return + var moving_index = moving.get_index() var direction = -event.ray.global_transform.basis.z var ceiling_plane = Plane(Vector3.DOWN, room.room_ceiling.global_position) var new_position = ceiling_plane.intersects_ray(event.ray.global_position, direction) if new_position == null: + deleting = true + + new_position = event.ray.global_position + direction + + room.get_corner(moving_index).global_position = new_position + + if room.wall_edges.get_child_count() == 0: + return + + var edge_transform = corners_to_edge_transform(room.get_corner(moving_index - 1).position, room.get_corner(moving_index + 1).position) + room.get_edge(moving_index).transform = edge_transform + room.get_edge(moving_index - 1).transform = edge_transform + return + deleting = false + new_position.y = 0 moving.position = new_position - var moving_index = moving.get_index() + if room.wall_edges.get_child_count() == 0: return @@ -180,29 +196,43 @@ func add_corner(position: Vector3): ) corner.get_node("Clickable").on_grab_up.connect(func(_event): + if deleting: + var moving_index = moving.get_index() + room.remove_corner(moving_index) + moving = null + deleting = false ) room.wall_corners.add_child(corner) - + room.wall_corners.move_child(corner, index) var num_corners = room.wall_corners.get_child_count() var edge if num_corners > 1: - edge = add_edge(room.wall_corners.get_child(num_corners - 2).position, position) + edge = add_edge(position, room.get_corner(index + 1).position, index) - if num_corners > 2: + if num_corners > 2: if num_corners != room.wall_edges.get_child_count(): - add_edge(position, room.wall_corners.get_child(0).position) + add_edge(room.get_corner(-2).position, room.get_corner(-1).position, -2) else: - room.wall_edges.move_child(edge, num_corners - 2) - room.get_edge(-1).transform = corners_to_edge_transform(position, room.get_corner(0).position) + room.get_edge(index - 1).transform = corners_to_edge_transform(room.get_corner(index - 1).position, position) + -func add_edge(from_pos: Vector3, to_pos: Vector3): - var edge: MeshInstance3D = wall_edge_scene.instantiate() +func add_edge(from_pos: Vector3, to_pos: Vector3, index: int = -1): + var edge: StaticBody3D = wall_edge_scene.instantiate() edge.transform = corners_to_edge_transform(from_pos, to_pos) + + edge.get_node("Clickable").on_press_down.connect(func(event): + var point = event.ray.get_collision_point() + point.y = 0 + add_corner(point, edge.get_index() + 1) + ) + + room.wall_edges.add_child(edge) + room.wall_edges.move_child(edge, index) return edge func corners_to_edge_transform(from_pos: Vector3, to_pos: Vector3) -> Transform3D: diff --git a/content/system/house/room/wall_corner.tscn b/content/system/house/room/wall_corner.tscn index c12b7ed..f713d4c 100644 --- a/content/system/house/room/wall_corner.tscn +++ b/content/system/house/room/wall_corner.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_e5awq"] [sub_resource type="SphereShape3D" id="SphereShape3D_86rci"] -radius = 0.05 +radius = 0.1 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_i1w3w"] transparency = 1 diff --git a/content/system/house/room/wall_edge.tscn b/content/system/house/room/wall_edge.tscn index 21f2718..311a46c 100644 --- a/content/system/house/room/wall_edge.tscn +++ b/content/system/house/room/wall_edge.tscn @@ -1,11 +1,24 @@ -[gd_scene load_steps=3 format=3 uid="uid://dlj5chj7ndgua"] +[gd_scene load_steps=5 format=3 uid="uid://dlj5chj7ndgua"] -[ext_resource type="Material" uid="uid://j12e5wwthtaa" path="res://content/ui/menu/room/edge.tres" id="1_b21dw"] +[ext_resource type="Material" uid="uid://j12e5wwthtaa" path="res://content/system/house/room/edge.tres" id="1_hs3tw"] +[ext_resource type="Script" path="res://content/functions/clickable.gd" id="2_cfvx2"] [sub_resource type="CapsuleMesh" id="CapsuleMesh_a2dct"] -material = ExtResource("1_b21dw") +material = ExtResource("1_hs3tw") radius = 0.01 height = 1.0 -[node name="MeshInstance3D" type="MeshInstance3D"] +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_6m53l"] +radius = 0.04 +height = 1.0 + +[node name="StaticBody3D" type="StaticBody3D"] + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] mesh = SubResource("CapsuleMesh_a2dct") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +shape = SubResource("CapsuleShape3D_6m53l") + +[node name="Clickable" type="Node" parent="."] +script = ExtResource("2_cfvx2") diff --git a/content/system/house/room/walls.tres b/content/system/house/room/walls.tres index c9a6a4d..b0ebc48 100644 --- a/content/system/house/room/walls.tres +++ b/content/system/house/room/walls.tres @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:935bcbcfaa2d5dca17ce3efea167357dca8b5ac05f49d5b322add9a84607b4f0 -size 158 +oid sha256:fe7c098cfc9adadb447df0230eba8a81f1825dd3c0cf4e654ebf98ca2314c8cc +size 151 diff --git a/content/system/house/room/walls_selected.tres b/content/system/house/room/walls_selected.tres deleted file mode 100644 index 5ff9fcc..0000000 --- a/content/system/house/room/walls_selected.tres +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:d7af833108b37da9ba445d2da6dbac058b5894f550177bf917fb9ab9fb6ecd1c -size 172 diff --git a/content/ui/menu/room/room_menu.gd b/content/ui/menu/room/room_menu.gd index 6aad742..7ff2561 100644 --- a/content/ui/menu/room/room_menu.gd +++ b/content/ui/menu/room/room_menu.gd @@ -3,8 +3,8 @@ extends Node3D const Room = preload("res://content/system/house/room/room.tscn") const RoomType = preload("res://content/system/house/room/room.gd") -const material_selected = preload("res://content/system/house/room/walls_selected.tres") -const material_unselected = preload("res://content/system/house/room/walls.tres") +const material_selected = preload("./room_selected.tres") +const material_unselected = preload("./room_unselected.tres") const window_scene = preload("./window.tscn") @@ -66,7 +66,12 @@ func _ready(): else: if edit_room: edit_room = false - House.body.edit_room(null) + + if !House.body.is_valid_room(selected_room): + House.body.delete_room(selected_room) + selected_room = null + else: + House.body.edit_room(null) _generate_room_map() else: edit_room = true @@ -141,13 +146,11 @@ func _generate_room_map(): if aabb_end.z > current_max.y: current_max.y = aabb_end.z - print(current_min, " ", current_max) + if current_min == null: + return var current_size = current_max - current_min - var target_scale = target_size / current_size - - print(target_scale) var scale_value = min(target_scale.x, target_scale.y) rooms_map.position.x = -current_min.x * scale_value diff --git a/content/ui/menu/room/room_selected.tres b/content/ui/menu/room/room_selected.tres new file mode 100644 index 0000000..8b71a8e --- /dev/null +++ b/content/ui/menu/room/room_selected.tres @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:317bb38a3e58a63bfefaa8c202116a2f9938c5f226eb7404752ceb1e32bdd1ef +size 134 diff --git a/content/ui/menu/room/room_unselected.tres b/content/ui/menu/room/room_unselected.tres new file mode 100644 index 0000000..8271fca --- /dev/null +++ b/content/ui/menu/room/room_unselected.tres @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c68faf32c34f6ca3e0ed9ef4ab76f7aafc34d5745163d932d0dbfbddd12ec0b6 +size 101