From ee7fd3960f0f56bb7c13533b2e53260c6159b779 Mon Sep 17 00:00:00 2001 From: Jesus Bracho Date: Mon, 12 Sep 2022 11:57:40 -0400 Subject: [PATCH] Tie the movement to delta time --- Content/BP_RTSCamera.uasset | 4 +- OpenRTSCamera.uplugin | 6 ++- Source/OpenRTSCamera/Private/RTSCamera.cpp | 45 ++++++++++++++-------- Source/OpenRTSCamera/Public/RTSCamera.h | 21 +++++++++- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/Content/BP_RTSCamera.uasset b/Content/BP_RTSCamera.uasset index 4c77b8a..5757514 100644 --- a/Content/BP_RTSCamera.uasset +++ b/Content/BP_RTSCamera.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cde9f455612a223ed74daa161f8d9ba2a6f1ef19a98112cfc4cfe630f4d3a6aa -size 25065 +oid sha256:984f7fd67597cf686d4ae09a89eea79bd93739d28fd6d6a2dfc726819974a50c +size 25165 diff --git a/OpenRTSCamera.uplugin b/OpenRTSCamera.uplugin index 99179e8..7b3f902 100644 --- a/OpenRTSCamera.uplugin +++ b/OpenRTSCamera.uplugin @@ -18,7 +18,9 @@ "Name": "OpenRTSCamera", "Type": "Runtime", "LoadingPhase": "Default", - "WhitelistPlatforms": ["Win64"] + "PlatformAllowList": [ + "Win64" + ] } ], "Plugins": [ @@ -29,5 +31,5 @@ ], "SupportURL": "https://github.com/HeyZoos/OpenRTSCamera/issues", "Version": 1, - "VersionName": "0.16.0" + "VersionName": "0.17.0" } \ No newline at end of file diff --git a/Source/OpenRTSCamera/Private/RTSCamera.cpp b/Source/OpenRTSCamera/Private/RTSCamera.cpp index 94557aa..6680733 100644 --- a/Source/OpenRTSCamera/Private/RTSCamera.cpp +++ b/Source/OpenRTSCamera/Private/RTSCamera.cpp @@ -78,6 +78,7 @@ void URTSCamera::TickComponent( { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); this->DeltaSeconds = DeltaTime; + this->ApplyMoveCameraCommands(); this->ConditionallyPerformEdgeScrolling(); this->ConditionallyKeepCameraAtDesiredZoomAboveGround(); this->SmoothTargetArmLengthToDesiredZoom(); @@ -148,7 +149,7 @@ void URTSCamera::OnTurnCameraRight(const FInputActionValue&) void URTSCamera::OnMoveCameraYAxis(const FInputActionValue& Value) { - this->MoveSpringArmWithMoveSpeed( + this->RequestMoveCamera( this->SpringArm->GetForwardVector().X, this->SpringArm->GetForwardVector().Y, Value.Get() @@ -157,7 +158,7 @@ void URTSCamera::OnMoveCameraYAxis(const FInputActionValue& Value) void URTSCamera::OnMoveCameraXAxis(const FInputActionValue& Value) { - this->MoveSpringArmWithMoveSpeed( + this->RequestMoveCamera( this->SpringArm->GetRightVector().X, this->SpringArm->GetRightVector().Y, Value.Get() @@ -182,13 +183,13 @@ void URTSCamera::OnDragCamera(const FInputActionValue& Value) Delta.X = FMath::Clamp(Delta.X, -DragExtents.X, DragExtents.X) / DragExtents.X; Delta.Y = FMath::Clamp(Delta.Y, -DragExtents.Y, DragExtents.Y) / DragExtents.Y; - this->MoveSpringArmWithMoveSpeed( + this->RequestMoveCamera( this->SpringArm->GetRightVector().X, this->SpringArm->GetRightVector().Y, Delta.X ); - this->MoveSpringArmWithMoveSpeed( + this->RequestMoveCamera( this->SpringArm->GetForwardVector().X, this->SpringArm->GetForwardVector().Y, Delta.Y * -1 @@ -201,14 +202,28 @@ void URTSCamera::OnDragCamera(const FInputActionValue& Value) } } -void URTSCamera::MoveSpringArmWithMoveSpeed(const float X, const float Y, const float Scale) const +void URTSCamera::RequestMoveCamera(const float X, const float Y, const float Scale) { - auto Movement = FVector2D(X, Y); - Movement.Normalize(); - Movement *= this->MoveSpeed * Scale; - this->Root->SetWorldLocation( - this->Root->GetComponentLocation() + FVector(Movement.X, Movement.Y, 0.0f) - ); + FMoveCameraCommand MoveCameraCommand; + MoveCameraCommand.X = X; + MoveCameraCommand.Y = Y; + MoveCameraCommand.Scale = Scale; + MoveCameraCommands.Push(MoveCameraCommand); +} + +void URTSCamera::ApplyMoveCameraCommands() +{ + for (const auto& [X, Y, Scale] : this->MoveCameraCommands) + { + auto Movement = FVector2D(X, Y); + Movement.Normalize(); + Movement *= this->MoveSpeed * Scale * this->DeltaSeconds; + this->Root->SetWorldLocation( + this->Root->GetComponentLocation() + FVector(Movement.X, Movement.Y, 0.0f) + ); + } + + this->MoveCameraCommands.Empty(); } void URTSCamera::CollectComponentDependencyReferences() @@ -387,7 +402,7 @@ void URTSCamera::EdgeScrollLeft() const const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0); this->Root->AddRelativeLocation( - -1 * this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed + -1 * this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds ); } @@ -403,7 +418,7 @@ void URTSCamera::EdgeScrollRight() const const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0); this->Root->AddRelativeLocation( - this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed + this->Root->GetRightVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds ); } @@ -419,7 +434,7 @@ void URTSCamera::EdgeScrollUp() const const auto Movement = 1 - UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0); this->Root->AddRelativeLocation( - this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed + this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds ); } @@ -435,7 +450,7 @@ void URTSCamera::EdgeScrollDown() const const auto Movement = UKismetMathLibrary::FClamp(NormalizedMousePosition, 0.0, 1.0); this->Root->AddRelativeLocation( - -1 * this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed + -1 * this->Root->GetForwardVector() * Movement * this->EdgeScrollSpeed * this->DeltaSeconds ); } diff --git a/Source/OpenRTSCamera/Public/RTSCamera.h b/Source/OpenRTSCamera/Public/RTSCamera.h index efcdc53..95c58ce 100644 --- a/Source/OpenRTSCamera/Public/RTSCamera.h +++ b/Source/OpenRTSCamera/Public/RTSCamera.h @@ -9,6 +9,22 @@ #include "GameFramework/SpringArmComponent.h" #include "RTSCamera.generated.h" +/** + * We use these commands so that move camera inputs can be tied to the tick rate of the game. + * https://github.com/HeyZoos/OpenRTSCamera/issues/27 + */ +USTRUCT() +struct FMoveCameraCommand +{ + GENERATED_BODY() + UPROPERTY() + float X; + UPROPERTY() + float Y; + UPROPERTY() + float Scale; +}; + UCLASS(Blueprintable, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) class OPENRTSCAMERA_API URTSCamera : public UActorComponent { @@ -125,7 +141,8 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent void OnMoveCameraXAxis(const FInputActionValue& Value); void OnDragCamera(const FInputActionValue& Value); - void MoveSpringArmWithMoveSpeed(float X, float Y, float Scale) const; + void RequestMoveCamera(float X, float Y, float Scale); + void ApplyMoveCameraCommands(); UPROPERTY() AActor* Owner; @@ -175,4 +192,6 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent bool IsDragging; UPROPERTY() FVector2D DragStartLocation; + UPROPERTY() + TArray MoveCameraCommands; };