2023-11-18 15:27:42 +02:00
|
|
|
extends Node3D
|
|
|
|
|
2023-12-20 02:54:22 +02:00
|
|
|
const Room = preload("res://content/system/house/room/room.tscn")
|
|
|
|
const RoomType = preload("res://content/system/house/room/room.gd")
|
2023-12-09 22:45:18 +02:00
|
|
|
|
2023-12-21 19:35:43 +02:00
|
|
|
const material_selected = preload("./room_selected.tres")
|
|
|
|
const material_unselected = preload("./room_unselected.tres")
|
2023-12-21 17:44:56 +02:00
|
|
|
|
2023-12-01 22:37:36 +02:00
|
|
|
const window_scene = preload("./window.tscn")
|
2023-11-18 15:27:42 +02:00
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
@onready var background = $Background
|
2023-12-21 17:44:56 +02:00
|
|
|
@onready var room_button = $Interface/Button
|
2023-12-20 23:19:43 +02:00
|
|
|
@onready var input = $Interface/Input
|
|
|
|
@onready var rooms_map = $Interface/Rooms
|
|
|
|
|
|
|
|
var selected_room = null:
|
|
|
|
set(value):
|
2023-12-21 17:44:56 +02:00
|
|
|
if selected_room != null && value == null:
|
|
|
|
room_button.label = "add"
|
|
|
|
input.text = "New Room %s" % (rooms_map.get_child_count() + 1)
|
|
|
|
|
|
|
|
if selected_room != null:
|
|
|
|
var old_room = get_room(selected_room)
|
|
|
|
if old_room != null:
|
|
|
|
old_room.get_node("MeshInstance3D").material_override = material_unselected
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
if value != null:
|
2023-12-21 17:44:56 +02:00
|
|
|
input.text = value
|
|
|
|
edit_room = false
|
|
|
|
var new_room = get_room(value)
|
|
|
|
if new_room != null:
|
|
|
|
new_room.get_node("MeshInstance3D").material_override = material_selected
|
|
|
|
|
|
|
|
|
|
|
|
selected_room = value
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
var edit_room = false:
|
|
|
|
set(value):
|
|
|
|
edit_room = value
|
|
|
|
if value:
|
2023-12-21 17:44:56 +02:00
|
|
|
room_button.label = "save"
|
2023-12-20 23:19:43 +02:00
|
|
|
else:
|
2023-12-21 17:44:56 +02:00
|
|
|
room_button.label = "edit"
|
2023-11-18 15:27:42 +02:00
|
|
|
|
|
|
|
func _ready():
|
2023-11-26 03:01:27 +02:00
|
|
|
background.visible = false
|
2023-11-18 15:27:42 +02:00
|
|
|
|
2023-12-09 22:45:18 +02:00
|
|
|
HomeApi.on_connect.connect(func():
|
2023-12-20 02:54:22 +02:00
|
|
|
var rooms = House.body.get_rooms(0)
|
2023-11-29 23:24:38 +02:00
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
# for room in rooms:
|
|
|
|
# var mesh = room.wall_mesh
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
)
|
2023-11-18 15:27:42 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
room_button.on_button_down.connect(func():
|
|
|
|
if selected_room == null:
|
|
|
|
var room_name = input.text
|
|
|
|
if get_room(room_name) != null:
|
|
|
|
EventSystem.notify("Name already taken", EventNotify.Type.WARNING)
|
|
|
|
return
|
|
|
|
|
|
|
|
House.body.create_room(room_name, 0)
|
|
|
|
selected_room = room_name
|
2023-12-20 23:19:43 +02:00
|
|
|
edit_room = true
|
2023-12-21 17:44:56 +02:00
|
|
|
else:
|
|
|
|
if edit_room:
|
|
|
|
edit_room = false
|
2023-12-21 19:35:43 +02:00
|
|
|
|
|
|
|
if !House.body.is_valid_room(selected_room):
|
|
|
|
House.body.delete_room(selected_room)
|
|
|
|
selected_room = null
|
|
|
|
else:
|
|
|
|
House.body.edit_room(null)
|
2023-12-21 17:44:56 +02:00
|
|
|
_generate_room_map()
|
|
|
|
else:
|
|
|
|
edit_room = true
|
|
|
|
House.body.edit_room(selected_room)
|
2023-11-18 15:27:42 +02:00
|
|
|
)
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
func get_room(room_name):
|
|
|
|
if rooms_map.has_node("%s" % room_name):
|
|
|
|
return rooms_map.get_node("%s" % room_name)
|
|
|
|
return null
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
func _on_click(event: EventPointer):
|
|
|
|
if event.target.get_parent() == rooms_map:
|
|
|
|
var room_name = event.target.name
|
2023-12-21 17:44:56 +02:00
|
|
|
|
|
|
|
if selected_room == room_name:
|
|
|
|
selected_room = null
|
|
|
|
House.body.edit_room(null)
|
|
|
|
return
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
selected_room = room_name
|
|
|
|
|
|
|
|
func _generate_room_map():
|
|
|
|
var rooms = House.body.get_rooms(0)
|
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
var target_size = Vector2(0.3, 0.24)
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
for old_room in rooms_map.get_children():
|
|
|
|
old_room.queue_free()
|
2023-12-21 17:44:56 +02:00
|
|
|
await old_room.tree_exited
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
var current_min = null
|
|
|
|
var current_max = null
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
for room in rooms:
|
2023-12-21 17:44:56 +02:00
|
|
|
var mesh = room.ceiling_mesh.mesh
|
|
|
|
if mesh == null:
|
|
|
|
continue
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
var body = StaticBody3D.new()
|
2023-12-21 17:44:56 +02:00
|
|
|
body.name = room.name
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
var mesh_instance = MeshInstance3D.new()
|
2023-12-21 17:44:56 +02:00
|
|
|
mesh_instance.name = "MeshInstance3D"
|
2023-12-20 23:19:43 +02:00
|
|
|
mesh_instance.mesh = mesh
|
2023-12-21 17:44:56 +02:00
|
|
|
mesh_instance.material_override = material_unselected if selected_room != room.name else material_selected
|
2023-12-20 23:19:43 +02:00
|
|
|
body.add_child(mesh_instance)
|
|
|
|
|
|
|
|
var collision_shape = CollisionShape3D.new()
|
|
|
|
collision_shape.shape = mesh.create_trimesh_shape()
|
|
|
|
body.add_child(collision_shape)
|
|
|
|
|
|
|
|
rooms_map.add_child(body)
|
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
var aabb_position = room.ceiling_mesh.to_global(mesh.get_aabb().position)
|
|
|
|
var aabb_end = room.ceiling_mesh.to_global(mesh.get_aabb().end)
|
|
|
|
|
|
|
|
if current_min == null:
|
|
|
|
current_min = Vector2(aabb_position.x, aabb_position.z)
|
|
|
|
current_max = Vector2(aabb_end.x, aabb_end.z)
|
|
|
|
else:
|
|
|
|
if aabb_position.x < current_min.x:
|
|
|
|
current_min.x = aabb_position.x
|
|
|
|
|
|
|
|
if aabb_position.z < current_min.y:
|
|
|
|
current_min.y = aabb_position.z
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
if aabb_end.x > current_max.x:
|
|
|
|
current_max.x = aabb_end.x
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 17:44:56 +02:00
|
|
|
if aabb_end.z > current_max.y:
|
|
|
|
current_max.y = aabb_end.z
|
2023-12-20 23:19:43 +02:00
|
|
|
|
2023-12-21 19:35:43 +02:00
|
|
|
if current_min == null:
|
|
|
|
return
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
var current_size = current_max - current_min
|
|
|
|
var target_scale = target_size / current_size
|
2023-12-21 17:44:56 +02:00
|
|
|
var scale_value = min(target_scale.x, target_scale.y)
|
|
|
|
|
|
|
|
rooms_map.position.x = -current_min.x * scale_value
|
|
|
|
rooms_map.position.z = -current_min.y * scale_value
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
rooms_map.scale = Vector3(scale_value, scale_value, scale_value)
|