MCPcopy Create free account
hub / github.com/arnaud-jamin/Cog / FindBestTarget

Method FindBestTarget

Source/CogSample/CogSampleTargetAcquisition.cpp:95–231  ·  view source on GitHub ↗

--------------------------------------------------------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

93
94//--------------------------------------------------------------------------------------------------------------------------
95void UCogSampleTargetAcquisition::FindBestTarget(
96 const APlayerController* Controller,
97 const TArray<AActor*>& TargetsToIgnore,
98 const AActor* CurrentLockedTarget,
99 const bool bForceSynchronousDetection,
100 const FVector2D TargetSwitchSearchDirection,
101 const bool bIsDebugPersistent,
102 FCogSampleTargetAcquisitionResult& Result) const
103{
104 TRACE_CPUPROFILER_EVENT_SCOPE(UCogSampleTargetAcquisition::FindBestTarget);
105
106 ACogSampleCharacter* Character = Cast<ACogSampleCharacter>(Controller->GetPawn());
107 if (Character == nullptr)
108 {
109 return;
110 }
111
112 if (FMath::IsNearlyZero(DetectionLength) && FMath::IsNearlyZero(DetectionRadius))
113 {
114 return;
115 }
116
117 FMatrix ViewProjectionMatrix;
118 FIntRect ViewRect;
119 if (GetViewInfo(Controller, ViewProjectionMatrix, ViewRect) == false)
120 {
121 return;
122 }
123
124 FVector2D SearchDirectionScreenOrigin(ViewRect.Width() / 2.0f, ViewRect.Height() / 2.0f);
125 if (CurrentLockedTarget != nullptr)
126 {
127 FVector CurrentLockedTargetLocation = UCogSampleFunctionLibrary_Gameplay::GetActorTargetLocation(CurrentLockedTarget);
128 FSceneView::ProjectWorldToScreen(CurrentLockedTargetLocation, ViewRect, ViewProjectionMatrix, SearchDirectionScreenOrigin);
129 }
130
131 //----------------------------------------------------------------------------------------------------------------------
132 // Draw the ScreenSearchDirection if valid
133 //----------------------------------------------------------------------------------------------------------------------
134#if ENABLE_COG
135 const FVector2D SearchDirectionNormalized = (TargetSwitchSearchDirection.IsNearlyZero() == false) ? TargetSwitchSearchDirection.GetSafeNormal() : FVector2D::ZeroVector;
136 if (SearchDirectionNormalized.IsNearlyZero() == false)
137 {
138 FCogDebugDraw::Segment2D(LogCogTargetAcquisition, Controller, FVector2D::ZeroVector, FVector2D(SearchDirectionNormalized.X, -SearchDirectionNormalized.Y), FColor(255, 255, 0, 255), bIsDebugPersistent);
139 }
140#endif //ENABLE_COG
141
142 static const FName TraceTag(TEXT("FindLockTarget_GatherTargets"));
143 FCollisionQueryParams QueryParams(TraceTag, SCENE_QUERY_STAT_ONLY(CogSampleTargetAcquisition), false);
144 QueryParams.bReturnPhysicalMaterial = true;
145 QueryParams.bReturnFaceIndex = false;
146 QueryParams.AddIgnoredActor(Character);
147
148 const FCollisionObjectQueryParams ObjectParams = UCogSampleFunctionLibrary_Gameplay::ConfigureCollisionObjectParams(ObjectTypes);
149 const FVector CastLocation = GetReferentialLocation(Character, DetectionLocation);
150 const FRotator CastRotation = GetReferentialRotation(Character, DetectionRotation);
151 const float CapsuleHalfHeight = DetectionLength * 0.5f;
152 const float CapsuleRadius = DetectionRadius;

Callers 1

TickTargetingMethod · 0.80

Calls 2

GetWorldMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected