From abb01b7f0b200f64667e1af963661b48f1c3e3c9 Mon Sep 17 00:00:00 2001 From: Duncan Brown Date: Thu, 28 Apr 2022 12:10:14 -0400 Subject: [PATCH] feat: validates updated camera position against current camera limits; also some messaging updates --- .../esc/commands/camera_set_pos.gd | 24 +++++++++++++++++++ .../core-scripts/esc/commands/camera_shift.gd | 19 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos.gd index dca9f53a..da9dd29b 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_pos.gd @@ -24,6 +24,30 @@ func configure() -> ESCCommandArgumentDescriptor: ) +# 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 + var camera_limit: Rect2 = Rect2(camera.limit_left, camera.limit_top, camera.limit_right - camera.limit_left, camera.limit_bottom - camera.limit_top) + + if not camera_limit.has_point(new_pos): + escoria.logger.error( + self, + "[%s]: invalid camera position. Camera cannot be moved to %s as this is outside the current camera limit %s." + % [ + get_command_name(), + new_pos, + camera_limit + ] + ) + return false + + return true + + # Run the command func run(command_params: Array) -> int: (escoria.object_manager.get_object(escoria.object_manager.CAMERA).node as ESCCamera)\ diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd index de49d1d9..6c121dee 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd @@ -53,6 +53,7 @@ func run(command_params: Array) -> int: ) return ESCExecution.RC_OK + # Validate whether the given arguments match the command descriptor func validate(arguments: Array): if not .validate(arguments): @@ -74,6 +75,24 @@ func validate(arguments: Array): ) return false + var camera: ESCCamera = escoria.object_manager.get_object(escoria.object_manager.CAMERA).node as ESCCamera + var camera_limit: Rect2 = Rect2(camera.limit_left, camera.limit_top, camera.limit_right - camera.limit_left, camera.limit_bottom - camera.limit_top) + var shift_by: Vector2 = Vector2(arguments[0], arguments[1]) + var new_pos: Vector2 = Vector2(camera.position.x + shift_by.x, camera.position.y + shift_by.y) + + if not camera_limit.has_point(new_pos): + escoria.logger.error( + self, + "[%s]: invalid camera position. Camera cannot be moved by %s to %s as this is outside the current camera limit %s." + % [ + get_command_name(), + shift_by, + new_pos, + camera_limit + ] + ) + return false + return true