From 6d3b189234d16ab6ec891972511eb34899585667 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 17 Jan 2022 14:56:25 -0500 Subject: [PATCH] Clamp character and camera position to stage boundaries Clamp character and camera position to stage boundaries --- KOFForever/Content/Maps/DebugStage.umap | 4 +- .../Private/Camera/KOFDefaultCamera.cpp | 6 ++- .../KOFCharacterMovementComponent.cpp | 40 ++++--------------- .../Private/GameModes/KOFDefaultGameMode.cpp | 1 + .../KOFCharacterMovementComponent.h | 2 - .../Public/GameModes/KOFDefaultGameMode.h | 5 +++ 6 files changed, 19 insertions(+), 39 deletions(-) diff --git a/KOFForever/Content/Maps/DebugStage.umap b/KOFForever/Content/Maps/DebugStage.umap index 9fa925c..0f2842d 100644 --- a/KOFForever/Content/Maps/DebugStage.umap +++ b/KOFForever/Content/Maps/DebugStage.umap @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0734c34f22596584ab16ae499c6df6869ce893efc067598c4e80b8a451b6677d -size 8531398 +oid sha256:743fbea5cf9687ba65781c185871af056bec291a110e2adb760de694f1ea94e4 +size 8528916 diff --git a/KOFForever/Source/KOFForever/Private/Camera/KOFDefaultCamera.cpp b/KOFForever/Source/KOFForever/Private/Camera/KOFDefaultCamera.cpp index 16ae7c2..0699074 100644 --- a/KOFForever/Source/KOFForever/Private/Camera/KOFDefaultCamera.cpp +++ b/KOFForever/Source/KOFForever/Private/Camera/KOFDefaultCamera.cpp @@ -7,6 +7,7 @@ #include "Camera/CameraComponent.h" #include "Character/KOFBaseCharacter.h" #include "GameModes/KOFDefaultGameMode.h" +#include "Kismet/GameplayStatics.h" AKOFDefaultCamera::AKOFDefaultCamera() { @@ -41,8 +42,9 @@ void AKOFDefaultCamera::Tick(float DeltaSeconds) FVector P1Loc = P1->GetActorLocation(); FVector P2Loc = P2->GetActorLocation(); FVector CameraLoc = GetActorLocation(); - - FVector MidPoint = FVector(CameraLoc.X, (P1Loc.Y + P2Loc.Y) / 2.0f, ((P1Loc.Z + P2Loc.Z) / ZAveragingFactor) + HeightOffset); + + float HalfStageLength = GameMode->GetStageLength() / 2.0f; + FVector MidPoint = FVector(CameraLoc.X, FMath::Clamp((P1Loc.Y + P2Loc.Y) / 2.0f, -HalfStageLength, HalfStageLength), ((P1Loc.Z + P2Loc.Z) / ZAveragingFactor) + HeightOffset); SetActorLocation(MidPoint); // set FOV diff --git a/KOFForever/Source/KOFForever/Private/Componenets/KOFCharacterMovementComponent.cpp b/KOFForever/Source/KOFForever/Private/Componenets/KOFCharacterMovementComponent.cpp index 87f401e..2406845 100644 --- a/KOFForever/Source/KOFForever/Private/Componenets/KOFCharacterMovementComponent.cpp +++ b/KOFForever/Source/KOFForever/Private/Componenets/KOFCharacterMovementComponent.cpp @@ -40,37 +40,11 @@ void UKOFCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick T FVector CurrentLoc = Owner->GetActorLocation(); Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z)); } -} - -void UKOFCharacterMovementComponent::PostPhysicsTickComponent(float DeltaTime, - FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction) -{ - Super::PostPhysicsTickComponent(DeltaTime, ThisTickFunction); - - AKOFBaseCharacter* Owner = Cast(GetOwner()); - if(!Owner) - { - UE_LOG(LogTemp, Error, TEXT("AKOFCharacterMovementComponent::PostPhysicsTickComponent - could not get the owner")); - return; - } - - AKOFDefaultGameMode* GameMode = Cast(GetWorld()->GetAuthGameMode()); - if(!GameMode) - { - UE_LOG(LogTemp, Error, TEXT("AKOFCharacterMovementComponent::PostPhysicsTickComponent - could not get the game mode")); - return; - } - - AKOFBaseCharacter* Opponent = GameMode->GetCurrentOpponent(Owner->IsPossessedByPlayer1()); - if(Opponent) - { - float YDistance = FMath::Abs(Opponent->GetActorLocation().Y - Owner->GetActorLocation().Y); - float HalfYDistance = YDistance / 2.0f; - float YMidpoint = (Owner->GetActorLocation().Y + Opponent->GetActorLocation().Y) / 2.0f; - - float AdjustedY = FMath::Clamp(Owner->GetActorLocation().Y, YMidpoint - HalfYDistance, YMidpoint + HalfYDistance); - - FVector CurrentLoc = Owner->GetActorLocation(); - Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z)); - } + + // clamp to stage bounds + FVector CurrentLoc = Owner->GetActorLocation(); + float HalfStageLength = GameMode->GetStageLength() / 2.0f; + Owner->SetActorLocation(FVector(CurrentLoc.X, + FMath::Clamp(CurrentLoc.Y, -HalfStageLength, HalfStageLength), + CurrentLoc.Z)); } diff --git a/KOFForever/Source/KOFForever/Private/GameModes/KOFDefaultGameMode.cpp b/KOFForever/Source/KOFForever/Private/GameModes/KOFDefaultGameMode.cpp index 5b91d5e..23b62e5 100644 --- a/KOFForever/Source/KOFForever/Private/GameModes/KOFDefaultGameMode.cpp +++ b/KOFForever/Source/KOFForever/Private/GameModes/KOFDefaultGameMode.cpp @@ -10,6 +10,7 @@ AKOFDefaultGameMode::AKOFDefaultGameMode() NumCharactersPerTeam = 3; MaxAllowedDistanceFromOpponent = 700.0f; + StageLength = 2400.0f; StartY = 250.0f; } diff --git a/KOFForever/Source/KOFForever/Public/Componenets/KOFCharacterMovementComponent.h b/KOFForever/Source/KOFForever/Public/Componenets/KOFCharacterMovementComponent.h index d1c789c..1a57391 100644 --- a/KOFForever/Source/KOFForever/Public/Componenets/KOFCharacterMovementComponent.h +++ b/KOFForever/Source/KOFForever/Public/Componenets/KOFCharacterMovementComponent.h @@ -17,6 +17,4 @@ class KOFFOREVER_API UKOFCharacterMovementComponent : public UCharacterMovementC UKOFCharacterMovementComponent(); virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; - - virtual void PostPhysicsTickComponent(float DeltaTime, FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction) override; }; diff --git a/KOFForever/Source/KOFForever/Public/GameModes/KOFDefaultGameMode.h b/KOFForever/Source/KOFForever/Public/GameModes/KOFDefaultGameMode.h index 4a21c11..a796ecc 100644 --- a/KOFForever/Source/KOFForever/Public/GameModes/KOFDefaultGameMode.h +++ b/KOFForever/Source/KOFForever/Public/GameModes/KOFDefaultGameMode.h @@ -32,6 +32,8 @@ public: AKOFBaseCharacter* GetCurrentOpponent(bool bIsPlayer1) { return bIsPlayer1 ? GetP2CurrentCharacter() : GetP1CurrentCharacter(); } FORCEINLINE float GetMaxAllowedDistanceFromOpponent() { return MaxAllowedDistanceFromOpponent; } + + FORCEINLINE float GetStageLength() { return StageLength; } protected: /** Number of characters on a single player's team */ @@ -42,6 +44,9 @@ protected: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage") float MaxAllowedDistanceFromOpponent; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage") + float StageLength; + /** Y offset from origin where character's will be spawned */ UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage") float StartY;