diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos_block.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos_block.gd new file mode 100644 index 00000000..e067bc4d --- /dev/null +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos_block.gd @@ -0,0 +1,68 @@ +# `camera_set_pos_block time x y` +# +# Moves the camera to the given absolute position over a time period. Blocks +# until the command completes. +# +# **Parameters** +# +# - *time*: Number of seconds the transition should take +# - *x*: Target X coordinate +# - "y*: Target Y coordinate +# +# For more details see: https://docs.escoria-framework.org/camera +# +# @ESC +extends ESCCameraBaseCommand +class_name CameraSetPosBlockCommand + + +# Tween for blocking +var _camera_tween: Tween + + +# Return the descriptor of the arguments of this command +func configure() -> ESCCommandArgumentDescriptor: + return ESCCommandArgumentDescriptor.new( + 3, + [[TYPE_REAL, TYPE_INT], TYPE_INT, TYPE_INT], + [null, null, null] + ) + + +# Validate whether the given arguments match the command descriptor +func validate(arguments: Array): + if not .validate(arguments): + return false + + var new_pos: Vector2 = Vector2(arguments[1], arguments[2]) + var camera: ESCCamera = escoria.object_manager.get_object(escoria.object_manager.CAMERA).node as ESCCamera + + if not camera.check_point_is_inside_viewport_limits(new_pos): + generate_viewport_warning(new_pos, camera) + return false + + _camera_tween = camera.get_tween() + + return true + + +# Run the command +func run(command_params: Array) -> int: + (escoria.object_manager.get_object(escoria.object_manager.CAMERA).node as ESCCamera)\ + .set_target( + Vector2(command_params[1], command_params[2]), + command_params[0] + ) + + if command_params[0] > 0.0: + yield(_camera_tween, "tween_completed") + + return ESCExecution.RC_OK + + +# Function called when the command is interrupted. +func interrupt(): + escoria.logger.warn( + self, + "[%s] interrupt() function not implemented." % get_command_name() + ) diff --git a/addons/escoria-core/game/scenes/camera_player/esc_camera.gd b/addons/escoria-core/game/scenes/camera_player/esc_camera.gd index 0a24730e..2ca9c83f 100644 --- a/addons/escoria-core/game/scenes/camera_player/esc_camera.gd +++ b/addons/escoria-core/game/scenes/camera_player/esc_camera.gd @@ -53,6 +53,13 @@ func register(room = null): ) +# Returns the camera's tween. +# +# **Returns** the tween owned by this camera. +func get_tween() -> Tween: + return _tween + + # Sets camera limits so it doesn't go out of the scene # # #### Parameters @@ -65,36 +72,6 @@ func set_limits(limits: ESCCameraLimits): self.limit_bottom = limits.limit_bottom -# Resolve the correct position and zoom of the target object -# -# #### Parameters -# - p_target: The target to resolve -func _resolve_target_and_zoom(p_target) -> void: - _target = Vector2() - _zoom_target = Vector2() - _follow_target = null - - if p_target is Node and "is_movable" in p_target and p_target.is_movable: - _follow_target = p_target - - if p_target is Vector2: - _target = p_target - elif p_target is Array and p_target.size() > 0: - var target_pos = Vector2() - - for obj in p_target: - target_pos += obj.get_camera_pos() - - _target = target_pos / p_target.size() - elif p_target.has_method("get_camera_node"): - if "global_position" in p_target.get_camera_node(): - _target = p_target.get_camera_node().global_position - if "zoom" in p_target.get_camera_node(): - _zoom_target = p_target.get_camera_node().zoom - else: - _target = p_target.global_position - - func set_drag_margin_enabled(p_dm_h_enabled, p_dm_v_enabled): self.drag_margin_h_enabled = p_dm_h_enabled self.drag_margin_v_enabled = p_dm_v_enabled @@ -425,3 +402,33 @@ func _convert_pos_for_disabled_drag_margin(pos: Vector2) -> Vector2: ret_position.y = limit_bottom - viewport_rect.size.y * 0.5 * zoom.y return ret_position + + +# Resolve the correct position and zoom of the target object +# +# #### Parameters +# - p_target: The target to resolve +func _resolve_target_and_zoom(p_target) -> void: + _target = Vector2() + _zoom_target = Vector2() + _follow_target = null + + if p_target is Node and "is_movable" in p_target and p_target.is_movable: + _follow_target = p_target + + if p_target is Vector2: + _target = p_target + elif p_target is Array and p_target.size() > 0: + var target_pos = Vector2() + + for obj in p_target: + target_pos += obj.get_camera_pos() + + _target = target_pos / p_target.size() + elif p_target.has_method("get_camera_node"): + if "global_position" in p_target.get_camera_node(): + _target = p_target.get_camera_node().global_position + if "zoom" in p_target.get_camera_node(): + _zoom_target = p_target.get_camera_node().zoom + else: + _target = p_target.global_position diff --git a/game/rooms/room07/esc/button_set_pos.esc b/game/rooms/room07/esc/button_set_pos.esc index 2ea8a971..b84d3ed5 100755 --- a/game/rooms/room07/esc/button_set_pos.esc +++ b/game/rooms/room07/esc/button_set_pos.esc @@ -6,12 +6,12 @@ say player "That button uses camera_set_pos to point the camera at coordinates ( accept_input NONE # Note that camera_set_pos puts the camera centre at a different location depending on # the camera's limits -camera_set_limits 1 -camera_set_pos 0 1200 800 -wait 2 -camera_set_limits 0 -camera_set_pos 0 1200 800 -wait 2 +#camera_set_limits 1 +camera_set_pos_block 2 2260 1050 +#wait 2 +#camera_set_limits 0 +#camera_set_pos 0 1200 800 +#wait 2 camera_set_target 1 player # Re-enable user input accept_input ALL