From bbe885edc2aab0fd18c98ef94070c0c7bc671e28 Mon Sep 17 00:00:00 2001 From: Julian Murgia Date: Fri, 12 Nov 2021 22:57:44 +0100 Subject: [PATCH] Ensure player is set at start location if there is one (#435) This is done even if there is no ESC script attached to the room. First default position will then be the start ESCLocation if there is one. Else, fallback to origin (0,0). --- .../core-scripts/esc/esc_object_manager.gd | 4 +- .../game/core-scripts/esc_room.gd | 20 ++-- addons/escoria-core/game/main.gd | 9 +- game/rooms/room14/esc/right_exit.esc | 4 +- game/rooms/room15/background.tscn | 30 ++++++ game/rooms/room15/esc/left_exit.esc | 5 + game/rooms/room15/r_door.tscn | 25 +++++ game/rooms/room15/room15.tscn | 96 +++++++++++++++++++ game/rooms/room15/walkable_area.tscn | 18 ++++ 9 files changed, 192 insertions(+), 19 deletions(-) create mode 100644 game/rooms/room15/background.tscn create mode 100644 game/rooms/room15/esc/left_exit.esc create mode 100644 game/rooms/room15/r_door.tscn create mode 100644 game/rooms/room15/room15.tscn create mode 100644 game/rooms/room15/walkable_area.tscn 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 cb366cdc..50be5906 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 @@ -124,7 +124,9 @@ func save_game(p_savegame: ESCSaveGame) -> void: func get_start_location() -> ESCLocation: for object in objects.values(): - if object.node is ESCLocation and object.node.is_start_location: + if is_instance_valid(object.node) \ + and object.node is ESCLocation \ + and object.node.is_start_location: return object escoria.logger.report_warnings( "esc_object_manager.gd:get_start_location()", diff --git a/addons/escoria-core/game/core-scripts/esc_room.gd b/addons/escoria-core/game/core-scripts/esc_room.gd index e007cf2c..da73e9b4 100644 --- a/addons/escoria-core/game/core-scripts/esc_room.gd +++ b/addons/escoria-core/game/core-scripts/esc_room.gd @@ -82,9 +82,6 @@ func _ready(): add_child(game) move_child(game, 0) - if escoria.main.current_scene == null: - escoria.main.set_scene(self) - if player_scene: player = player_scene.instance() add_child(player) @@ -113,22 +110,21 @@ func _ready(): else: is_run_directly = true + # Manage player location at room start + if player != null \ + and escoria.object_manager.get_start_location() != null: + player.teleport(escoria.object_manager.get_start_location().node) + perform_script_events() +# Performs the ESC script events "setup" and "ready", in this order, if they are +# present. Also manages automatic transitions. func perform_script_events(): if esc_script and escoria.event_manager._running_event == null \ or (escoria.event_manager._running_event != null \ and escoria.event_manager._running_event.name != "load"): - # Manage player location at room start - if (escoria.globals_manager.get_global("ESC_LAST_SCENE") == null \ - or escoria.globals_manager \ - .get_global("ESC_LAST_SCENE").empty()) \ - and player != null \ - and escoria.object_manager.get_start_location() != null: - player.teleport(escoria.object_manager.get_start_location().node) - # If the room was loaded from change_scene and automatic transitions # are not disabled, do the transition out now if enabled_automatic_transitions \ @@ -145,7 +141,6 @@ func perform_script_events(): script_transition_out.events['transition_out'] ) - # Run the setup event _run_script_event("setup") @@ -194,6 +189,7 @@ func perform_script_events(): true ) + # Runs the script event from the script attached, if any. # # #### Parameters diff --git a/addons/escoria-core/game/main.gd b/addons/escoria-core/game/main.gd index 5e40b138..d04ce7a2 100644 --- a/addons/escoria-core/game/main.gd +++ b/addons/escoria-core/game/main.gd @@ -40,10 +40,11 @@ func set_scene(p_scene: Node) -> void: if current_scene != null: clear_scene() - if not p_scene.is_inside_tree() or not p_scene in get_children(): - add_child(p_scene) - elif p_scene in get_children(): - move_child(p_scene, 0) + if p_scene.is_inside_tree() and not p_scene.get_parent() != self: + p_scene.get_parent().remove_child(p_scene) + + add_child(p_scene) + move_child(p_scene, 0) current_scene = p_scene check_game_scene_methods() diff --git a/game/rooms/room14/esc/right_exit.esc b/game/rooms/room14/esc/right_exit.esc index a73b2437..a347e3af 100644 --- a/game/rooms/room14/esc/right_exit.esc +++ b/game/rooms/room14/esc/right_exit.esc @@ -1,3 +1,3 @@ :exit_scene -#set_sound_state _sound res://game/sfx/sounds/doorOpen_2.ogg false -#change_scene "res://game/rooms/room15/room15.tscn" +set_sound_state _sound res://game/sfx/sounds/doorOpen_2.ogg false +change_scene "res://game/rooms/room15/room15.tscn" diff --git a/game/rooms/room15/background.tscn b/game/rooms/room15/background.tscn new file mode 100644 index 00000000..fa42fc10 --- /dev/null +++ b/game/rooms/room15/background.tscn @@ -0,0 +1,30 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_background.gd" type="Script" id=1] + +[node name="background" type="TextureRect"] +margin_right = 1289.0 +margin_bottom = 555.0 +mouse_filter = 2 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="l_platform" type="Line2D" parent="."] +position = Vector2( 2, -266 ) +points = PoolVector2Array( -2.96298, 712.01, 129.973, 614.429, 1167.5, 612.894, 1274.59, 669.705, 1273.25, 812.694, 2.36697, 811.043, 2.36697, 713.389 ) + +[node name="l_door" type="Line2D" parent="."] +position = Vector2( 0, -266 ) +points = PoolVector2Array( 6.61201, 704.409, 6.61203, 389.558, 87.755, 339.775, 87.5463, 649.784 ) +__meta__ = { +"_editor_description_": "" +} + +[node name="r_door" type="Line2D" parent="."] +position = Vector2( 0, -267.828 ) +points = PoolVector2Array( 1175.07, 620.086, 1171.24, 311.267, 1274.8, 356.87, 1278.31, 672.412, 1188.64, 624.843 ) +__meta__ = { +"_editor_description_": "" +} diff --git a/game/rooms/room15/esc/left_exit.esc b/game/rooms/room15/esc/left_exit.esc new file mode 100644 index 00000000..c1fcf214 --- /dev/null +++ b/game/rooms/room15/esc/left_exit.esc @@ -0,0 +1,5 @@ +:exit_scene +set_sound_state _sound res://game/sfx/sounds/doorOpen_2.ogg false +change_scene "res://game/rooms/room14/room14.tscn" + + diff --git a/game/rooms/room15/r_door.tscn b/game/rooms/room15/r_door.tscn new file mode 100644 index 00000000..39b4fa38 --- /dev/null +++ b/game/rooms/room15/r_door.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_item.gd" type="Script" id=1] +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_location.gd" type="Script" id=2] + +[node name="r_door" type="Area2D"] +pause_mode = 1 +script = ExtResource( 1 ) +__meta__ = { +"_editor_description_": "" +} +global_id = "r1_r_exit" +esc_script = "res://game/rooms/room01/esc/right_exit.esc" +is_exit = true +tooltip_name = "Exit" +default_action = "walk" +dialog_color = Color( 1, 1, 1, 1 ) +animations = null + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] +polygon = PoolVector2Array( 1177.94, 348.61, 1175.95, 45.3759, 1276.06, 92.0953, 1277.95, 399.407 ) + +[node name="Position2D" type="Position2D" parent="."] +position = Vector2( 1225.47, 353.99 ) +script = ExtResource( 2 ) diff --git a/game/rooms/room15/room15.tscn b/game/rooms/room15/room15.tscn new file mode 100644 index 00000000..57e86aab --- /dev/null +++ b/game/rooms/room15/room15.tscn @@ -0,0 +1,96 @@ +[gd_scene load_steps=10 format=2] + +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_terrain.gd" type="Script" id=1] +[ext_resource path="res://game/rooms/room15/background.tscn" type="PackedScene" id=2] +[ext_resource path="res://game/fonts/caslonantique.tres" type="DynamicFont" id=3] +[ext_resource path="res://game/characters/mark/mark.tscn" type="PackedScene" id=4] +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_location.gd" type="Script" id=5] +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_room.gd" type="Script" id=6] +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_item.gd" type="Script" id=7] +[ext_resource path="res://game/rooms/room15/r_door.tscn" type="PackedScene" id=8] + +[sub_resource type="NavigationPolygon" id=1] +vertices = PoolVector2Array( 1168.92, 640.557, 1182.53, 588.863, 1269.59, 622.872, 1275.03, 799.721, 864.626, 613.518, 1143.08, 613.35, -9.16094, 803.802, 386.666, 618.012, 129.634, 615.792, 84.5821, 654.06, -6.44019, 711.297, 3.15687, 646.051, 59.2201, 628.698 ) +polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 4, 5, 0, 3, 6, 7 ), PoolIntArray( 8, 7, 6, 9 ), PoolIntArray( 9, 6, 10, 11, 12 ) ] +outlines = [ PoolVector2Array( -6.44019, 711.297, 3.15687, 646.051, 59.2201, 628.698, 84.5821, 654.06, 129.634, 615.792, 386.666, 618.012, 864.626, 613.518, 1143.08, 613.35, 1168.92, 640.557, 1182.53, 588.863, 1269.59, 622.872, 1275.03, 799.721, -9.16094, 803.802 ) ] + +[node name="room15" type="Node2D"] +script = ExtResource( 6 ) +__meta__ = { +"_edit_vertical_guides_": [ ] +} +global_id = "room15" +player_scene = ExtResource( 4 ) +camera_limits = [ Rect2( 0, 0, 1289, 555 ) ] + +[node name="background" parent="." instance=ExtResource( 2 )] + +[node name="room_label" type="Label" parent="background"] +margin_right = 92.0 +margin_bottom = 21.0 +custom_fonts/font = ExtResource( 3 ) +text = "ROOM 15" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="description" type="Label" parent="background"] +margin_left = 198.0 +margin_top = 97.0 +margin_right = 681.0 +margin_bottom = 142.0 +custom_fonts/font = ExtResource( 3 ) +text = "This room has no ESC Script attached so the player will spawn at +starting location if there is one. + +If there is none, the player will spawn at origin (0,0), top-left corner of the screen. " +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="walkable_area" type="Navigation2D" parent="."] +script = ExtResource( 1 ) + +[node name="platform" type="NavigationPolygonInstance" parent="walkable_area"] +position = Vector2( 6.73163, -264.779 ) +navpoly = SubResource( 1 ) +__meta__ = { +"_editor_description_": "" +} + +[node name="Hotspots" type="Node" parent="."] + +[node name="l_door" type="Area2D" parent="Hotspots"] +pause_mode = 1 +script = ExtResource( 7 ) +global_id = "r14_l_exit" +esc_script = "res://game/rooms/room15/esc/left_exit.esc" +is_exit = true +tooltip_name = "Left exit" +default_action = "use" +dialog_color = Color( 1, 1, 1, 1 ) +animations = null + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Hotspots/l_door"] +polygon = PoolVector2Array( 0.328762, 440.897, 1.85199, 119.926, 85.9517, 74.6212, 87.1409, 377.869 ) + +[node name="Position2D" type="Position2D" parent="Hotspots/l_door"] +position = Vector2( 37.4521, 392.045 ) +script = ExtResource( 5 ) +global_id = "r12_l_exit" + +[node name="r_door" parent="Hotspots" instance=ExtResource( 8 )] +global_id = "r14_r_exit" +esc_script = "res://game/rooms/room15/esc/right_exit.esc" +default_action = "use" + +[node name="ESCLocation" type="Position2D" parent="Hotspots/r_door"] +position = Vector2( 1231.78, 360.624 ) +script = ExtResource( 5 ) + +[node name="start" type="Position2D" parent="Hotspots"] +position = Vector2( 243.677, 455.569 ) +script = ExtResource( 5 ) +global_id = "start" +is_start_location = true +interaction_direction = 180 diff --git a/game/rooms/room15/walkable_area.tscn b/game/rooms/room15/walkable_area.tscn new file mode 100644 index 00000000..2d6b73ec --- /dev/null +++ b/game/rooms/room15/walkable_area.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_terrain.gd" type="Script" id=1] + +[sub_resource type="NavigationPolygon" id=1] +vertices = PoolVector2Array( 1168.92, 640.557, 1182.53, 588.863, 1269.59, 622.872, 1275.03, 799.721, 129.634, 615.792, 1143.08, 613.35, -9.16094, 803.802, 84.5821, 654.06, -6.44019, 711.297, 3.15687, 646.051, 59.2201, 628.698 ) +polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 4, 5, 0, 3, 6, 7 ), PoolIntArray( 7, 6, 8, 9, 10 ) ] +outlines = [ PoolVector2Array( -6.44019, 711.297, 3.15687, 646.051, 59.2201, 628.698, 84.5821, 654.06, 129.634, 615.792, 1143.08, 613.35, 1168.92, 640.557, 1182.53, 588.863, 1269.59, 622.872, 1275.03, 799.721, -9.16094, 803.802 ) ] + +[node name="walkable_area" type="Navigation2D"] +script = ExtResource( 1 ) + +[node name="platform" type="NavigationPolygonInstance" parent="."] +position = Vector2( 6.73163, -264.779 ) +navpoly = SubResource( 1 ) +__meta__ = { +"_editor_description_": "" +}