Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Content/BP_RTSCamera.uasset
Git LFS file not shown
6 changes: 4 additions & 2 deletions OpenRTSCamera.uplugin
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"Name": "OpenRTSCamera",
"Type": "Runtime",
"LoadingPhase": "Default",
"WhitelistPlatforms": ["Win64"]
"PlatformAllowList": [
"Win64"
]
}
],
"Plugins": [
Expand All @@ -29,5 +31,5 @@
],
"SupportURL": "https://github.com/HeyZoos/OpenRTSCamera/issues",
"Version": 1,
"VersionName": "0.16.0"
"VersionName": "0.17.0"
}
45 changes: 30 additions & 15 deletions Source/OpenRTSCamera/Private/RTSCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void URTSCamera::TickComponent(
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
this->DeltaSeconds = DeltaTime;
this->ApplyMoveCameraCommands();
this->ConditionallyPerformEdgeScrolling();
this->ConditionallyKeepCameraAtDesiredZoomAboveGround();
this->SmoothTargetArmLengthToDesiredZoom();
Expand Down Expand Up @@ -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<float>()
Expand All @@ -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<float>()
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand Down
21 changes: 20 additions & 1 deletion Source/OpenRTSCamera/Public/RTSCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -175,4 +192,6 @@ class OPENRTSCAMERA_API URTSCamera : public UActorComponent
bool IsDragging;
UPROPERTY()
FVector2D DragStartLocation;
UPROPERTY()
TArray<FMoveCameraCommand> MoveCameraCommands;
};