From b94b414bb3b9a928ea97fb2dd923e5ba6d4322fe Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sat, 27 Jan 2024 15:38:47 +0100 Subject: [PATCH] Bug fixes and cleanup --- content/system/house/house.gd | 31 +++++++++++++++++++++--- content/system/house/room/states/edit.gd | 31 ++++++++++++------------ content/ui/menu/edit/edit_menu.gd | 5 +++- content/ui/menu/room/views/rooms.gd | 3 +++ 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/content/system/house/house.gd b/content/system/house/house.gd index 60df373..36678c2 100644 --- a/content/system/house/house.gd +++ b/content/system/house/house.gd @@ -38,7 +38,11 @@ func update_house(): create_room(new_room.name, 0) for entity in Store.house.entities: - var entity_instance = create_entity(entity.id, entity.position) + var entity_instance = create_entity_in(entity.id, entity.room) + + if entity_instance == null: + continue + entity_instance.global_rotation = entity.rotation func create_room(room_name: String, level: int) -> RoomType: @@ -92,6 +96,7 @@ func delete_room(room_name): room.get_parent().remove_child(room) room.queue_free() + await room.tree_exited func is_editiong(room_name): return editing_room != null && editing_room.name == room_name @@ -140,12 +145,12 @@ func create_entity(entity_id: String, entity_position: Vector3): var room = find_room_at(entity_position) if room == null: - return + return null var entity = EntityFactory.create_entity(entity_id) if entity == null: - return + return null room.get_node("Entities").add_child(entity) entity.global_position = entity_position @@ -154,6 +159,24 @@ func create_entity(entity_id: String, entity_position: Vector3): return entity +func create_entity_in(entity_id: String, room_name: String): + var room = find_room(room_name) + + if room == null: + return null + + var entity = EntityFactory.create_entity(entity_id) + + 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 + + save_all_entities() + + return entity + func update_mini_view(): collision_shape.disabled = !mini_view @@ -208,4 +231,4 @@ func save_all_entities(): Store.house.entities.append(entity_data) - Store.house.save_local() \ No newline at end of file + Store.house.save_local() diff --git a/content/system/house/room/states/edit.gd b/content/system/house/room/states/edit.gd index a796d32..edb3c97 100644 --- a/content/system/house/room/states/edit.gd +++ b/content/system/house/room/states/edit.gd @@ -22,6 +22,7 @@ func _on_enter(): add_floor_corner(Vector3(corners[0].x, 0, corners[0].y)) add_height_corner(Vector3(corners[0].x, 0, corners[0].y)) room.room_ceiling.position.y = room_store.height + height_edge.align_to_corners(floor_corner.global_position, height_corner.global_position) for i in range(1, corners.size()): add_corner(Vector3(corners[i].x, 0, corners[i].y)) @@ -117,14 +118,14 @@ func add_floor_corner(position: Vector3): height_edge.align_to_corners(new_position, new_position + Vector3.UP * room.room_ceiling.global_position.y) - room.get_corner(moving_index).position.x = new_position.x - room.get_corner(moving_index).position.z = new_position.z + get_corner(moving_index).position.x = new_position.x + get_corner(moving_index).position.z = new_position.z if room.wall_edges.get_child_count() == 0: return - room.get_edge(moving_index).align_to_corners(new_position, room.get_corner(moving_index + 1).position) - room.get_edge(moving_index - 1).align_to_corners(room.get_corner(moving_index - 1).position, new_position) + get_edge(moving_index).align_to_corners(new_position, get_corner(moving_index + 1).position) + get_edge(moving_index - 1).align_to_corners(get_corner(moving_index - 1).position, new_position) ) floor_corner.get_node("Clickable").on_grab_up.connect(func(_event): @@ -199,13 +200,13 @@ func add_corner(position: Vector3, index: int = -1): new_position = event.ray.global_position + direction - room.get_corner(moving_index).global_position = new_position + get_corner(moving_index).global_position = new_position if room.wall_edges.get_child_count() == 0: return - room.get_edge(moving_index).align_to_corners(room.get_corner(moving_index - 1).position, room.get_corner(moving_index + 1).position) - room.get_edge(moving_index - 1).transform = room.get_edge(moving_index).transform + get_edge(moving_index).align_to_corners(get_corner(moving_index - 1).position, get_corner(moving_index + 1).position) + get_edge(moving_index - 1).transform = get_edge(moving_index).transform return @@ -219,14 +220,14 @@ func add_corner(position: Vector3, index: int = -1): if room.wall_edges.get_child_count() == 0: return - room.get_edge(moving_index).align_to_corners(new_position, room.get_corner(moving_index + 1).position) - room.get_edge(moving_index - 1).align_to_corners(room.get_corner(moving_index - 1).position, new_position) + get_edge(moving_index).align_to_corners(new_position, get_corner(moving_index + 1).position) + get_edge(moving_index - 1).align_to_corners(get_corner(moving_index - 1).position, new_position) ) corner.get_node("Clickable").on_grab_up.connect(func(_event): if deleting: var moving_index = moving.get_index() - room.remove_corner(moving_index) + remove_corner(moving_index) moving = null deleting = false @@ -238,13 +239,13 @@ func add_corner(position: Vector3, index: int = -1): var num_corners = room.wall_corners.get_child_count() if num_corners > 1: - add_edge(position, room.get_corner(index + 1).position, index) + add_edge(position, get_corner(index + 1).position, index) if num_corners > 2: if num_corners != room.wall_edges.get_child_count(): - add_edge(room.get_corner(-2).position, room.get_corner(-1).position, -2) + add_edge(get_corner(-2).position, get_corner(-1).position, -2) else: - room.get_edge(index - 1).align_to_corners(room.get_corner(index - 1).position, position) + get_edge(index - 1).align_to_corners(get_corner(index - 1).position, position) func add_edge(from_pos: Vector3, to_pos: Vector3, index: int = -1): @@ -273,7 +274,7 @@ func update_store(): store_room.corners = corners store_room.height = room.room_ceiling.position.y - if corners.size() > 3: - Store.house.rooms.erase(store_room) + if corners.size() < 3: + House.body.delete_room(room.name) Store.house.save_local() diff --git a/content/ui/menu/edit/edit_menu.gd b/content/ui/menu/edit/edit_menu.gd index 247e717..6db33e9 100644 --- a/content/ui/menu/edit/edit_menu.gd +++ b/content/ui/menu/edit/edit_menu.gd @@ -146,7 +146,10 @@ func _on_entity_click(entity_name): AudioPlayer.play_effect("spawn") - House.body.create_entity(entity_name, global_position) + var entity = House.body.create_entity(entity_name, global_position) + + if entity == null: + EventSystem.notify("Entity is not in Room", EventNotify.Type.INFO) func clear_menu(): for child in devices_node.get_children(): diff --git a/content/ui/menu/room/views/rooms.gd b/content/ui/menu/room/views/rooms.gd index d899936..535fa4d 100644 --- a/content/ui/menu/room/views/rooms.gd +++ b/content/ui/menu/room/views/rooms.gd @@ -96,6 +96,9 @@ func _generate_room_map(): old_room.queue_free() await old_room.tree_exited + if rooms.size() == 0: + return + var current_min = Vector2(rooms[0].corners[0].x, rooms[0].corners[0].y) var current_max = current_min