From 09ef9e89feb55f97744566a764f2a0396afe53c5 Mon Sep 17 00:00:00 2001 From: Dennis Ploeger Date: Mon, 27 Sep 2021 15:33:51 +0200 Subject: [PATCH] feat: Use wait parameter instead of immediate for turn_to and set_angle (#403) Co-authored-by: Dennis Ploeger --- .../core-scripts/behaviors/esc_movable.gd | 70 +++++++++---------- .../core-scripts/esc/commands/set_angle.gd | 16 +++-- .../game/core-scripts/esc/commands/turn_to.gd | 10 +-- .../game/core-scripts/esc_item.gd | 14 ++-- game/rooms/room01/esc/room01.esc | 4 +- game/rooms/room10/esc/button_turn_to.esc | 4 +- 6 files changed, 57 insertions(+), 61 deletions(-) diff --git a/addons/escoria-core/game/core-scripts/behaviors/esc_movable.gd b/addons/escoria-core/game/core-scripts/behaviors/esc_movable.gd index b98bd313..928f9846 100644 --- a/addons/escoria-core/game/core-scripts/behaviors/esc_movable.gd +++ b/addons/escoria-core/game/core-scripts/behaviors/esc_movable.gd @@ -443,10 +443,8 @@ func _angle_is_between( # #### Parameters # # - deg int angle to set the character -# - immediate -# If true, direction is switched immediately. Else, successive -# animations are used so that the character turns to target angle. -func set_angle(deg: int, immediate = true) -> void: +# - wait float Wait this amount of seconds until continuing with turning around +func set_angle(deg: int, wait: float = 0.0) -> void: if deg < 0 or deg > 360: escoria.logger.report_errors( "movable.gd:set_angle()", @@ -454,37 +452,35 @@ func set_angle(deg: int, immediate = true) -> void: ) moved = true - if immediate: - last_dir = _get_dir_deg(deg, parent.animations) - - # The character may have a state animation from before, which would be - # resumed, so we immediately force the correct idle animation - if parent.get_animation_player().get_animation() != \ - parent.animations.idles[last_dir].animation: - parent.get_animation_player().play( - parent.animations.idles[last_dir].animation - ) - pose_scale = -1 if parent.animations.idles[last_dir].mirrored else 1 - else: - var current_dir = last_dir - var target_dir = _get_dir_deg(deg, parent.animations) - - var way_to_turn = get_shortest_way_to_dir(current_dir, target_dir) + var current_dir = last_dir + var target_dir = _get_dir_deg(deg, parent.animations) - var dir = current_dir - while dir != target_dir: - dir += way_to_turn - if dir >= parent.animations.dir_angles.size(): - dir = 0 - if dir < 0: - dir = parent.animations.dir_angles.size() - 1 - - parent.get_animation_player().play( - parent.animations.idles[dir].animation - ) - yield(parent.get_animation_player(), "animation_finished") - pose_scale = -1 if parent.animations.idles[dir].mirrored else 1 + var way_to_turn = get_shortest_way_to_dir(current_dir, target_dir) + + var dir = current_dir + while dir != target_dir: + dir += way_to_turn + if dir >= parent.animations.dir_angles.size(): + dir = 0 + if dir < 0: + dir = parent.animations.dir_angles.size() - 1 + + parent.get_animation_player().play( + parent.animations.idles[dir].animation + ) + if wait > 0.0: + yield(get_tree().create_timer(wait), "timeout") + pose_scale = -1 if parent.animations.idles[dir].mirrored else 1 + last_dir = _get_dir_deg(deg, parent.animations) + + # The character may have a state animation from before, which would be + # resumed, so we immediately force the correct idle animation + if parent.get_animation_player().get_animation() != \ + parent.animations.idles[last_dir].animation: + parent.get_animation_player().play( + parent.animations.idles[last_dir].animation + ) update_terrain() @@ -493,17 +489,15 @@ func set_angle(deg: int, immediate = true) -> void: # #### Parameters # # - item_id id of the object to face. -# - immediate -# If true, direction is switched immediately. Else, successive -# animations are used so that the character turns to target angle. -func turn_to(item: Node, immediate = true) -> void: +# - float Wait this amount of seconds until continuing with turning around +func turn_to(item: Node, wait: float = 0.0) -> void: set_angle( wrapi( rad2deg(parent.get_position().angle_to_point(item.get_position())), 0, 360 ), - immediate + wait ) diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd index 9a210111..507c88d4 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd @@ -1,4 +1,4 @@ -# `set_angle object degrees [immediate]` +# `set_angle object degrees [wait]` # # Turns object to a degrees angle without animations. 0 sets object facing # forward, 90 sets it 90 degrees clockwise ("east") etc. When turning to the @@ -7,6 +7,9 @@ # object must be player or interactive. degrees must be between [0, 360] or an # error is reported. # +# The wait parameter sets how long to wait for each intermediate angle. It +# defaults to 0, meaning the turnaround is immediate. +# # @ESC extends ESCBaseCommand class_name SetAngleCommand @@ -16,8 +19,8 @@ class_name SetAngleCommand func configure() -> ESCCommandArgumentDescriptor: return ESCCommandArgumentDescriptor.new( 2, - [TYPE_STRING, TYPE_INT, TYPE_BOOL], - [null, null, true] + [TYPE_STRING, TYPE_INT, TYPE_REAL], + [null, null, 0.0] ) @@ -36,12 +39,13 @@ func validate(arguments: Array): # Run the command func run(command_params: Array) -> int: - var immediate = command_params[2] - # HACK Countering the fact that angle_to_point() function gives # angle against X axis not Y, we need to check direction using (angle-90°). # Since the ESC command already gives the right angle, we add 90. escoria.object_manager.get_object(command_params[0]).node\ - .set_angle(wrapi(int(command_params[1]) + 90, 0, 360), immediate) + .set_angle( + wrapi(int(command_params[1]) + 90, 0, 360), + command_params[2] + ) return ESCExecution.RC_OK diff --git a/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd b/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd index 4af1a9d5..34cd0774 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd @@ -1,9 +1,9 @@ -# `turn_to object object_to_face [immediate]` +# `turn_to object object_to_face [wait]` # # Turns object to face another object. # -# Set immediate to true to show directly switch to the direction and not -# show intermediate angles +# The wait parameter sets how long to wait for each intermediate angle. It +# defaults to 0, meaning the turnaround is immediate. # # @ESC extends ESCBaseCommand @@ -14,8 +14,8 @@ class_name TurnToCommand func configure() -> ESCCommandArgumentDescriptor: return ESCCommandArgumentDescriptor.new( 2, - [TYPE_STRING, TYPE_STRING, TYPE_BOOL], - [null, null, false] + [TYPE_STRING, TYPE_STRING, TYPE_REAL], + [null, null, 0.0] ) diff --git a/addons/escoria-core/game/core-scripts/esc_item.gd b/addons/escoria-core/game/core-scripts/esc_item.gd index 9c7d1d77..d8f67b2f 100644 --- a/addons/escoria-core/game/core-scripts/esc_item.gd +++ b/addons/escoria-core/game/core-scripts/esc_item.gd @@ -421,10 +421,9 @@ func get_sprite() -> Node: # #### Parameters # # - deg: The angle degree to set -# - immediate: Set the angle immediately. If false will show intermediate -# angles -func set_angle(deg: int, immediate = true): - _movable.set_angle(deg, immediate) +# - wait: Wait this amount of seconds until continuing with turning around +func set_angle(deg: int, wait: float = 0.0): + _movable.set_angle(deg, wait) # Turn to face another object @@ -432,10 +431,9 @@ func set_angle(deg: int, immediate = true): # #### Parameters # # - deg: The angle degree to set -# - immediate: Set the angle immediately. If false will show intermediate -# angles -func turn_to(object: Node, immediate = true): - _movable.turn_to(object, immediate) +# - float Wait this amount of seconds until continuing with turning around +func turn_to(object: Node, wait: float = 0.0): + _movable.turn_to(object, wait) # Play the talking animation diff --git a/game/rooms/room01/esc/room01.esc b/game/rooms/room01/esc/room01.esc index 558a8eec..a1a53f8f 100644 --- a/game/rooms/room01/esc/room01.esc +++ b/game/rooms/room01/esc/room01.esc @@ -21,10 +21,10 @@ set_sound_state _music res://game/sfx/contemplation.ogg true wait 2 walk_block player r1_destination_point2 wait 2 - set_angle player 225 false + set_angle player 225 0.2 wait 2 walk_block player r1_destination_point3 wait 1 - turn_to player r1_r_exit false + turn_to player r1_r_exit diff --git a/game/rooms/room10/esc/button_turn_to.esc b/game/rooms/room10/esc/button_turn_to.esc index 1a9bdf4a..33a57ce9 100644 --- a/game/rooms/room10/esc/button_turn_to.esc +++ b/game/rooms/room10/esc/button_turn_to.esc @@ -2,8 +2,8 @@ say player "Huh?" -turn_to player 180 true +turn_to player slide_pos_1 say player "Nothing." -turn_to player 0 +turn_to player slide_pos_2 0.2