Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add esc_current_scene reserved global #474

Merged
merged 3 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ func run(command_params: Array) -> int:
# exit_scene event. Also room selector actions require the transition.
if command_params[1] \
and (
not escoria.globals_manager.get_global("ESC_LAST_SCENE").empty()
not escoria.globals_manager.get_global( \
escoria.room_manager.GLOBAL_LAST_SCENE).empty()
or (
escoria.event_manager.get_running_event("_front") != null \
and escoria.event_manager.get_running_event("_front").name \
in ["newgame", "exit_scene", "room_selector"]
and escoria.globals_manager.get_global(
"ESC_LAST_SCENE"
escoria.room_manager.GLOBAL_LAST_SCENE
).empty()
)
):
Expand All @@ -91,16 +92,18 @@ func run(command_params: Array) -> int:
escoria.game_scene.unpause_game()

# If FORCE_LAST_SCENE_NULL is true, force ESC_LAST_SCENE to empty
if escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL"):
if escoria.globals_manager.get_global( \
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL):

escoria.globals_manager.set_global(
"ESC_LAST_SCENE",
escoria.room_manager.GLOBAL_LAST_SCENE,
null,
true
)
elif escoria.main.current_scene:
# If FORCE_LAST_SCENE_NULL is false, set ESC_LAST_SCENE = current roomid
escoria.globals_manager.set_global(
"ESC_LAST_SCENE",
escoria.room_manager.GLOBAL_LAST_SCENE,
escoria.main.current_scene.global_id,
true
)
Expand Down Expand Up @@ -150,6 +153,14 @@ func run(command_params: Array) -> int:
room_scene.game = escoria.game_scene
escoria.main.set_scene(room_scene)

# We know the scene has been loaded. Make its global ID available for
# use by ESC script.
escoria.globals_manager.set_global(
escoria.room_manager.GLOBAL_CURRENT_SCENE,
room_scene.global_id,
true
)

# Clear queued resources
escoria.resource_cache.clear()

Expand Down
41 changes: 24 additions & 17 deletions addons/escoria-core/game/core-scripts/esc/esc_globals_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,16 @@ extends Resource
class_name ESCGlobalsManager


const GLOBAL_ANIMATION_RESOURCES = "ANIMATION_RESOURCES"


# Emitted when a global is changed
signal global_changed(global, old_value, new_value)


# A list of reserved globals which can not be overridden
const RESERVED_GLOBALS = [
"ESC_LAST_SCENE", # Contains the global_id of previous room
"FORCE_LAST_SCENE_NULL", # If true, ESC_LAST_SCENE is not considered for
# automatic transitions
"ANIMATION_RESOURCES"
]


# The globals registry
export(Dictionary) var _globals = {}



func _init():
set_global("ESC_LAST_SCENE", "", true)
set_global("FORCE_LAST_SCENE_NULL", false, true)
# Registry of globals that are to be reserved for internal use only.
var _reserved_globals: Dictionary = {}
dploeger marked this conversation as resolved.
Show resolved Hide resolved


# Check if a global was registered
Expand All @@ -41,6 +27,27 @@ func has(key: String) -> bool:
return _globals.has(key)


# Registers a global as being reserved and initializes it.
#
# #### Parameters
#
# - key: The key of the global to register
# - value: The initial value (optional)
func register_reserved_global(key: String, value = null) -> void:
if key in _reserved_globals:
escoria.logger.report_errors(
"ESCGlobalsManager.register_reserved_global: Can not override reserved global",
[
"Global key %s is already registered as reserved" % key
]
)
_reserved_globals[key] = value
_globals[key] = value

if value != null:
emit_signal("global_changed", key, _globals[key], value)


# Get the current value of a global
#
# #### Parameters
Expand Down Expand Up @@ -77,7 +84,7 @@ func filter(pattern: String) -> Dictionary:
# - key: The key of the global to modify
# - value: The new value
func set_global(key: String, value, ignore_reserved: bool = false) -> void:
if key in RESERVED_GLOBALS and not ignore_reserved:
if key in _reserved_globals and not ignore_reserved:
escoria.logger.report_errors(
"ESCGlobalsManager.set_global: Can not override reserved global",
[
Expand Down
32 changes: 32 additions & 0 deletions addons/escoria-core/game/core-scripts/esc/esc_room_manager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extends Object
class_name ESCRoomManager
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not so fond of the name of this class: ESCRoomManager seems to imply this class manages rooms, but this is not true as it's just an enum entries holder. Would ESCReservedGlobals fit instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my idea was that this manager would also change the room for example. Also, why's the GLOBAL_ANIMATION_RESOURCES here? That doesn't have to do anything with rooms if I'm not mistaken.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. You saw it in ESCRoom and put it here. It's actually the global cache of the current animation resource of the player, so it doesn't really fit here. Hm... maybe ESCPlayer? I'm not 100% sure.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my idea was that this manager would also change the room for example

Can you give more precisions on this?

Copy link
Collaborator Author

@BHSDuncan BHSDuncan Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. You saw it in ESCRoom and put it here. It's actually the global cache of the current animation resource of the player, so it doesn't really fit here. Hm... maybe ESCPlayer? I'm not 100% sure.

Yeah, you got it. It's also only ever used in ESCRoom, it seems. But I'm happy to put it wherever you deem best.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it also used in the set_animations command? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, good catch. I didn't see it because it's actually referenced by a one-off constant in globals_manager, so I'll need to fix that anyway.

But like I said: I'll put the definition in whichever class you prefer!



# Reserved globals which can not be overridden; prefixed with "GLOBAL_"
#
# Contains the global_id of previous room
const GLOBAL_LAST_SCENE = "ESC_LAST_SCENE"

# If true, ESC_LAST_SCENE is not considered for automatic transitions
const GLOBAL_FORCE_LAST_SCENE_NULL = "FORCE_LAST_SCENE_NULL"

const GLOBAL_ANIMATION_RESOURCES = "ANIMATION_RESOURCES"

# Contains the global_id of the current room
const GLOBAL_CURRENT_SCENE = "ESC_CURRENT_SCENE"

# Dict of the reserved globals to register and their initial values.
const RESERVED_GLOBALS = {
GLOBAL_LAST_SCENE: "",
GLOBAL_FORCE_LAST_SCENE_NULL: false,
GLOBAL_ANIMATION_RESOURCES: [],
GLOBAL_CURRENT_SCENE: ""
}


# Registers all reserved global flags for use.
func register_reserved_globals() -> void:
for key in RESERVED_GLOBALS:
escoria.globals_manager.register_reserved_global( \
key,
RESERVED_GLOBALS[key])
23 changes: 17 additions & 6 deletions addons/escoria-core/game/core-scripts/esc_room.gd
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func _ready():
true
)
if escoria.globals_manager.has(
escoria.globals_manager.GLOBAL_ANIMATION_RESOURCES
escoria.room_manager.GLOBAL_ANIMATION_RESOURCES
):
var animations = escoria.globals_manager.get_global(
escoria.globals_manager.GLOBAL_ANIMATION_RESOURCES
escoria.room_manager.GLOBAL_ANIMATION_RESOURCES
)

if player.global_id in animations and \
Expand Down Expand Up @@ -168,7 +168,8 @@ func perform_script_events():
if enabled_automatic_transitions \
or (
not enabled_automatic_transitions \
and escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL")
and escoria.globals_manager.get_global( \
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL)
):
var script_transition_in = escoria.esc_compiler.compile([
":transition_in",
Expand All @@ -195,18 +196,28 @@ func perform_script_events():

# Now that :ready is finished, if FORCE_LAST_SCENE_NULL was true, reset it
# to false
if escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL"):
if escoria.globals_manager.get_global( \
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL):

escoria.globals_manager.set_global(
"FORCE_LAST_SCENE_NULL",
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL,
false,
true
)
escoria.globals_manager.set_global(
"ESC_LAST_SCENE",
escoria.room_manager.GLOBAL_LAST_SCENE,
escoria.main.current_scene.global_id \
if escoria.main.current_scene != null else "",
true
)

# Make the room's global ID available for use in ESC script.
escoria.globals_manager.set_global(
escoria.room_manager.GLOBAL_CURRENT_SCENE,
escoria.main.current_scene.global_id \
if escoria.main.current_scene != null else "",
true
)


# Runs the script event from the script attached, if any.
Expand Down
5 changes: 5 additions & 0 deletions addons/escoria-core/game/escoria.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var event_manager: ESCEventManager
# ESC globals registry instance
var globals_manager: ESCGlobalsManager

# ESC room manager instance
var room_manager: ESCRoomManager

# ESC object manager instance
var object_manager: ESCObjectManager

Expand Down Expand Up @@ -107,6 +110,7 @@ func _init():
self.save_manager = ESCSaveManager.new()
self.inputs_manager = ESCInputsManager.new()
self.controller = ESCController.new()
self.room_manager = ESCRoomManager.new()

settings = ESCSaveSettings.new()

Expand All @@ -124,6 +128,7 @@ func _init():
func _ready():
settings = save_manager.load_settings()
apply_settings(settings)
room_manager.register_reserved_globals()
StraToN marked this conversation as resolved.
Show resolved Hide resolved
inputs_manager.register_core()
if ProjectSettings.get_setting("escoria/main/game_start_script").empty():
logger.report_errors("escoria.gd",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func _on_button_pressed():
# automatic transitions.
# If FORCE_LAST_SCENE_NULL is True when change_scene starts:
# - ESC_LAST_SCENE is set to empty
escoria.globals_manager.set_global("FORCE_LAST_SCENE_NULL", true, true)
escoria.globals_manager.set_global( \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd better format that like

escoria.globals_manager.set_global(
  escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL,
  true,
  true
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you don't need the \ in the first line. Also, please put the closing bracket into the next line all to the left aligned with the command itself.

escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL,
true,
true)

var script = escoria.esc_compiler.compile([
":room_selector",
"change_scene %s" % _options_paths[_selected_id]
Expand Down
3 changes: 3 additions & 0 deletions game/rooms/room15/esc/right_exit.esc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:exit_scene
play_snd res://game/sfx/sounds/doorOpen_2.ogg
change_scene "res://game/rooms/room16/room16.tscn"
13 changes: 9 additions & 4 deletions game/rooms/room15/room15.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=2]
[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]
Expand All @@ -8,7 +8,6 @@
[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/items/escitems/button.tscn" type="PackedScene" id=9]
[ext_resource path="res://addons/escoria-core/game/core-scripts/esc_exit.gd" type="Script" id=10]

[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 )
Expand Down Expand Up @@ -81,9 +80,15 @@ script = ExtResource( 5 )
global_id = "r12_l_exit"

[node name="r_door" type="Area2D" parent="Hotspots"]
script = ExtResource( 10 )
pause_mode = 1
script = ExtResource( 7 )
global_id = "r15_r_exit"
esc_script = "res://game/rooms/room15/esc/right_exit.esc"
is_exit = true
tooltip_name = "Right exit"
switch_sound = "res://game/sfx/sounds/doorOpen_2.ogg"
default_action = "walk"
dialog_color = Color( 1, 1, 1, 1 )
animations = null

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Hotspots/r_door"]
position = Vector2( 0, -1 )
Expand Down
30 changes: 30 additions & 0 deletions game/rooms/room16/background.tscn
Original file line number Diff line number Diff line change
@@ -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_": ""
}
11 changes: 11 additions & 0 deletions game/rooms/room16/esc/current_scene_button.esc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
:use

> [eq ESC_CURRENT_SCENE room16]
say player "This sure looks like room 16."

> [eq ESC_CURRENT_SCENE ESC_LAST_SCENE]
say player "I'll never say this."

> [eq ESC_CURRENT_SCENE room1]
say player "I'll never say this, either."

3 changes: 3 additions & 0 deletions game/rooms/room16/esc/left_exit.esc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:exit_scene
play_snd res://game/sfx/sounds/doorOpen_2.ogg
change_scene "res://game/rooms/room15/room15.tscn"
8 changes: 8 additions & 0 deletions game/rooms/room16/esc/room16.esc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:setup

> [eq ESC_LAST_SCENE room15]
teleport player r16_l_exit
# Set player look right
set_angle player 90
stop

Loading