diff --git a/content/entities/button/button.gd b/content/entities/button/button.gd index f198104..9e5713b 100644 --- a/content/entities/button/button.gd +++ b/content/entities/button/button.gd @@ -1,9 +1,12 @@ -extends Node3D +extends Entity + +const Entity = preload("../entity.gd") -var entity_id = "button.plug_printer_2" @onready var button = $Button func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) if stateInfo == null: diff --git a/content/entities/camera/camera.gd b/content/entities/camera/camera.gd index b350059..1e8b08e 100644 --- a/content/entities/camera/camera.gd +++ b/content/entities/camera/camera.gd @@ -1,6 +1,7 @@ -extends StaticBody3D +extends Entity + +const Entity = preload("../entity.gd") -@export var entity_id = "camera.bedroomspeaker" @export var view_width = 0.15 @onready var view = $View @@ -10,6 +11,8 @@ extends StaticBody3D # Called when the node enters the scene tree for the first time. func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) set_state(stateInfo) diff --git a/content/entities/entity.gd b/content/entities/entity.gd new file mode 100644 index 0000000..04d861d --- /dev/null +++ b/content/entities/entity.gd @@ -0,0 +1,11 @@ +extends StaticBody3D + +var entity_id: String + +func _ready(): + var movable = get_node("Movable") + + if movable: + movable.on_moved.connect(func(): + House.body.save_all_entities() + ) diff --git a/content/entities/light/light.gd b/content/entities/light/light.gd index 2641ead..5406ee4 100644 --- a/content/entities/light/light.gd +++ b/content/entities/light/light.gd @@ -1,6 +1,7 @@ -extends StaticBody3D +extends Entity + +const Entity = preload("../entity.gd") -@export var entity_id = "switch.plug_printer_2" @export var color_off = Color(0.23, 0.23, 0.23) @export var color_on = Color(1.0, 0.85, 0.0) @@ -12,6 +13,8 @@ var brightness = 0 # 0-255 # Called when the node enters the scene tree for the first time. func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) set_state(stateInfo["state"] == "on") diff --git a/content/entities/media_player/media_player.gd b/content/entities/media_player/media_player.gd index 0396b11..5efaa9c 100644 --- a/content/entities/media_player/media_player.gd +++ b/content/entities/media_player/media_player.gd @@ -1,6 +1,7 @@ -extends StaticBody3D +extends Entity + +const Entity = preload("../entity.gd") -@export var entity_id = "media_player.bedroomspeaker" @export var image_width = 0.15 @onready var previous = $Previous @@ -18,6 +19,8 @@ var volume = 50 # Called when the node enters the scene tree for the first time. func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) set_state(stateInfo) diff --git a/content/entities/sensor/sensor.gd b/content/entities/sensor/sensor.gd index ef48d10..ac22e43 100644 --- a/content/entities/sensor/sensor.gd +++ b/content/entities/sensor/sensor.gd @@ -1,10 +1,13 @@ -extends StaticBody3D +extends Entity + +const Entity = preload("../entity.gd") -@export var entity_id = "sensor.sun_next_dawn" @onready var label: Label3D = $Label # Called when the node enters the scene tree for the first time. func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) set_text(stateInfo) diff --git a/content/entities/switch/switch.gd b/content/entities/switch/switch.gd index 5d595f5..f91098b 100644 --- a/content/entities/switch/switch.gd +++ b/content/entities/switch/switch.gd @@ -1,10 +1,13 @@ -extends StaticBody3D +extends Entity + +const Entity = preload("../entity.gd") -@export var entity_id = "switch.plug_printer_2" @onready var sprite: AnimatedSprite3D = $Icon # Called when the node enters the scene tree for the first time. func _ready(): + super() + var stateInfo = await HomeApi.get_state(entity_id) if stateInfo == null: return diff --git a/content/functions/movable.gd b/content/functions/movable.gd index 46ad7ad..1937512 100644 --- a/content/functions/movable.gd +++ b/content/functions/movable.gd @@ -3,6 +3,7 @@ extends Function class_name Movable signal on_move(position: Vector3, rotation: Vector3) +signal on_moved() @export var restricted: bool = false @export var restrict_movement: Callable @@ -33,6 +34,7 @@ func _on_grab_move(_event: EventPointer): func _on_grab_up(event: EventPointer): event.initiator.node.remove_child(hit_node) + on_moved.emit() func _get_configuration_warnings() -> PackedStringArray: var warnings := PackedStringArray() diff --git a/content/system/house/house.gd b/content/system/house/house.gd index c3c6948..60df373 100644 --- a/content/system/house/house.gd +++ b/content/system/house/house.gd @@ -27,37 +27,35 @@ func _physics_process(delta): levels.scale.z = lerp(levels.scale.z, target_size, 10.0 * delta) func update_house(): - for room in get_rooms(0): - room.queue_free() + for old_room in get_rooms(0): + old_room.queue_free() + await old_room.tree_exited align_reference.update_align_reference() - for room in Store.house.rooms: - create_room(room.name, 0) - # TODO: Make room load itself! + for index in range(Store.house.rooms.size()): + var new_room = Store.house.rooms[index] + create_room(new_room.name, 0) for entity in Store.house.entities: var entity_instance = create_entity(entity.id, entity.position) entity_instance.global_rotation = entity.rotation func create_room(room_name: String, level: int) -> RoomType: - if editing_room != null: - editing_room.editable = false - editing_room = null + var existing_room = Store.house.get_room(room_name) + + if existing_room == null: + Store.house.rooms.append({ + "name": room_name, + "height": 2.0, + "corners": [], + }) var room = Room.instantiate() room.name = room_name - room.editable = true - editing_room = room get_level(level).add_child(room) - Store.house.rooms.append({ - "name": room_name, - "height": 2.0, - "corners": [], - }) - return room func edit_room(room_name): @@ -152,6 +150,8 @@ func create_entity(entity_id: String, entity_position: Vector3): room.get_node("Entities").add_child(entity) entity.global_position = entity_position + save_all_entities() + return entity func update_mini_view(): @@ -203,8 +203,9 @@ func save_all_entities(): "id": entity.entity_id, "position": entity.global_position, "rotation": entity.global_rotation, - "room": room.name + "room": String(room.name) } Store.house.entities.append(entity_data) - \ No newline at end of file + + Store.house.save_local() \ No newline at end of file diff --git a/content/system/house/room/room.gd b/content/system/house/room/room.gd index 4a495b0..ec9e131 100644 --- a/content/system/house/room/room.gd +++ b/content/system/house/room/room.gd @@ -20,47 +20,103 @@ var editable: bool = false: else: state_machine.change_to("View") -func get_corner(index: int) -> MeshInstance3D: - return wall_corners.get_child(index % wall_corners.get_child_count()) - -func get_edge(index: int) -> MeshInstance3D: - return wall_edges.get_child(index % wall_edges.get_child_count()) - func has_point(point: Vector3) -> bool: return get_aabb().has_point(point) -func remove_corner(index: int): - get_corner(index).queue_free() - get_edge(index).queue_free() - func get_aabb(): - if wall_corners.get_child_count() == 0: + var room_store = Store.house.get_room(name) + + var corners = room_store.corners + + if corners.size() == 0: return AABB() - var min_pos = wall_corners.get_child(0).position - var max_pos = wall_corners.get_child(0).position + var min_pos = Vector3(corners[0].x, 0, corners[0].y) + var max_pos = min_pos - for corner in wall_corners.get_children(): - min_pos.x = min(min_pos.x, corner.position.x) - min_pos.z = min(min_pos.z, corner.position.z) + for corner in corners: + min_pos.x = min(min_pos.x, corner.x) + min_pos.z = min(min_pos.z, corner.y) - max_pos.x = max(max_pos.x, corner.position.x) - max_pos.z = max(max_pos.z, corner.position.z) + max_pos.x = max(max_pos.x, corner.x) + max_pos.z = max(max_pos.z, corner.y) - min_pos.y = room_floor.position.y - max_pos.y = room_ceiling.position.y + min_pos.y = 0 + max_pos.y = room_store.height return AABB(to_global(min_pos), to_global(max_pos) - to_global(min_pos)) -func update_store(): - var store_room = Store.house.get_room(name) +static func generate_wall_mesh(room_store: Dictionary): + if room_store.corners.size() < 2: + return null - var corners = [] + var st = SurfaceTool.new() + var wall_up = Vector3.UP * room_store.height - for corner in wall_corners.get_children(): - corners.append(Vector2(corner.position.x, corner.position.z)) + st.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP) - store_room.corners = corners - store_room.height = room_ceiling.position.y + for corner in room_store.corners: + var corner3D = Vector3(corner.x, 0, corner.y) + + st.add_vertex(corner3D) + st.add_vertex(corner3D + wall_up) + + var first_corner = Vector3(room_store.corners[0].x, 0, room_store.corners[0].y) + + st.add_vertex(first_corner) + st.add_vertex(first_corner + wall_up) + + st.index() + st.generate_normals() + st.generate_tangents() + var mesh = st.commit() + + return mesh + +static func generate_ceiling_mesh(room_store: Dictionary): + + var points: PackedVector2Array = PackedVector2Array() + var edges: PackedInt32Array = PackedInt32Array() + var triangles: PackedInt32Array + + var corners = room_store.corners + + if corners.size() < 3: + return null + + for i in range(corners.size()): + var corner = corners[i] + points.append(Vector2(corner.x, corner.y)) + edges.append(i) + edges.append((i + 1) % corners.size()) + + var cdt: ConstrainedTriangulation = ConstrainedTriangulation.new() + + cdt.init(true, true, 0.1) + + cdt.insert_vertices(points) + cdt.insert_edges(edges) + + cdt.erase_outer_triangles() + + points = cdt.get_all_vertices() + triangles = cdt.get_all_triangles() + + var st = SurfaceTool.new() + + st.begin(Mesh.PRIMITIVE_TRIANGLES) + + for i in range(points.size()): + st.add_vertex(Vector3(points[i].x, 0, points[i].y)) + + for i in range(triangles.size()): + st.add_index(triangles[i]) + + st.index() + st.generate_normals() + st.generate_tangents() + + var mesh = st.commit() + + return mesh - Store.house.save_local() diff --git a/content/system/house/room/room.tscn b/content/system/house/room/room.tscn index 7c13bfb..0c89780 100644 --- a/content/system/house/room/room.tscn +++ b/content/system/house/room/room.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://bswgmclohuqui"] +[gd_scene load_steps=10 format=3 uid="uid://bswgmclohuqui"] [ext_resource type="Script" path="res://content/system/house/room/room.gd" id="1_fccq0"] [ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_ugebq"] @@ -10,8 +10,6 @@ [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_08sv0"] -[sub_resource type="ArrayMesh" id="ArrayMesh_7dibq"] - [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ap613"] plane = Plane(0, -1, 0, 0) @@ -30,7 +28,6 @@ script = ExtResource("1_ugebq") [node name="WallMesh" type="MeshInstance3D" parent="."] material_override = ExtResource("3_al1ev") -mesh = SubResource("ArrayMesh_7dibq") [node name="CeilingMesh" type="MeshInstance3D" parent="."] material_override = ExtResource("3_al1ev") diff --git a/content/system/house/room/states/edit.gd b/content/system/house/room/states/edit.gd index 537ac90..bf05e46 100644 --- a/content/system/house/room/states/edit.gd +++ b/content/system/house/room/states/edit.gd @@ -11,13 +11,20 @@ var height_corner: StaticBody3D = null var height_edge: StaticBody3D = null func _on_enter(): - room.wall_corners.visible = true - room.wall_edges.visible = true + var room_store = Store.house.get_room(room.name) - if floor_corner != null: - floor_corner.visible = true - height_corner.visible = true - height_edge.visible = true + if room_store == null: + return + + var corners = room_store.corners + + if corners.size() > 0: + 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 + + for i in range(1, corners.size()): + add_corner(Vector3(corners[i].x, 0, corners[i].y)) room.room_ceiling.get_node("CollisionShape3D").disabled = (floor_corner == null && height_corner == null) room.room_floor.get_node("CollisionShape3D").disabled = false @@ -32,15 +39,21 @@ func _on_enter(): room.room_floor.get_node("Clickable").on_click.connect(_on_click_floor) func _on_leave(): - room.update_store() + update_store() - room.wall_corners.visible = false - room.wall_edges.visible = false + for child in room.wall_corners.get_children(): + child.queue_free() + await child.tree_exited + + for child in room.wall_edges.get_children(): + child.queue_free() + await child.tree_exited if floor_corner != null: - floor_corner.visible = false - height_corner.visible = false - height_edge.visible = false + floor_corner.queue_free() + await floor_corner.tree_exited + height_edge.queue_free() + await height_edge.tree_exited room.room_ceiling.get_node("CollisionShape3D").disabled = true room.room_floor.get_node("CollisionShape3D").disabled = true @@ -48,6 +61,16 @@ func _on_leave(): room.room_ceiling.get_node("Clickable").on_click.disconnect(_on_click_ceiling) room.room_floor.get_node("Clickable").on_click.disconnect(_on_click_floor) +func get_corner(index: int) -> MeshInstance3D: + return room.wall_corners.get_child(index % room.wall_corners.get_child_count()) + +func get_edge(index: int) -> MeshInstance3D: + return room.wall_edges.get_child(index % room.wall_edges.get_child_count()) + +func remove_corner(index: int): + get_corner(index).queue_free() + get_edge(index).queue_free() + func _on_click_floor(event): if floor_corner != null && height_corner != null: return @@ -237,4 +260,17 @@ func add_edge(from_pos: Vector3, to_pos: Vector3, index: int = -1): room.wall_edges.add_child(edge) room.wall_edges.move_child(edge, index) - return edge \ No newline at end of file + return edge + +func update_store(): + var store_room = Store.house.get_room(room.name) + + var corners = [] + + for corner in room.wall_corners.get_children(): + corners.append(Vector2(corner.position.x, corner.position.z)) + + store_room.corners = corners + store_room.height = room.room_ceiling.position.y + + Store.house.save_local() diff --git a/content/system/house/room/states/view.gd b/content/system/house/room/states/view.gd index 06e4715..441c3ac 100644 --- a/content/system/house/room/states/view.gd +++ b/content/system/house/room/states/view.gd @@ -2,20 +2,17 @@ extends RoomState const RoomState = preload("./room_state.gd") -var room_height = 3 -var corner_count = 0 func _on_enter(): - corner_count = room.wall_corners.get_child_count() + var room_store = Store.house.get_room(room.name) - if corner_count < 3: + if room_store == null || room_store.corners.size() < 3: return - room_height = room.room_ceiling.position.y - room.wall_mesh.visible = true room.ceiling_mesh.visible = true - room.wall_mesh.mesh = generate_mesh() + + room.wall_mesh.mesh = Room.generate_wall_mesh(room_store) if room.wall_mesh.mesh == null: return @@ -26,7 +23,7 @@ func _on_enter(): ceiling_shape.disabled = false floor_shape.disabled = false - room.ceiling_mesh.mesh = generate_ceiling_mesh() + room.ceiling_mesh.mesh = Room.generate_ceiling_mesh(room_store) 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 @@ -52,87 +49,28 @@ func _on_leave(): for collision in room.wall_collisions.get_children(): collision.queue_free() - -func generate_mesh(): - var st = SurfaceTool.new() - var wall_up = Vector3.UP * room_height - - st.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP) - - for i in range(corner_count): - var corner = room.get_corner(i) - - st.add_vertex(corner.position) - st.add_vertex(corner.position + wall_up) - - var first_corner = room.get_corner(0) - - st.add_vertex(first_corner.position) - st.add_vertex(first_corner.position + wall_up) - - st.index() - st.generate_normals() - st.generate_tangents() - var mesh = st.commit() - - return mesh - -func generate_ceiling_mesh(): - var points: PackedVector2Array = PackedVector2Array() - var edges: PackedInt32Array = PackedInt32Array() - var triangles: PackedInt32Array - - for i in range(corner_count): - var corner = room.get_corner(i) - points.append(Vector2(corner.position.x, corner.position.z)) - edges.append(i) - edges.append((i + 1) % corner_count) - - var cdt: ConstrainedTriangulation = ConstrainedTriangulation.new() - - cdt.init(true, true, 0.1) - - cdt.insert_vertices(points) - cdt.insert_edges(edges) - - cdt.erase_outer_triangles() - - points = cdt.get_all_vertices() - triangles = cdt.get_all_triangles() - - var st = SurfaceTool.new() - - st.begin(Mesh.PRIMITIVE_TRIANGLES) - - for i in range(points.size()): - st.add_vertex(Vector3(points[i].x, 0, points[i].y)) - - for i in range(triangles.size()): - st.add_index(triangles[i]) - - st.index() - st.generate_normals() - st.generate_tangents() - - var mesh = st.commit() - - return mesh + await collision.tree_exited func generate_collision(): + var room_store = Store.house.get_room(room.name) + var collision_shapes: Array[CollisionShape3D] = [] - for i in range(corner_count): - var corner = room.get_corner(i) - var next_corner = room.get_corner(i + 1) + 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) var shape = BoxShape3D.new() - shape.size = Vector3((next_corner.position - corner.position).length(), room_height, 0.04) + shape.size = Vector3((next_corner - corner).length(), room_store.height, 0.04) var transform = Transform3D() - var back_vector = (corner.position - next_corner.position).cross(Vector3.UP).normalized() * shape.size.z / 2 + var back_vector = (corner - next_corner).cross(Vector3.UP).normalized() * shape.size.z / 2 - transform.basis = Basis((next_corner.position - corner.position).normalized(), Vector3.UP, back_vector.normalized()) - transform.origin = corner.position + (next_corner.position - corner.position) / 2 + back_vector + Vector3.UP * shape.size.y / 2 + transform.basis = Basis((next_corner - corner).normalized(), Vector3.UP, back_vector.normalized()) + transform.origin = corner + (next_corner - corner) / 2 + back_vector + Vector3.UP * shape.size.y / 2 var collision_shape = CollisionShape3D.new() diff --git a/content/ui/menu/room/views/rooms.gd b/content/ui/menu/room/views/rooms.gd index 5efc268..d899936 100644 --- a/content/ui/menu/room/views/rooms.gd +++ b/content/ui/menu/room/views/rooms.gd @@ -41,6 +41,9 @@ var edit_room = false: room_button.label = "edit" func _ready(): + Store.house.on_loaded.connect(func(): + _generate_room_map() + ) room_button.on_button_down.connect(func(): if selected_room == null: @@ -50,6 +53,7 @@ func _ready(): return House.body.create_room(room_name, 0) + House.body.edit_room(room_name) selected_room = room_name edit_room = true else: @@ -84,26 +88,32 @@ func _on_click(event: EventPointer): selected_room = room_name func _generate_room_map(): - var rooms = House.body.get_rooms(0) + var rooms = Store.house.rooms - var target_size = Vector3(0.3, 1, 0.24) + var target_size = Vector2(0.3, 0.24) for old_room in rooms_map.get_children(): old_room.queue_free() await old_room.tree_exited - var aabb = House.body.get_level_aabb(0) - var current_min = aabb.position - var current_max = aabb.position + aabb.size + var current_min = Vector2(rooms[0].corners[0].x, rooms[0].corners[0].y) + var current_max = current_min for room in rooms: - var mesh = room.ceiling_mesh.mesh + for corner in room.corners: + current_min.x = min(current_min.x, corner.x) + current_min.y = min(current_min.y, corner.y) + current_max.x = max(current_max.x, corner.x) + current_max.y = max(current_max.y, corner.y) + + for room in rooms: + var mesh = RoomType.generate_ceiling_mesh(room) + if mesh == null: continue var body = StaticBody3D.new() body.name = room.name - var mesh_instance = MeshInstance3D.new() mesh_instance.name = "MeshInstance3D" @@ -122,9 +132,9 @@ func _generate_room_map(): var current_size = current_max - current_min var target_scale = target_size / current_size - var scale_value = min(target_scale.x, target_scale.z) + 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.z * scale_value + rooms_map.position.z = -current_min.y * scale_value rooms_map.scale = Vector3(scale_value, scale_value, scale_value) diff --git a/content/ui/menu/settings/settings_menu.gd b/content/ui/menu/settings/settings_menu.gd index e1e901a..9ddc2a0 100644 --- a/content/ui/menu/settings/settings_menu.gd +++ b/content/ui/menu/settings/settings_menu.gd @@ -27,15 +27,16 @@ func _ready(): ) credits.on_click.connect(func(_event): - print("_active_controller") var credits_instance = credits_scene.instantiate() get_tree().root.add_child(credits_instance) var label = $Content/Credits/Label credits_instance.global_position = + label.to_global(label.position + Vector3(0.1, 0, -0.15)) ) - input_url.text = Store.settings.url - input_token.text = Store.settings.token + Store.settings.on_loaded.connect(func(): + input_url.text = Store.settings.url + input_token.text = Store.settings.token + ) button_connect.on_button_down.connect(func(): var url = input_url.text diff --git a/lib/globals/home_api.gd b/lib/globals/home_api.gd index 95f4dde..c922e5a 100644 --- a/lib/globals/home_api.gd +++ b/lib/globals/home_api.gd @@ -27,7 +27,7 @@ func _ready(): var success = Store.settings.load_local() if success: - start_adapter(Store.settings.type, Store.settings.url, Store.settings.token) + start_adapter(Store.settings.type.to_lower(), Store.settings.url, Store.settings.token) func start_adapter(type: String, url: String, token: String): @@ -93,4 +93,4 @@ func watch_state(entity: String, callback: Callable): func _notification(what): if what == NOTIFICATION_WM_CLOSE_REQUEST || what == NOTIFICATION_WM_GO_BACK_REQUEST: # Store.house.save_local() - pass \ No newline at end of file + pass diff --git a/lib/stores/store.gd b/lib/stores/store.gd index 505bfd7..2a68ba8 100644 --- a/lib/stores/store.gd +++ b/lib/stores/store.gd @@ -32,7 +32,7 @@ func create_dict(): func use_dict(dict: Dictionary): for prop_info in get_property_list(): - if prop_info.name.begins_with("_"): + if prop_info.name.begins_with("_") || prop_info.hint_string != "": continue var prop = get(prop_info.name) @@ -57,8 +57,7 @@ func save_local(path = _save_path): var json_text = JSON.stringify(data) save_file.store_line(json_text) - _loaded = true - on_loaded.emit() + on_saved.emit() return true @@ -71,7 +70,15 @@ func load_local(path = _save_path): if save_file == null: return false - var json_text = save_file.get_line() + var json_text = save_file.get_as_text() var save_data = VariantSerializer.parse_value(JSON.parse_string(json_text)) + if save_data == null: + return false + use_dict(save_data) + + _loaded = true + on_loaded.emit() + + return true