Skip to content

Commit 5a9143b

Browse files
committed
multi: changes to work on a multiplayer setup
1 parent 8bf3d85 commit 5a9143b

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

TargetSystem/Source/TargetSystem/Private/TargetSystemComponent.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Engine/Public/TimerManager.h"
88
#include "GameFramework/CharacterMovementComponent.h"
99
#include "Engine/Classes/Camera/CameraComponent.h"
10+
#include "Engine/Classes/Kismet/GameplayStatics.h"
1011
#include "EngineUtils.h"
1112

1213
// Sets default values for this component's properties
@@ -36,15 +37,15 @@ void UTargetSystemComponent::BeginPlay()
3637
if (!CharacterReference)
3738
{
3839
UE_LOG(LogTemp, Error, TEXT("[%s] TargetSystemComponent: Cannot get Owner reference ..."), *this->GetName());
40+
return;
3941
}
4042

41-
CharacterController = CharacterReference->GetInstigatorController();
42-
if (!CharacterController)
43+
PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
44+
if (!PlayerController)
4345
{
44-
UE_LOG(LogTemp, Error, TEXT("[%s] TargetSystemComponent: Cannot get Controller reference ..."), *CharacterReference->GetName());
46+
UE_LOG(LogTemp, Error, TEXT("[%s] TargetSystemComponent: Cannot get PlayerController reference ..."), *CharacterReference->GetName());
47+
return;
4548
}
46-
47-
PlayerController = Cast<APlayerController>(CharacterController);
4849
}
4950

5051
void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
@@ -53,6 +54,12 @@ void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType,
5354

5455
if (TargetLocked && NearestTarget)
5556
{
57+
if (!TargetIsTargetable(NearestTarget))
58+
{
59+
TargetLockOff();
60+
return;
61+
}
62+
5663
SetControlRotationOnTarget(NearestTarget);
5764

5865
// Target Locked Off based on Distance
@@ -66,7 +73,8 @@ void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType,
6673
if (BreakLineOfSightDelay <= 0)
6774
{
6875
TargetLockOff();
69-
} else
76+
}
77+
else
7078
{
7179
bIsBreakingLineOfSight = true;
7280
GetWorld()->GetTimerManager().SetTimer(
@@ -87,7 +95,8 @@ void UTargetSystemComponent::TargetActor()
8795
if (TargetLocked)
8896
{
8997
TargetLockOff();
90-
} else
98+
}
99+
else
91100
{
92101
TArray<AActor*> Actors = GetAllActorsOfClass(TargetableActors);
93102
NearestTarget = FindNearestTarget(Actors);
@@ -114,7 +123,7 @@ void UTargetSystemComponent::TargetActorWithAxisInput(float AxisValue)
114123
{
115124
return;
116125
}
117-
126+
118127
// Lock off target
119128
AActor* CurrentTarget = NearestTarget;
120129

@@ -127,7 +136,7 @@ void UTargetSystemComponent::TargetActorWithAxisInput(float AxisValue)
127136

128137
// Get All Actors of Class
129138
TArray<AActor*> Actors = GetAllActorsOfClass(TargetableActors);
130-
139+
131140
// For each of these actors, check line trace and ignore Current Target and build the list of actors to look from
132141
TArray<AActor*> ActorsToLook;
133142

@@ -236,7 +245,7 @@ void UTargetSystemComponent::TargetLockOn(AActor* TargetToLockOn)
236245
ControlRotation(true);
237246
}
238247

239-
CharacterController->SetIgnoreLookInput(true);
248+
PlayerController->SetIgnoreLookInput(true);
240249

241250
OnTargetLockedOn.Broadcast(TargetToLockOn);
242251
}
@@ -257,7 +266,7 @@ void UTargetSystemComponent::TargetLockOff()
257266
ControlRotation(false);
258267
}
259268

260-
CharacterController->ResetIgnoreLookInput();
269+
PlayerController->ResetIgnoreLookInput();
261270

262271
OnTargetLockedOff.Broadcast(NearestTarget);
263272
}
@@ -271,7 +280,8 @@ void UTargetSystemComponent::CreateAndAttachTargetLockedOnWidgetComponent(AActor
271280
if (TargetLockedOnWidgetClass)
272281
{
273282
TargetLockedOnWidgetComponent->SetWidgetClass(TargetLockedOnWidgetClass);
274-
} else
283+
}
284+
else
275285
{
276286
TargetLockedOnWidgetComponent->SetWidgetClass(UTargetSystemLockOnWidget::StaticClass());
277287
}
@@ -290,15 +300,8 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
290300
for (TActorIterator<AActor> ActorIterator(GetWorld(), ActorClass); ActorIterator; ++ActorIterator)
291301
{
292302
AActor* Actor = *ActorIterator;
293-
bool bIsImplemented = Actor->GetClass()->ImplementsInterface(UTargetSystemTargetableInterface::StaticClass());
294-
if (bIsImplemented)
295-
{
296-
bool bIsTargetable = ITargetSystemTargetableInterface::Execute_IsTargetable(Actor);
297-
if (bIsTargetable)
298-
{
299-
Actors.Add(Actor);
300-
}
301-
} else
303+
bool IsTargetable = TargetIsTargetable(Actor);
304+
if (IsTargetable)
302305
{
303306
Actors.Add(Actor);
304307
}
@@ -307,6 +310,17 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
307310
return Actors;
308311
}
309312

313+
bool UTargetSystemComponent::TargetIsTargetable(AActor* Actor)
314+
{
315+
bool bIsImplemented = Actor->GetClass()->ImplementsInterface(UTargetSystemTargetableInterface::StaticClass());
316+
if (bIsImplemented)
317+
{
318+
return ITargetSystemTargetableInterface::Execute_IsTargetable(Actor);
319+
}
320+
321+
return true;
322+
}
323+
310324
AActor* UTargetSystemComponent::FindNearestTarget(TArray<AActor*> Actors)
311325
{
312326
TArray<AActor*> ActorsHit;
@@ -380,7 +394,7 @@ bool UTargetSystemComponent::LineTrace(FHitResult& HitResult, AActor* OtherActor
380394

381395
FRotator UTargetSystemComponent::GetControlRotationOnTarget(AActor* OtherActor)
382396
{
383-
FRotator ControlRotation = CharacterController->GetControlRotation();
397+
FRotator ControlRotation = PlayerController->GetControlRotation();
384398

385399
FVector CharacterLocation = CharacterReference->GetActorLocation();
386400
FVector OtherActorLocation = OtherActor->GetActorLocation();
@@ -395,12 +409,12 @@ FRotator UTargetSystemComponent::GetControlRotationOnTarget(AActor* OtherActor)
395409

396410
void UTargetSystemComponent::SetControlRotationOnTarget(AActor* TargetActor)
397411
{
398-
if (!CharacterController)
412+
if (!PlayerController)
399413
{
400414
return;
401415
}
402416

403-
CharacterController->SetControlRotation(GetControlRotationOnTarget(TargetActor));
417+
PlayerController->SetControlRotation(GetControlRotationOnTarget(TargetActor));
404418
}
405419

406420
float UTargetSystemComponent::GetDistanceFromCharacter(AActor* OtherActor)

TargetSystem/Source/TargetSystem/Public/TargetSystemComponent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class TARGETSYSTEM_API UTargetSystemComponent : public UActorComponent
104104
FRotator FindLookAtRotation(const FVector Start, const FVector Target);
105105
void ResetIsSwitchingTarget();
106106

107+
bool TargetIsTargetable(AActor* Actor);
108+
107109
protected:
108110
// Called when the game starts
109111
virtual void BeginPlay() override;

TargetSystem/TargetSystem.uplugin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"FileVersion": 3,
33
"Version": 1,
4-
"VersionName": "1.0.0",
4+
"VersionName": "1.0.1",
55
"EngineVersion": "4.21.0",
66
"FriendlyName": "TargetSystem",
77
"Description": "Dark Souls inspired Camera Lock On / Targeting system",

0 commit comments

Comments
 (0)