Clamp character and camera position to stage boundaries
Clamp character and camera position to stage boundaries
This commit is contained in:
		
							parent
							
								
									0942a3a1c6
								
							
						
					
					
						commit
						6d3b189234
					
				
							
								
								
									
										
											BIN
										
									
								
								KOFForever/Content/Maps/DebugStage.umap
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								KOFForever/Content/Maps/DebugStage.umap
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							@ -7,6 +7,7 @@
 | 
				
			|||||||
#include "Camera/CameraComponent.h"
 | 
					#include "Camera/CameraComponent.h"
 | 
				
			||||||
#include "Character/KOFBaseCharacter.h"
 | 
					#include "Character/KOFBaseCharacter.h"
 | 
				
			||||||
#include "GameModes/KOFDefaultGameMode.h"
 | 
					#include "GameModes/KOFDefaultGameMode.h"
 | 
				
			||||||
 | 
					#include "Kismet/GameplayStatics.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
AKOFDefaultCamera::AKOFDefaultCamera()
 | 
					AKOFDefaultCamera::AKOFDefaultCamera()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@ -41,8 +42,9 @@ void AKOFDefaultCamera::Tick(float DeltaSeconds)
 | 
				
			|||||||
		FVector P1Loc = P1->GetActorLocation();
 | 
							FVector P1Loc = P1->GetActorLocation();
 | 
				
			||||||
		FVector P2Loc = P2->GetActorLocation();
 | 
							FVector P2Loc = P2->GetActorLocation();
 | 
				
			||||||
		FVector CameraLoc = 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);
 | 
							SetActorLocation(MidPoint);
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// set FOV
 | 
							// set FOV
 | 
				
			||||||
 | 
				
			|||||||
@ -40,37 +40,11 @@ void UKOFCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick T
 | 
				
			|||||||
		FVector CurrentLoc = Owner->GetActorLocation();
 | 
							FVector CurrentLoc = Owner->GetActorLocation();
 | 
				
			||||||
		Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z));
 | 
							Owner->SetActorLocation(FVector(CurrentLoc.X, AdjustedY, CurrentLoc.Z));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					
 | 
				
			||||||
 | 
						// clamp to stage bounds
 | 
				
			||||||
void UKOFCharacterMovementComponent::PostPhysicsTickComponent(float DeltaTime,
 | 
						FVector CurrentLoc = Owner->GetActorLocation();
 | 
				
			||||||
                                                              FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction)
 | 
						float HalfStageLength = GameMode->GetStageLength() / 2.0f;
 | 
				
			||||||
{
 | 
						Owner->SetActorLocation(FVector(CurrentLoc.X,
 | 
				
			||||||
	Super::PostPhysicsTickComponent(DeltaTime, ThisTickFunction);
 | 
														FMath::Clamp(CurrentLoc.Y, -HalfStageLength, HalfStageLength),
 | 
				
			||||||
 | 
														CurrentLoc.Z));
 | 
				
			||||||
	AKOFBaseCharacter* Owner = Cast<AKOFBaseCharacter>(GetOwner());
 | 
					 | 
				
			||||||
	if(!Owner)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		UE_LOG(LogTemp, Error, TEXT("AKOFCharacterMovementComponent::PostPhysicsTickComponent - could not get the owner"));
 | 
					 | 
				
			||||||
		return;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	AKOFDefaultGameMode* GameMode = Cast<AKOFDefaultGameMode>(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));
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,7 @@ AKOFDefaultGameMode::AKOFDefaultGameMode()
 | 
				
			|||||||
	NumCharactersPerTeam = 3;
 | 
						NumCharactersPerTeam = 3;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	MaxAllowedDistanceFromOpponent = 700.0f;
 | 
						MaxAllowedDistanceFromOpponent = 700.0f;
 | 
				
			||||||
 | 
						StageLength = 2400.0f;
 | 
				
			||||||
	StartY = 250.0f;
 | 
						StartY = 250.0f;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,4 @@ class KOFFOREVER_API UKOFCharacterMovementComponent : public UCharacterMovementC
 | 
				
			|||||||
	UKOFCharacterMovementComponent();
 | 
						UKOFCharacterMovementComponent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
 | 
						virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	virtual void PostPhysicsTickComponent(float DeltaTime, FCharacterMovementComponentPostPhysicsTickFunction& ThisTickFunction) override;
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -32,6 +32,8 @@ public:
 | 
				
			|||||||
	AKOFBaseCharacter* GetCurrentOpponent(bool bIsPlayer1) { return bIsPlayer1 ? GetP2CurrentCharacter() : GetP1CurrentCharacter(); }
 | 
						AKOFBaseCharacter* GetCurrentOpponent(bool bIsPlayer1) { return bIsPlayer1 ? GetP2CurrentCharacter() : GetP1CurrentCharacter(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	FORCEINLINE float GetMaxAllowedDistanceFromOpponent() { return MaxAllowedDistanceFromOpponent; }
 | 
						FORCEINLINE float GetMaxAllowedDistanceFromOpponent() { return MaxAllowedDistanceFromOpponent; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						FORCEINLINE float GetStageLength() { return StageLength; }
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
	/** Number of characters on a single player's team */
 | 
						/** Number of characters on a single player's team */
 | 
				
			||||||
@ -42,6 +44,9 @@ protected:
 | 
				
			|||||||
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
 | 
						UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
 | 
				
			||||||
	float MaxAllowedDistanceFromOpponent;
 | 
						float MaxAllowedDistanceFromOpponent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
 | 
				
			||||||
 | 
						float StageLength;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	/** Y offset from origin where character's will be spawned */
 | 
						/** Y offset from origin where character's will be spawned */
 | 
				
			||||||
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
 | 
						UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Settings|Stage")
 | 
				
			||||||
	float StartY;
 | 
						float StartY;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user