From 4b2b6f516e01e9e84fc99db5aa579d78d6dadc49 Mon Sep 17 00:00:00 2001 From: Julian Murgia Date: Sat, 23 Apr 2022 18:55:21 +0200 Subject: [PATCH] feat: Save objects states in Obj Manager to keep them btwn rooms (#554) --- .../core-scripts/esc/esc_object_manager.gd | 34 +++++++++++++++---- .../game/core-scripts/esc/types/esc_object.gd | 6 +++- .../game/scenes/sound/esc_music_player.gd | 1 + .../game/scenes/sound/esc_speech_player.gd | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd b/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd index be635a4e..44b986e9 100644 --- a/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd +++ b/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd @@ -163,8 +163,12 @@ func register_object(object: ESCObject, room: ESCRoom = null, force: bool = fals ] ] ) - return + # Object exists in room, set it to is last state (if different from + # "default") + elif _object_exists_in_room(object, room_key): + # Object is already known, set its state to last known state + object.set_state(get_object(object.global_id).state) # If the object is already connected, disconnect it for the case of # forcing the registration, since we don't know if this object will be @@ -205,6 +209,23 @@ func register_object(object: ESCObject, room: ESCRoom = null, force: bool = fals var objects: Dictionary = _get_room_objects_objects(room_key) objects[object.global_id] = object + + # If object state is not STATE_DEFAULT, save it in manager's object states + if object.state != ESCObject.STATE_DEFAULT: + if get_object(object.global_id) == null: + escoria.logger.report_errors( + "ESCObjectManager:register_object()", + [ + "Object with global id %s in room (%s, %s) not found in Object Manager" % + [ + object.global_id, + room_key.room_global_id, + room_key.room_instance_id + ] + ] + ) + else: + get_object(object.global_id).state = object.state # If this is the first object for the room, that means we have a brand new # room and it needs to be setup and tracked. @@ -346,8 +367,9 @@ func unregister_object(object: ESCObject, room_key: ESCRoomObjectsKey) -> void: if object.node != null: object.node = object.node.duplicate() register_object(object, null, true) - - room_objects.erase(object.global_id) + + if object.state == ESCObject.STATE_DEFAULT: + room_objects.erase(object.global_id) # If this room is truly empty, it's time to do away with it. if room_objects.size() == 0: @@ -438,8 +460,7 @@ func _is_current_room(container: ESCRoomObjects) -> bool: # **Returns** True iff container represents the the object manager entry specified # by room_key. func _compare_container_to_key(container: ESCRoomObjects, room_key: ESCRoomObjectsKey) -> bool: - return container.room_global_id == room_key.room_global_id \ - and container.room_instance_id == room_key.room_instance_id + return container.room_global_id == room_key.room_global_id # Checks whether an entry in the object manager array corresponds to the passed in @@ -477,8 +498,7 @@ func _object_exists_in_room(object: ESCObject, room_key: ESCRoomObjectsKey) -> b for room_container in room_objects: if _compare_container_to_key(room_container, room_key) \ - and room_container.objects.has(object.global_id): - + and room_container.objects.has(object.global_id): return true return false diff --git a/addons/escoria-core/game/core-scripts/esc/types/esc_object.gd b/addons/escoria-core/game/core-scripts/esc/types/esc_object.gd index d9e040bc..28ccd31c 100644 --- a/addons/escoria-core/game/core-scripts/esc/types/esc_object.gd +++ b/addons/escoria-core/game/core-scripts/esc/types/esc_object.gd @@ -3,6 +3,10 @@ extends Node class_name ESCObject +# Default object state +const STATE_DEFAULT: String = "default" + + # The global id of the object var global_id: String @@ -14,7 +18,7 @@ var interactive: bool = true setget _set_interactive, _get_interactive # The state of the object. If the object has a respective animation, # it will be played -var state: String = "default" +var state: String = STATE_DEFAULT # The events registered with the object var events: Dictionary = {} diff --git a/addons/escoria-core/game/scenes/sound/esc_music_player.gd b/addons/escoria-core/game/scenes/sound/esc_music_player.gd index 9b01be86..95f66cb1 100644 --- a/addons/escoria-core/game/scenes/sound/esc_music_player.gd +++ b/addons/escoria-core/game/scenes/sound/esc_music_player.gd @@ -56,6 +56,7 @@ func _ready(): pause_mode = Node.PAUSE_MODE_STOP escoria.object_manager.register_object( ESCObject.new(global_id, self), + null, true ) diff --git a/addons/escoria-core/game/scenes/sound/esc_speech_player.gd b/addons/escoria-core/game/scenes/sound/esc_speech_player.gd index e0e1a0df..ff1fc622 100644 --- a/addons/escoria-core/game/scenes/sound/esc_speech_player.gd +++ b/addons/escoria-core/game/scenes/sound/esc_speech_player.gd @@ -37,6 +37,7 @@ func _ready(): pause_mode = Node.PAUSE_MODE_STOP escoria.object_manager.register_object( ESCObject.new(global_id, self), + null, true )