immersive-home/content/system/house/room/states/view.gd

79 lines
2.4 KiB
GDScript3
Raw Normal View History

2023-12-19 00:15:48 +02:00
extends RoomState
2024-03-07 15:21:31 +02:00
const RoomState = preload ("./room_state.gd")
2023-12-19 00:15:48 +02:00
func _on_enter():
2024-01-27 16:13:43 +02:00
var room_store = Store.house.get_room(room.name)
2023-12-19 00:15:48 +02:00
2024-03-07 15:21:31 +02:00
if room_store == null||room_store.corners.size() < 3:
2023-12-19 00:15:48 +02:00
return
2024-01-30 18:47:38 +02:00
room.wall_mesh.visible = false
room.ceiling_mesh.visible = false
2024-01-27 16:13:43 +02:00
room.wall_mesh.mesh = Room.generate_wall_mesh(room_store)
2023-12-19 00:15:48 +02:00
if room.wall_mesh.mesh == null:
return
2024-03-07 15:21:31 +02:00
room.room_ceiling.position.y = room_store.height
2024-01-25 13:36:33 +02:00
var ceiling_shape = room.room_ceiling.get_node("CollisionShape3D")
var floor_shape = room.room_floor.get_node("CollisionShape3D")
ceiling_shape.disabled = false
floor_shape.disabled = false
2024-01-27 16:13:43 +02:00
room.ceiling_mesh.mesh = Room.generate_ceiling_mesh(room_store)
2024-01-25 13:36:33 +02:00
ceiling_shape.shape = room.ceiling_mesh.mesh.create_trimesh_shape()
floor_shape.shape = room.ceiling_mesh.mesh.create_trimesh_shape()
ceiling_shape.shape.backface_collision = true
2023-12-19 00:15:48 +02:00
var collisions = generate_collision()
for collision in collisions:
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)
func _on_leave():
2024-01-25 13:36:33 +02:00
room.room_ceiling.get_node("CollisionShape3D").disabled = true
room.room_floor.get_node("CollisionShape3D").disabled = true
2023-12-19 00:15:48 +02:00
for collision in room.wall_collisions.get_children():
collision.queue_free()
2024-01-27 16:13:43 +02:00
await collision.tree_exited
2023-12-19 00:15:48 +02:00
func generate_collision():
2024-01-27 16:13:43 +02:00
var room_store = Store.house.get_room(room.name)
2023-12-19 00:15:48 +02:00
var collision_shapes: Array[CollisionShape3D] = []
2024-01-27 16:13:43 +02:00
var corners = room_store.corners
for i in range(corners.size()):
var corner = Vector3(corners[i].x, 0, corners[i].y)
var next_corner_index = (i + 1) % corners.size()
var next_corner = Vector3(corners[next_corner_index].x, 0, corners[next_corner_index].y)
2023-12-19 00:15:48 +02:00
var shape = BoxShape3D.new()
2024-01-27 16:13:43 +02:00
shape.size = Vector3((next_corner - corner).length(), room_store.height, 0.04)
2023-12-19 00:15:48 +02:00
var transform = Transform3D()
2024-01-27 16:13:43 +02:00
var back_vector = (corner - next_corner).cross(Vector3.UP).normalized() * shape.size.z / 2
2023-12-19 00:15:48 +02:00
2024-03-07 15:21:31 +02:00
transform.basis = Basis((next_corner - corner).normalized(), Vector3.UP, back_vector.normalized())
2024-01-27 16:13:43 +02:00
transform.origin = corner + (next_corner - corner) / 2 + back_vector + Vector3.UP * shape.size.y / 2
2023-12-19 00:15:48 +02:00
var collision_shape = CollisionShape3D.new()
collision_shape.shape = shape
collision_shape.transform = transform
collision_shapes.append(collision_shape)
return collision_shapes