Adds "name" parameter to ESC spawn command (#410)

Co-authored-by: StraToN <StraToN@users.noreply.github.com>
This commit is contained in:
Julian Murgia
2021-10-07 08:22:11 +02:00
committed by GitHub
parent 378f29283d
commit 41902b5791
11 changed files with 128 additions and 90 deletions

View File

@@ -71,8 +71,6 @@ func run(command_params: Array) -> int:
escoria.inputs_manager.clear_stack()
escoria.main_menu_instance.hide()
var res_room = escoria.resource_cache.get_resource(command_params[0])
# Load game scene

View File

@@ -1,4 +1,4 @@
# `spawn path [object2]`
# `spawn identifier path [is_active=true] [object2] `
#
# Instances a scene determined by "path", and places in the position of
# object2 (object2 is optional)
@@ -11,50 +11,71 @@ class_name SpawnCommand
# Return the descriptor of the arguments of this command
func configure() -> ESCCommandArgumentDescriptor:
return ESCCommandArgumentDescriptor.new(
1,
[TYPE_STRING, TYPE_STRING],
[null, null]
2,
[TYPE_STRING, TYPE_STRING, TYPE_BOOL, TYPE_STRING],
[null, null, true, null]
)
# Validate wether the given arguments match the command descriptor
func validate(arguments: Array):
if not ResourceLoader.exists(arguments[0]):
if arguments[0].empty() \
or arguments[0] in escoria.object_manager.RESERVED_OBJECTS:
escoria.logger.report_errors(
"spawn: invalid global_id",
[
"global_id %s is invalid" % arguments[0]
]
)
return false
if not ResourceLoader.exists(arguments[1]):
escoria.logger.report_errors(
"spawn: invalid scene path",
[
"Scene with path %s not found" % arguments[0]
"Scene with path %s not found" % arguments[1]
]
)
return false
if arguments[1] and not escoria.object_manager.objects.has(arguments[1]):
return false
if arguments[3] and not escoria.object_manager.objects.has(arguments[2]):
escoria.logger.report_errors(
"spawn: invalid object",
[
"Object with global id %s not found" % arguments[1]
"Object with global id %s not found" % arguments[2]
]
)
return false
return false
return .validate(arguments)
# Run the command
func run(command_params: Array) -> int:
var res_scene = escoria.resource_cache.get_resource(command_params[0])
var res_scene = escoria.resource_cache.get_resource(command_params[1])
# Load room scene
var scene = res_scene.instance()
if scene:
escoria.main.get_node("/root").add_child(scene)
if command_params[1]:
var obj = escoria.object_manager.get_object(command_params[1])
if command_params[3]:
var obj = escoria.object_manager.get_object(command_params[3])
scene.set_position(obj.get_global_position())
escoria.inputs_manager.hotspot_focused = ""
escoria.object_manager.register_object(
ESCObject.new(
command_params[0],
scene
),
true
)
escoria.object_manager.get_object(command_params[0]).active = \
command_params[2]
else:
escoria.logger.report_errors(
"spawn: Invalid scene",
[
"Failed loading scene %s" % command_params[0]
"Failed loading scene %s" % command_params[1]
]
)

View File

@@ -49,9 +49,6 @@ var command_registry: ESCCommandRegistry
# Resource cache handler
var resource_cache: ESCResourceCache
# Instance of the main menu
var main_menu_instance
# Terrain of the current room
var room_terrain
@@ -64,7 +61,6 @@ var inventory
# These are settings that the player can affect and save/load later
var settings: ESCSaveSettings
# The current state of the game
onready var current_state = GAME_STATE.DEFAULT
@@ -87,6 +83,11 @@ var controller: ESCController
# The game scene loaded
var game_scene: ESCGame
# The compiled start script loaded from ProjectSettings
# escoria/main/game_start_script
var start_script : ESCScript
# Initialize various objects
func _init():
@@ -119,38 +120,25 @@ func _init():
ProjectSettings.get_setting("escoria/ui/game_scene")
).instance()
if ProjectSettings.get_setting("escoria/ui/main_menu_scene") == "":
logger.report_errors("escoria.gd",
["Parameter escoria/ui/main_menu_scene is not set!"]
)
else:
self.main_menu_instance = resource_cache.get_resource(
ProjectSettings.get_setting("escoria/ui/main_menu_scene")
).instance()
# Load settings
func _ready():
inputs_manager.register_core()
start_script = self.esc_compiler.load_esc_file(
ProjectSettings.get_setting("escoria/main/game_start_script")
)
# Called by Escoria's main_scene as very very first event EVER.
# Usually you'll want to show some logos animations before spawning the main
# menu in the escoria/main/game_start_script 's :init event
func init():
run_event_from_script(start_script, "init")
# Called by Main menu "start new game"
func new_game():
var script = self.esc_compiler.load_esc_file(
ProjectSettings.get_setting("escoria/main/game_start_script")
)
event_manager.queue_event(script.events["start"])
var rc = yield(event_manager, "event_finished")
while rc[1] != "start":
rc = yield(event_manager, "event_finished")
if rc[0] != ESCExecution.RC_OK:
self.logger.report_errors(
"Start event of the start script returned unsuccessful: %d" % rc[0],
[]
)
return
run_event_from_script(start_script, "newgame")
# Run a generic action
@@ -319,3 +307,29 @@ func _input(event):
# - p_paused: if true, pauses the game. If false, unpauses the game.
func set_game_paused(p_paused: bool):
get_tree().paused = p_paused
# Runs the event "event_name" from the "script" ESC script.
#
# #### Parameters
# - script: ESC script containing the event to run. The script must have been
# loaded.
# - event_name: Name of the event to run
func run_event_from_script(script: ESCScript, event_name: String):
if script == null:
logger.report_errors(
"escoria.gd:run_event_from_script()",
["Requested action %s on unloaded script %s" % [event_name, script],
"Please load the ESC script using esc_compiler.load_esc_file()."]
)
event_manager.queue_event(script.events[event_name])
var rc = yield(event_manager, "event_finished")
while rc[1] != event_name:
rc = yield(event_manager, "event_finished")
if rc[0] != ESCExecution.RC_OK:
self.logger.report_errors(
"Start event of the start script returned unsuccessful: %d" % rc[0],
[]
)
return

View File

@@ -5,15 +5,5 @@ extends Node
# Start the main menu
func _ready():
if escoria.main_menu_instance == null:
if ProjectSettings.get_setting("escoria/ui/main_menu_scene") == "":
escoria.logger.report_errors("escoria.gd",
["Parameter escoria/ui/main_menu_scene is not set!"]
)
else:
escoria.main_menu_instance = escoria.resource_cache.get_resource(
ProjectSettings.get_setting("escoria/ui/main_menu_scene")
).instance()
escoria.call_deferred("add_child", escoria.main_menu_instance)
escoria.init()

View File

@@ -50,26 +50,6 @@ func set_escoria_ui_settings():
}
ProjectSettings.add_property_info(default_dialog_scene_property_info)
if !ProjectSettings.has_setting("escoria/ui/main_menu_scene"):
ProjectSettings.set_setting("escoria/ui/main_menu_scene", "")
var main_menu_scene_property_info = {
"name": "escoria/ui/main_menu_scene",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_FILE,
"hint_string": "*.tscn, *.scn"
}
ProjectSettings.add_property_info(main_menu_scene_property_info)
if !ProjectSettings.has_setting("escoria/ui/pause_menu_scene"):
ProjectSettings.set_setting("escoria/ui/pause_menu_scene", "")
var pause_menu_scene_property_info = {
"name": "escoria/ui/pause_menu_scene",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_FILE,
"hint_string": "*.tscn, *.scn"
}
ProjectSettings.add_property_info(pause_menu_scene_property_info)
if !ProjectSettings.has_setting("escoria/ui/game_scene"):
ProjectSettings.set_setting("escoria/ui/game_scene", "")
var game_scene_property_info = {