implement way for adding and removing corners

This commit is contained in:
Nitwel 2023-12-21 18:35:43 +01:00
parent 40f6c00eb8
commit b38a492bcc
11 changed files with 108 additions and 34 deletions

View File

@ -35,6 +35,27 @@ func edit_room(room_name):
room.editable = true room.editable = true
editing_room = room 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): func is_editiong(room_name):
return editing_room != null && editing_room.name == room_name return editing_room != null && editing_room.name == room_name

View File

@ -29,6 +29,10 @@ func get_edge(index: int) -> MeshInstance3D:
func has_point(point: Vector3) -> bool: func has_point(point: Vector3) -> bool:
return wall_mesh.get_aabb().has_point(point) 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(): func _save():
return { return {
"corners": wall_corners.get_children().map(func(corner): return corner.position), "corners": wall_corners.get_children().map(func(corner): return corner.position),

View File

@ -5,9 +5,10 @@ const wall_edge_scene = preload("../wall_edge.tscn")
const RoomState = preload("./room_state.gd") const RoomState = preload("./room_state.gd")
var moving = null var moving = null
var deleting: bool = false
var floor_corner: StaticBody3D = null var floor_corner: StaticBody3D = null
var height_corner: StaticBody3D = null var height_corner: StaticBody3D = null
var height_edge: MeshInstance3D = null var height_edge: StaticBody3D = null
func _on_enter(): func _on_enter():
room.wall_corners.visible = true room.wall_corners.visible = true
@ -50,7 +51,7 @@ func _on_click_floor(event):
room.room_ceiling.get_node("CollisionShape3D").disabled = false room.room_ceiling.get_node("CollisionShape3D").disabled = false
func _on_click_ceiling(event): 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 return
var pos = event.ray.get_collision_point() 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) 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): floor_corner.get_node("Clickable").on_grab_down.connect(func(event):
if !is_active(): if !is_active() || moving != null:
return return
moving = event.target moving = event.target
@ -76,15 +77,14 @@ func add_floor_corner(position: Vector3):
if moving == null: if moving == null:
return return
var moving_index = height_corner.get_index()
var direction = -event.ray.global_transform.basis.z 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) var new_position = room.room_floor.get_node("CollisionShape3D").shape.plane.intersects_ray(event.ray.global_position, direction)
if new_position == null: if new_position == null:
# mark for deletion
return return
moving.position = new_position 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) 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.position.z = position.z
height_corner.get_node("Clickable").on_grab_down.connect(func(event): height_corner.get_node("Clickable").on_grab_down.connect(func(event):
if !is_active(): if !is_active() || moving != null:
return return
moving = event.target moving = event.target
@ -144,13 +144,13 @@ func add_height_corner(position: Vector3):
room.wall_corners.add_child(height_corner) 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() var corner = wall_corner_scene.instantiate()
corner.position.x = position.x corner.position.x = position.x
corner.position.z = position.z corner.position.z = position.z
corner.get_node("Clickable").on_grab_down.connect(func(event): corner.get_node("Clickable").on_grab_down.connect(func(event):
if !is_active(): if !is_active() || moving != null:
return return
moving = event.target moving = event.target
@ -160,17 +160,33 @@ func add_corner(position: Vector3):
if moving == null: if moving == null:
return return
var moving_index = moving.get_index()
var direction = -event.ray.global_transform.basis.z var direction = -event.ray.global_transform.basis.z
var ceiling_plane = Plane(Vector3.DOWN, room.room_ceiling.global_position) var ceiling_plane = Plane(Vector3.DOWN, room.room_ceiling.global_position)
var new_position = ceiling_plane.intersects_ray(event.ray.global_position, direction) var new_position = ceiling_plane.intersects_ray(event.ray.global_position, direction)
if new_position == null: 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 return
deleting = false
new_position.y = 0 new_position.y = 0
moving.position = new_position moving.position = new_position
var moving_index = moving.get_index()
if room.wall_edges.get_child_count() == 0: if room.wall_edges.get_child_count() == 0:
return return
@ -180,29 +196,43 @@ func add_corner(position: Vector3):
) )
corner.get_node("Clickable").on_grab_up.connect(func(_event): 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 moving = null
deleting = false
) )
room.wall_corners.add_child(corner) room.wall_corners.add_child(corner)
room.wall_corners.move_child(corner, index)
var num_corners = room.wall_corners.get_child_count() var num_corners = room.wall_corners.get_child_count()
var edge var edge
if num_corners > 1: 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(): 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: else:
room.wall_edges.move_child(edge, num_corners - 2) room.get_edge(index - 1).transform = corners_to_edge_transform(room.get_corner(index - 1).position, position)
room.get_edge(-1).transform = corners_to_edge_transform(position, room.get_corner(0).position)
func add_edge(from_pos: Vector3, to_pos: Vector3): func add_edge(from_pos: Vector3, to_pos: Vector3, index: int = -1):
var edge: MeshInstance3D = wall_edge_scene.instantiate() var edge: StaticBody3D = wall_edge_scene.instantiate()
edge.transform = corners_to_edge_transform(from_pos, to_pos) 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.add_child(edge)
room.wall_edges.move_child(edge, index)
return edge return edge
func corners_to_edge_transform(from_pos: Vector3, to_pos: Vector3) -> Transform3D: func corners_to_edge_transform(from_pos: Vector3, to_pos: Vector3) -> Transform3D:

View File

@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_e5awq"] [ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_e5awq"]
[sub_resource type="SphereShape3D" id="SphereShape3D_86rci"] [sub_resource type="SphereShape3D" id="SphereShape3D_86rci"]
radius = 0.05 radius = 0.1
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_i1w3w"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_i1w3w"]
transparency = 1 transparency = 1

View File

@ -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"] [sub_resource type="CapsuleMesh" id="CapsuleMesh_a2dct"]
material = ExtResource("1_b21dw") material = ExtResource("1_hs3tw")
radius = 0.01 radius = 0.01
height = 1.0 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") mesh = SubResource("CapsuleMesh_a2dct")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("CapsuleShape3D_6m53l")
[node name="Clickable" type="Node" parent="."]
script = ExtResource("2_cfvx2")

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:935bcbcfaa2d5dca17ce3efea167357dca8b5ac05f49d5b322add9a84607b4f0 oid sha256:fe7c098cfc9adadb447df0230eba8a81f1825dd3c0cf4e654ebf98ca2314c8cc
size 158 size 151

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d7af833108b37da9ba445d2da6dbac058b5894f550177bf917fb9ab9fb6ecd1c
size 172

View File

@ -3,8 +3,8 @@ extends Node3D
const Room = preload("res://content/system/house/room/room.tscn") const Room = preload("res://content/system/house/room/room.tscn")
const RoomType = preload("res://content/system/house/room/room.gd") const RoomType = preload("res://content/system/house/room/room.gd")
const material_selected = preload("res://content/system/house/room/walls_selected.tres") const material_selected = preload("./room_selected.tres")
const material_unselected = preload("res://content/system/house/room/walls.tres") const material_unselected = preload("./room_unselected.tres")
const window_scene = preload("./window.tscn") const window_scene = preload("./window.tscn")
@ -66,7 +66,12 @@ func _ready():
else: else:
if edit_room: if edit_room:
edit_room = false 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() _generate_room_map()
else: else:
edit_room = true edit_room = true
@ -141,13 +146,11 @@ func _generate_room_map():
if aabb_end.z > current_max.y: if aabb_end.z > current_max.y:
current_max.y = aabb_end.z current_max.y = aabb_end.z
print(current_min, " ", current_max) if current_min == null:
return
var current_size = current_max - current_min var current_size = current_max - current_min
var target_scale = target_size / current_size var target_scale = target_size / current_size
print(target_scale)
var scale_value = min(target_scale.x, target_scale.y) var scale_value = min(target_scale.x, target_scale.y)
rooms_map.position.x = -current_min.x * scale_value rooms_map.position.x = -current_min.x * scale_value

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:317bb38a3e58a63bfefaa8c202116a2f9938c5f226eb7404752ceb1e32bdd1ef
size 134

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c68faf32c34f6ca3e0ed9ef4ab76f7aafc34d5745163d932d0dbfbddd12ec0b6
size 101