From caa73b5f55e9e997bc9dd5392de528b04e2eef31 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Wed, 10 Apr 2024 16:52:23 +0200 Subject: [PATCH] clean up room collisions --- app/content/system/house/room/room.gd | 6 +++--- app/content/system/house/room/room.tscn | 6 +++++- app/content/system/house/room/states/view.gd | 16 ++++------------ app/content/ui/menu/room/views/rooms.gd | 4 ++-- app/lib/utils/mesh/construct_room_mesh.gd | 6 +++--- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/app/content/system/house/room/room.gd b/app/content/system/house/room/room.gd index e9f1107..f2ed2f6 100644 --- a/app/content/system/house/room/room.gd +++ b/app/content/system/house/room/room.gd @@ -6,7 +6,7 @@ const ConstructRoomMesh = preload ("res://lib/utils/mesh/construct_room_mesh.gd" @onready var wall_edges = $Ceiling/WallEdges @onready var wall_mesh: MeshInstance3D = $WallMesh @onready var ceiling_mesh: MeshInstance3D = $CeilingMesh -@onready var wall_collisions = $WallCollisions +@onready var wall_collision = $WallCollision/CollisionShape3D @onready var room_floor = $Floor @onready var room_ceiling = $Ceiling @@ -60,9 +60,9 @@ static func generate_wall_mesh(room_store: Dictionary): var corners = room_store.corners var height = room_store.height - return ConstructRoomMesh.generate_wall_mesh_grid(corners, height) + return ConstructRoomMesh.generate_wall_mesh(corners, height) static func generate_ceiling_mesh(room_store: Dictionary): var corners = room_store.corners - return ConstructRoomMesh.generate_ceiling_mesh_grid(corners) + return ConstructRoomMesh.generate_ceiling_mesh(corners) diff --git a/app/content/system/house/room/room.tscn b/app/content/system/house/room/room.tscn index d222beb..e86326e 100644 --- a/app/content/system/house/room/room.tscn +++ b/app/content/system/house/room/room.tscn @@ -28,7 +28,11 @@ script = ExtResource("1_ugebq") [node name="CeilingMesh" type="MeshInstance3D" parent="."] -[node name="WallCollisions" type="Node3D" parent="."] +[node name="WallCollision" type="StaticBody3D" parent="."] +collision_layer = 24 +collision_mask = 0 + +[node name="CollisionShape3D" type="CollisionShape3D" parent="WallCollision"] [node name="Ceiling" type="StaticBody3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0) diff --git a/app/content/system/house/room/states/view.gd b/app/content/system/house/room/states/view.gd index e21c7ec..0bd1552 100644 --- a/app/content/system/house/room/states/view.gd +++ b/app/content/system/house/room/states/view.gd @@ -29,20 +29,12 @@ func _on_enter(): floor_shape.shape = room.ceiling_mesh.mesh.create_trimesh_shape() ceiling_shape.shape.backface_collision = true - var collision = CollisionShape3D.new() - collision.shape = room.wall_mesh.mesh.create_trimesh_shape() - - var static_body = StaticBody3D.new() - static_body.set_collision_layer_value(4, true) - static_body.set_collision_layer_value(5, true) - static_body.collision_mask = 0 - static_body.add_child(collision) - room.wall_collisions.add_child(static_body) + var wall_collisions = room.wall_mesh.mesh.create_trimesh_shape() + wall_collisions.backface_collision = true + room.wall_collision.shape = wall_collisions func _on_leave(): room.room_ceiling.get_node("CollisionShape3D").disabled = true room.room_floor.get_node("CollisionShape3D").disabled = true - for collision in room.wall_collisions.get_children(): - collision.queue_free() - await collision.tree_exited + room.wall_collision.shape = null diff --git a/app/content/ui/menu/room/views/rooms.gd b/app/content/ui/menu/room/views/rooms.gd index 967583f..803f87c 100644 --- a/app/content/ui/menu/room/views/rooms.gd +++ b/app/content/ui/menu/room/views/rooms.gd @@ -1,7 +1,7 @@ extends Node3D const Room = preload ("res://content/system/house/room/room.tscn") -const RoomType = preload ("res://content/system/house/room/room.gd") +const ConstructRoomMesh = preload ("res://lib/utils/mesh/construct_room_mesh.gd") const material_selected = preload ("../room_selected.tres") const material_unselected = preload ("../room_unselected.tres") @@ -128,7 +128,7 @@ func _generate_room_map(): current_max.y = max(current_max.y, corner.y) for room in rooms: - var mesh = RoomType.generate_ceiling_mesh(room) + var mesh = ConstructRoomMesh.generate_ceiling_mesh(room.corners) if mesh == null: continue diff --git a/app/lib/utils/mesh/construct_room_mesh.gd b/app/lib/utils/mesh/construct_room_mesh.gd index 92da23f..2ee9f5f 100644 --- a/app/lib/utils/mesh/construct_room_mesh.gd +++ b/app/lib/utils/mesh/construct_room_mesh.gd @@ -10,13 +10,13 @@ static func generate_wall_mesh(corners, height): for corner in corners: var corner3D = Vector3(corner.x, 0, corner.y) - st.add_vertex(corner3D) st.add_vertex(corner3D + wall_up) + st.add_vertex(corner3D) var first_corner = Vector3(corners[0].x, 0, corners[0].y) - st.add_vertex(first_corner) st.add_vertex(first_corner + wall_up) + st.add_vertex(first_corner) st.index() st.generate_normals() @@ -205,4 +205,4 @@ static func _create_mesh(points: PackedVector2Array, triangles: PackedInt32Array var mesh = st.commit() - return mesh \ No newline at end of file + return mesh