From f4f39995a519eccf1272ab2378d63179c8f8c5e5 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 24 Nov 2023 21:58:52 +0100 Subject: [PATCH] Refactor CMC and fix camera combat path reference --- Pawn_Unreal/Content/Characters/BP_Judy.uasset | 4 +- .../Input/BP_MainPlayerController.uasset | 4 +- .../Camera/BP_MainCameraManager.uasset | 4 +- .../0/GT/B3FVEW201B4H0JZT407R3G.uasset | 3 + .../9/RQ/JYNZEWOG6SXNE3HGUCSZIE.uasset | 4 +- .../PwnCharacterMovementComponent.cpp | 70 ++++++++++--------- .../PwnGameplayModeSubsystem.cpp | 7 ++ .../PwnCharacterMovementComponent.h | 4 ++ .../GameplayModes/PwnGameplayModeSubsystem.h | 2 + 9 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/0/GT/B3FVEW201B4H0JZT407R3G.uasset diff --git a/Pawn_Unreal/Content/Characters/BP_Judy.uasset b/Pawn_Unreal/Content/Characters/BP_Judy.uasset index 81daf98..389a5c3 100644 --- a/Pawn_Unreal/Content/Characters/BP_Judy.uasset +++ b/Pawn_Unreal/Content/Characters/BP_Judy.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ba68b8e6df108b46ff6182f4bb3a15391034b9259b8e16381379bd711afb5e04 -size 217117 +oid sha256:bd2a2e9d60bd4cbb7c01cb1692f10c44fd32352d550e9b5c49f0ced315796a9c +size 230326 diff --git a/Pawn_Unreal/Content/Input/BP_MainPlayerController.uasset b/Pawn_Unreal/Content/Input/BP_MainPlayerController.uasset index 3c9165b..f27fa8d 100644 --- a/Pawn_Unreal/Content/Input/BP_MainPlayerController.uasset +++ b/Pawn_Unreal/Content/Input/BP_MainPlayerController.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0a3d83a4007eb2b0e785528f71283f2fc8bc21fd41a3338d2a2a1118cf38516e -size 268121 +oid sha256:9626f55cfae07f9f58e1886f85cba00a69f9a8d0b52ff90d79fd8fa33838b85b +size 254476 diff --git a/Pawn_Unreal/Content/Systems/Camera/BP_MainCameraManager.uasset b/Pawn_Unreal/Content/Systems/Camera/BP_MainCameraManager.uasset index 8c0ab71..415f1d9 100644 --- a/Pawn_Unreal/Content/Systems/Camera/BP_MainCameraManager.uasset +++ b/Pawn_Unreal/Content/Systems/Camera/BP_MainCameraManager.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7b6bc522ab4664b5a68e5e9661850906c9fb33725a3a60f194b7c3488c5138a3 -size 382307 +oid sha256:fb7015689dcc5530837aaa9f515f15c24328b9db292cf69a6f08a6109a5c26b5 +size 403121 diff --git a/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/0/GT/B3FVEW201B4H0JZT407R3G.uasset b/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/0/GT/B3FVEW201B4H0JZT407R3G.uasset new file mode 100644 index 0000000..d15b211 --- /dev/null +++ b/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/0/GT/B3FVEW201B4H0JZT407R3G.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f9f24ee5e3a068b3b1d3b884b17e5a9b21d25f8ac526ab856ae74e327f08dd11 +size 92597 diff --git a/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/9/RQ/JYNZEWOG6SXNE3HGUCSZIE.uasset b/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/9/RQ/JYNZEWOG6SXNE3HGUCSZIE.uasset index ab67cb9..ac3060e 100644 --- a/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/9/RQ/JYNZEWOG6SXNE3HGUCSZIE.uasset +++ b/Pawn_Unreal/Content/__ExternalActors__/Maps/Dev/MAP_ControllerGym/9/RQ/JYNZEWOG6SXNE3HGUCSZIE.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b44d29873f5b9283e01f56ae5071217fd9a4dc983d8ce46a80e345769258dae2 -size 249503 +oid sha256:b5fc3b30f3fe873ddad5a3b878959df02740cc2002b9d9b90eb8ffcfa96750a0 +size 232038 diff --git a/Pawn_Unreal/Source/Pawn/Private/Characters/PwnCharacterMovementComponent.cpp b/Pawn_Unreal/Source/Pawn/Private/Characters/PwnCharacterMovementComponent.cpp index 7580329..103100c 100644 --- a/Pawn_Unreal/Source/Pawn/Private/Characters/PwnCharacterMovementComponent.cpp +++ b/Pawn_Unreal/Source/Pawn/Private/Characters/PwnCharacterMovementComponent.cpp @@ -86,32 +86,12 @@ void UPwnCharacterMovementComponent::EnterSplineFollowMode() { IsFollowingSpline = true; SetMovementMode(MOVE_Custom, SplineWalking); - const UPwnGameplayModeSubsystem* Subsystem = GetWorld()->GetSubsystem(); - check(Subsystem); - - - APwnCombatPlatformerPath* OutCombatPath; - if (Subsystem->FindClosestCombatPathLocation(UpdatedComponent->GetComponentLocation(), OutCombatPath)) { - CombatPath = OutCombatPath; - DotDirection = CombatPath->Reversed ? -1.0f : 1.0f; - - const float ClosestInputKey = CombatPath->Spline->FindInputKeyClosestToWorldLocation(UpdatedComponent->GetComponentLocation()); - FVector ClosestLocation = CombatPath->Spline->GetLocationAtSplineInputKey(ClosestInputKey, ESplineCoordinateSpace::World); - - FHitResult Hit; - if (GetWorld()->LineTraceSingleByChannel(Hit, ClosestLocation, ClosestLocation + FVector(0.0f, 0.0f, -LineTraceDistance), - ECC_Visibility, IGNORE_OWNER_PARAMS)) { - const float SplineKey = CombatPath->FlattenedSpline->FindInputKeyClosestToWorldLocation(Hit.ImpactPoint); - DistanceAlongSpline = CombatPath->FlattenedSpline->GetDistanceAlongSplineAtSplineInputKey(SplineKey); - - ClosestLocation.Z += CharacterOwner->GetCapsuleComponent()->GetScaledCapsuleHalfHeight(); - UpdatedComponent->SetWorldLocation(ClosestLocation); - } - } + UpdateCurrentCombatPath(true); } void UPwnCharacterMovementComponent::ExitSplineFollowMode() { IsFollowingSpline = false; + CombatPath = nullptr; SetMovementMode(MOVE_Walking); } @@ -119,6 +99,36 @@ bool UPwnCharacterMovementComponent::IsCustomMovementMode(const ECustomMovementM return MovementMode == MOVE_Custom && CustomMovementMode == Mode; } +bool UPwnCharacterMovementComponent::LineTraceToGround(FHitResult& OutHit, const FVector& StartLocation) const { + const FVector EndLocation = StartLocation + FVector(0.0f, 0.0f, -LineTraceDistance); + return GetWorld()->LineTraceSingleByChannel(OutHit, StartLocation, EndLocation, ECC_Visibility, IGNORE_OWNER_PARAMS); +} + +void UPwnCharacterMovementComponent::UpdateCurrentCombatPath(const bool UpdateLocation) { + APwnCombatPlatformerPath* OutCombatPath; + FHitResult Hit; + if (LineTraceToGround(Hit, UpdatedComponent->GetComponentLocation()) + && UPwnGameplayModeSubsystem::Get(this).FindClosestCombatPathLocation(Hit.ImpactPoint, OutCombatPath) + && OutCombatPath != CombatPath) { + CombatPath = OutCombatPath; + DotDirection = CombatPath->Reversed ? -1.0f : 1.0f; + + const float ClosestInputKey = CombatPath->Spline->FindInputKeyClosestToWorldLocation(UpdatedComponent->GetComponentLocation()); + const FVector ClosestLocation = CombatPath->Spline->GetLocationAtSplineInputKey(ClosestInputKey, ESplineCoordinateSpace::World); + + if (LineTraceToGround(Hit, ClosestLocation)) { + const float SplineKey = CombatPath->FlattenedSpline->FindInputKeyClosestToWorldLocation(Hit.ImpactPoint); + DistanceAlongSpline = CombatPath->FlattenedSpline->GetDistanceAlongSplineAtSplineInputKey(SplineKey); + + if (UpdateLocation) { + FVector NewLocation = Hit.ImpactPoint; + NewLocation.Z += CharacterOwner->GetCapsuleComponent()->GetScaledCapsuleHalfHeight(); + UpdatedComponent->SetWorldLocation(NewLocation); + } + } + } +} + void UPwnCharacterMovementComponent::UpdateTangentAndAcceleration() { Tangent2D = CombatPath->FlattenedSpline->GetTangentAtDistanceAlongSpline(DistanceAlongSpline, ESplineCoordinateSpace::World).GetSafeNormal2D(); // Recalculate acceleration so the input is relative to the spline @@ -150,9 +160,7 @@ void UPwnCharacterMovementComponent::UpdatePawnVelocity(const float TimeTick) { void UPwnCharacterMovementComponent::UpdateDistanceAlongSpline() { FHitResult Hit; - if (GetWorld()->LineTraceSingleByChannel(Hit, UpdatedComponent->GetComponentLocation(), - UpdatedComponent->GetComponentLocation() + FVector(0.0f, 0.0f, -LineTraceDistance), - ECC_Visibility, IGNORE_OWNER_PARAMS)) { + if (LineTraceToGround(Hit, UpdatedComponent->GetComponentLocation())) { const FVector ImpactPoint = Hit.ImpactPoint; const float InputKey = CombatPath->FlattenedSpline->FindInputKeyClosestToWorldLocation(ImpactPoint); DistanceAlongSpline = CombatPath->FlattenedSpline->GetDistanceAlongSplineAtSplineInputKey(InputKey); @@ -171,6 +179,8 @@ void UPwnCharacterMovementComponent::PhysSplineWalking(const float DeltaTime, in return; } + UpdateCurrentCombatPath(); /* -- PAWN MODIFICATIONS -- */ + if (!CharacterOwner || (!CharacterOwner->Controller && !bRunPhysicsWithNoController && !HasAnimRootMotion() && !CurrentRootMotion. HasOverrideVelocity() && (CharacterOwner->GetLocalRole() != ROLE_SimulatedProxy))) { Acceleration = FVector::ZeroVector; @@ -368,13 +378,12 @@ void UPwnCharacterMovementComponent::PhysSplineWalking(const float DeltaTime, in UpdateLocationOnFlattenedSpline(); } -UE_DISABLE_OPTIMIZATION - void UPwnCharacterMovementComponent::PhysSplineFalling(const float DeltaTime, int32 Iterations) { if (DeltaTime < MIN_TICK_TIME) { return; } + UpdateCurrentCombatPath(); /* -- PAWN MODIFICATIONS -- */ UpdateTangentAndAcceleration(); /* -- PAWN MODIFICATIONS -- */ FVector FallAcceleration = GetFallingLateralAcceleration(DeltaTime); @@ -579,8 +588,6 @@ void UPwnCharacterMovementComponent::PhysSplineFalling(const float DeltaTime, in : NewVelocity; } - //UpdatePawnVelocity(TimeTick); /* -- PAWN MODIFICATIONS -- */ - if (SubTimeTickRemaining > UE_KINDA_SMALL_NUMBER && (Delta | Adjusted) > 0.f) { // Move in deflected direction. SafeMoveUpdatedComponent(Delta, PawnRotation, true, Hit); @@ -631,7 +638,6 @@ void UPwnCharacterMovementComponent::PhysSplineFalling(const float DeltaTime, in Velocity = HasAnimRootMotion() || CurrentRootMotion.HasOverrideVelocityWithIgnoreZAccumulate() ? FVector(Velocity.X, Velocity.Y, NewVelocity.Z) : NewVelocity; - //UpdatePawnVelocity(TimeTick); /* -- PAWN MODIFICATIONS -- */ } // bDitch=true means that pawn is straddling two slopes, neither of which it can stand on @@ -661,8 +667,6 @@ void UPwnCharacterMovementComponent::PhysSplineFalling(const float DeltaTime, in Velocity.Y += 0.25f * GetMaxSpeed() * (RandomStream.FRand() - 0.5f); Velocity.Z = FMath::Max(JumpZVelocity * 0.25f, 1.f); - //UpdatePawnVelocity(TimeTick); /* -- PAWN MODIFICATIONS -- */ - Delta = Velocity * TimeTick; SafeMoveUpdatedComponent(Delta, PawnRotation, true, Hit); @@ -683,5 +687,3 @@ void UPwnCharacterMovementComponent::PhysSplineFalling(const float DeltaTime, in UpdateDistanceAlongSpline(); UpdateLocationOnFlattenedSpline(); } - -UE_ENABLE_OPTIMIZATION diff --git a/Pawn_Unreal/Source/Pawn/Private/GameplayModes/PwnGameplayModeSubsystem.cpp b/Pawn_Unreal/Source/Pawn/Private/GameplayModes/PwnGameplayModeSubsystem.cpp index bbf4c2a..22bbe14 100644 --- a/Pawn_Unreal/Source/Pawn/Private/GameplayModes/PwnGameplayModeSubsystem.cpp +++ b/Pawn_Unreal/Source/Pawn/Private/GameplayModes/PwnGameplayModeSubsystem.cpp @@ -3,6 +3,13 @@ #include "Components/SplineComponent.h" #include "GameplayModes/Combat/PwnCombatPlatformerPath.h" +UPwnGameplayModeSubsystem& UPwnGameplayModeSubsystem::Get(const UObject* WorldContextObject) { + const UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContextObject); + UPwnGameplayModeSubsystem* GameplayModeSubsystem = World->GetSubsystem(); + check(GameplayModeSubsystem); + return *GameplayModeSubsystem; +} + void UPwnGameplayModeSubsystem::Initialize(FSubsystemCollectionBase& Collection) { Super::Initialize(Collection); CurrentGameplayMode = EPwnGameplayMode::Narrative; diff --git a/Pawn_Unreal/Source/Pawn/Public/Characters/PwnCharacterMovementComponent.h b/Pawn_Unreal/Source/Pawn/Public/Characters/PwnCharacterMovementComponent.h index d39bced..1203fa9 100644 --- a/Pawn_Unreal/Source/Pawn/Public/Characters/PwnCharacterMovementComponent.h +++ b/Pawn_Unreal/Source/Pawn/Public/Characters/PwnCharacterMovementComponent.h @@ -51,6 +51,10 @@ public: bool IsCustomMovementMode(const ECustomMovementMode Mode) const; private: + bool LineTraceToGround(FHitResult& OutHit, const FVector& StartLocation) const; + + void UpdateCurrentCombatPath(const bool UpdateLocation = false); + /** * Updates the tangent in function of the current distance along the spline. Then updates the acceleration so it will simulate an input in the * direction of the tangent. diff --git a/Pawn_Unreal/Source/Pawn/Public/GameplayModes/PwnGameplayModeSubsystem.h b/Pawn_Unreal/Source/Pawn/Public/GameplayModes/PwnGameplayModeSubsystem.h index 0f3547a..6b01140 100644 --- a/Pawn_Unreal/Source/Pawn/Public/GameplayModes/PwnGameplayModeSubsystem.h +++ b/Pawn_Unreal/Source/Pawn/Public/GameplayModes/PwnGameplayModeSubsystem.h @@ -19,6 +19,8 @@ class PAWN_API UPwnGameplayModeSubsystem : public UWorldSubsystem { GENERATED_BODY() public: + static UPwnGameplayModeSubsystem& Get(const UObject* WorldContextObject); + virtual void Initialize(FSubsystemCollectionBase& Collection) override; virtual void Deinitialize() override;