2023-12-20 02:54:22 +02:00
|
|
|
extends Node3D
|
|
|
|
|
2024-03-12 13:23:39 +02:00
|
|
|
const Room = preload ("./room/room.tscn")
|
|
|
|
const RoomType = preload ("./room/room.gd")
|
2024-04-10 14:23:17 +03:00
|
|
|
const Miniature = preload ("./mini/miniature.gd")
|
2024-04-28 21:13:09 +03:00
|
|
|
const Doors = preload ("./doors/doors.gd")
|
2024-04-10 14:23:17 +03:00
|
|
|
const AlignReference = preload ("./align_reference.gd")
|
2023-12-20 02:54:22 +02:00
|
|
|
|
|
|
|
@onready var levels = $Levels
|
2024-01-16 16:00:30 +02:00
|
|
|
@onready var collision_shape = $Levels/CollisionShape3D
|
2024-04-10 14:23:17 +03:00
|
|
|
@onready var align_reference: AlignReference = $AlignReference
|
|
|
|
@onready var mini_view: Miniature = $Levels/Miniature
|
2024-04-28 21:13:09 +03:00
|
|
|
@onready var doors: Doors = $Levels/Doors
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2024-01-24 18:43:44 +02:00
|
|
|
var fixing_reference: bool = false
|
2023-12-20 02:54:22 +02:00
|
|
|
var editing_room: RoomType = null
|
2024-04-17 17:49:58 +03:00
|
|
|
var loaded = R.state(false)
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2024-01-25 17:29:33 +02:00
|
|
|
func _ready():
|
|
|
|
Store.house.on_loaded.connect(func():
|
|
|
|
update_house()
|
|
|
|
)
|
|
|
|
|
|
|
|
func update_house():
|
2024-04-17 17:49:58 +03:00
|
|
|
loaded.value = false
|
2024-01-27 16:13:43 +02:00
|
|
|
for old_room in get_rooms(0):
|
|
|
|
old_room.queue_free()
|
|
|
|
await old_room.tree_exited
|
2024-01-25 17:29:33 +02:00
|
|
|
|
|
|
|
align_reference.update_align_reference()
|
|
|
|
|
2024-04-10 01:21:38 +03:00
|
|
|
for index in range(Store.house.state.rooms.size()):
|
2024-04-09 18:46:59 +03:00
|
|
|
var new_room = Store.house.state.rooms[index]
|
2024-03-21 11:51:58 +02:00
|
|
|
|
|
|
|
if new_room.corners.size() == 0:
|
2024-04-09 18:46:59 +03:00
|
|
|
Store.house.state.rooms.remove_at(index)
|
2024-03-21 11:51:58 +02:00
|
|
|
Store.house.save_local()
|
|
|
|
continue
|
|
|
|
|
2024-01-27 16:13:43 +02:00
|
|
|
create_room(new_room.name, 0)
|
2024-01-25 17:29:33 +02:00
|
|
|
|
2024-04-09 18:46:59 +03:00
|
|
|
for entity_index in range(Store.house.state.entities.size()):
|
|
|
|
var entity = Store.house.state.entities[entity_index]
|
2024-01-28 18:37:26 +02:00
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
var entity_instance = create_entity_in(entity.id, entity.room, entity.get("interface", null))
|
2024-01-27 16:38:47 +02:00
|
|
|
|
|
|
|
if entity_instance == null:
|
|
|
|
continue
|
|
|
|
|
2024-01-27 18:40:35 +02:00
|
|
|
entity_instance.global_position = entity.position
|
2024-01-25 17:29:33 +02:00
|
|
|
entity_instance.global_rotation = entity.rotation
|
2024-05-01 18:42:38 +03:00
|
|
|
entity_instance.scale = Vector3(entity.scale, entity.scale, entity.scale) if entity.has("scale") else Vector3(1, 1, 1)
|
2024-01-22 17:54:15 +02:00
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
loaded.value = true
|
|
|
|
|
2023-12-20 02:54:22 +02:00
|
|
|
func create_room(room_name: String, level: int) -> RoomType:
|
2024-01-27 16:13:43 +02:00
|
|
|
var existing_room = Store.house.get_room(room_name)
|
|
|
|
|
|
|
|
if existing_room == null:
|
2024-04-09 18:46:59 +03:00
|
|
|
Store.house.state.rooms.append({
|
2024-01-27 16:13:43 +02:00
|
|
|
"name": room_name,
|
|
|
|
"height": 2.0,
|
|
|
|
"corners": [],
|
|
|
|
})
|
2023-12-20 02:54:22 +02:00
|
|
|
|
|
|
|
var room = Room.instantiate()
|
|
|
|
room.name = room_name
|
|
|
|
|
|
|
|
get_level(level).add_child(room)
|
|
|
|
|
|
|
|
return room
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
func edit_room(room_name):
|
|
|
|
var room = find_room(room_name)
|
|
|
|
|
|
|
|
if room == editing_room:
|
|
|
|
return
|
|
|
|
|
2023-12-20 02:54:22 +02:00
|
|
|
if editing_room != null:
|
|
|
|
editing_room.editable = false
|
|
|
|
editing_room = null
|
2023-12-20 23:19:43 +02:00
|
|
|
|
|
|
|
if room != null:
|
|
|
|
room.editable = true
|
|
|
|
editing_room = room
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2023-12-21 19:35:43 +02:00
|
|
|
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.queue_free()
|
2024-01-27 16:38:47 +02:00
|
|
|
await room.tree_exited
|
2023-12-21 19:35:43 +02:00
|
|
|
|
2024-01-29 16:35:58 +02:00
|
|
|
var store_room = Store.house.get_room(room_name)
|
|
|
|
|
|
|
|
if store_room != null:
|
2024-04-09 18:46:59 +03:00
|
|
|
Store.house.state.rooms.erase(store_room)
|
2024-01-29 16:35:58 +02:00
|
|
|
|
|
|
|
Store.house.save_local()
|
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
func is_editiong(room_name):
|
2024-03-12 13:23:39 +02:00
|
|
|
return editing_room != null&&editing_room.name == room_name
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2023-12-20 23:19:43 +02:00
|
|
|
func find_room(room_name):
|
2024-01-25 17:29:33 +02:00
|
|
|
for room in get_rooms(0):
|
|
|
|
if room.name == room_name:
|
|
|
|
return room
|
2023-12-20 02:54:22 +02:00
|
|
|
return null
|
|
|
|
|
|
|
|
func find_room_at(entity_position: Vector3):
|
2024-01-25 17:29:33 +02:00
|
|
|
for room in get_rooms(0):
|
|
|
|
if room.has_point(entity_position):
|
|
|
|
return room
|
2023-12-20 02:54:22 +02:00
|
|
|
return null
|
|
|
|
|
2024-03-12 13:23:39 +02:00
|
|
|
func rename_room(old_room: String, new_name: String):
|
|
|
|
var room = find_room(old_room)
|
|
|
|
|
|
|
|
if room == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
room.name = new_name
|
|
|
|
|
|
|
|
var store_room = Store.house.get_room(old_room)
|
|
|
|
|
|
|
|
if store_room != null:
|
|
|
|
store_room.name = new_name
|
|
|
|
|
2024-03-12 13:41:46 +02:00
|
|
|
save_all_entities()
|
2024-03-12 13:23:39 +02:00
|
|
|
Store.house.save_local()
|
|
|
|
|
2023-12-20 02:54:22 +02:00
|
|
|
func get_level(level: int):
|
2023-12-20 23:19:43 +02:00
|
|
|
return levels.get_child(level)
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2024-01-16 16:00:30 +02:00
|
|
|
func get_level_aabb(level: int):
|
|
|
|
var rooms = get_level(level).get_children()
|
|
|
|
if rooms.size() == 0:
|
|
|
|
return AABB()
|
|
|
|
|
|
|
|
var min_pos = rooms[0].get_aabb().position
|
|
|
|
var max_pos = min_pos + rooms[0].get_aabb().size
|
|
|
|
|
|
|
|
for room in rooms:
|
|
|
|
var room_min = room.get_aabb().position
|
|
|
|
var room_max = room_min + room.get_aabb().size
|
|
|
|
|
|
|
|
min_pos.x = min(min_pos.x, room_min.x)
|
|
|
|
min_pos.y = min(min_pos.y, room_min.y)
|
|
|
|
min_pos.z = min(min_pos.z, room_min.z)
|
|
|
|
|
|
|
|
max_pos.x = max(max_pos.x, room_max.x)
|
|
|
|
max_pos.y = max(max_pos.y, room_max.y)
|
|
|
|
max_pos.z = max(max_pos.z, room_max.z)
|
|
|
|
|
|
|
|
return AABB(min_pos, max_pos - min_pos)
|
|
|
|
|
2023-12-20 02:54:22 +02:00
|
|
|
func get_rooms(level: int):
|
|
|
|
return get_level(level).get_children()
|
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
func create_entity(entity_id: String, entity_position: Vector3, type=null):
|
2023-12-20 02:54:22 +02:00
|
|
|
var room = find_room_at(entity_position)
|
|
|
|
|
|
|
|
if room == null:
|
2024-05-06 12:45:43 +03:00
|
|
|
return false
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
var entity = EntityFactory.create_entity(entity_id, type)
|
2023-12-20 02:54:22 +02:00
|
|
|
|
|
|
|
if entity == null:
|
2024-01-27 16:38:47 +02:00
|
|
|
return null
|
2023-12-20 02:54:22 +02:00
|
|
|
|
2024-01-25 17:29:33 +02:00
|
|
|
room.get_node("Entities").add_child(entity)
|
2023-12-20 02:54:22 +02:00
|
|
|
entity.global_position = entity_position
|
|
|
|
|
2024-01-27 16:13:43 +02:00
|
|
|
save_all_entities()
|
|
|
|
|
2024-01-25 17:29:33 +02:00
|
|
|
return entity
|
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
func create_entity_in(entity_id: String, room_name: String, type=null):
|
2024-01-27 16:38:47 +02:00
|
|
|
var room = find_room(room_name)
|
|
|
|
|
|
|
|
if room == null:
|
2024-05-06 12:45:43 +03:00
|
|
|
return false
|
2024-01-27 16:38:47 +02:00
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
var entity = EntityFactory.create_entity(entity_id, type)
|
2024-01-27 16:38:47 +02:00
|
|
|
|
|
|
|
if entity == null:
|
|
|
|
return null
|
|
|
|
|
|
|
|
room.get_node("Entities").add_child(entity)
|
|
|
|
entity.global_position = room.get_aabb().position + room.get_aabb().size / 2.0
|
|
|
|
|
|
|
|
return entity
|
|
|
|
|
2024-01-24 18:43:44 +02:00
|
|
|
func edit_reference():
|
|
|
|
fixing_reference = false
|
2024-01-29 13:37:25 +02:00
|
|
|
align_reference.visible = true
|
2024-01-24 18:43:44 +02:00
|
|
|
align_reference.disabled = false
|
|
|
|
|
|
|
|
func fix_reference():
|
|
|
|
fixing_reference = true
|
|
|
|
align_reference.disabled = false
|
2024-01-29 13:37:25 +02:00
|
|
|
align_reference.visible = true
|
2024-01-24 18:43:44 +02:00
|
|
|
align_reference.update_initial_positions()
|
|
|
|
|
|
|
|
func save_reference():
|
|
|
|
if fixing_reference:
|
2024-01-29 16:35:58 +02:00
|
|
|
for room in get_rooms(0):
|
|
|
|
room.editable = true
|
|
|
|
|
2024-01-24 18:43:44 +02:00
|
|
|
var align_transform = align_reference.global_transform
|
2024-01-29 16:35:58 +02:00
|
|
|
transform = align_reference.get_new_transform()
|
2024-01-24 18:43:44 +02:00
|
|
|
align_reference.global_transform = align_transform
|
|
|
|
|
2024-01-29 16:35:58 +02:00
|
|
|
align_reference.update_store()
|
|
|
|
|
|
|
|
for room in get_rooms(0):
|
|
|
|
room.editable = false
|
|
|
|
|
|
|
|
save_all_entities()
|
|
|
|
|
2024-01-24 18:43:44 +02:00
|
|
|
align_reference.disabled = true
|
2024-01-29 13:37:25 +02:00
|
|
|
align_reference.visible = false
|
2024-01-25 17:29:33 +02:00
|
|
|
align_reference.update_initial_positions()
|
|
|
|
|
|
|
|
Store.house.save_local()
|
|
|
|
|
|
|
|
func save_all_entities():
|
2024-04-09 18:46:59 +03:00
|
|
|
Store.house.state.entities.clear()
|
2024-01-25 17:29:33 +02:00
|
|
|
|
|
|
|
for room in get_rooms(0):
|
|
|
|
for entity in room.get_node("Entities").get_children():
|
|
|
|
var entity_data = {
|
|
|
|
"id": entity.entity_id,
|
|
|
|
"position": entity.global_position,
|
|
|
|
"rotation": entity.global_rotation,
|
2024-05-01 18:42:38 +03:00
|
|
|
"scale": entity.scale.x,
|
2024-04-11 17:51:30 +03:00
|
|
|
"room": String(room.name),
|
2024-01-25 17:29:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
if entity.has_method("get_interface"):
|
|
|
|
entity_data["interface"] = entity.get_interface()
|
|
|
|
|
2024-04-09 18:46:59 +03:00
|
|
|
Store.house.state.entities.append(entity_data)
|
2024-04-17 20:57:34 +03:00
|
|
|
|
|
|
|
Store.house.state.entities = Store.house.state.entities
|
2024-01-27 16:38:47 +02:00
|
|
|
Store.house.save_local()
|