Skip to content

Commit

Permalink
multi: changes to work on a multiplayer setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Jan 15, 2019
1 parent 8bf3d85 commit 5a9143b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
62 changes: 38 additions & 24 deletions TargetSystem/Source/TargetSystem/Private/TargetSystemComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Engine/Public/TimerManager.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Engine/Classes/Camera/CameraComponent.h"
#include "Engine/Classes/Kismet/GameplayStatics.h"
#include "EngineUtils.h"

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

CharacterController = CharacterReference->GetInstigatorController();
if (!CharacterController)
PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (!PlayerController)
{
UE_LOG(LogTemp, Error, TEXT("[%s] TargetSystemComponent: Cannot get Controller reference ..."), *CharacterReference->GetName());
UE_LOG(LogTemp, Error, TEXT("[%s] TargetSystemComponent: Cannot get PlayerController reference ..."), *CharacterReference->GetName());
return;
}

PlayerController = Cast<APlayerController>(CharacterController);
}

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

if (TargetLocked && NearestTarget)
{
if (!TargetIsTargetable(NearestTarget))
{
TargetLockOff();
return;
}

SetControlRotationOnTarget(NearestTarget);

// Target Locked Off based on Distance
Expand All @@ -66,7 +73,8 @@ void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType,
if (BreakLineOfSightDelay <= 0)
{
TargetLockOff();
} else
}
else
{
bIsBreakingLineOfSight = true;
GetWorld()->GetTimerManager().SetTimer(
Expand All @@ -87,7 +95,8 @@ void UTargetSystemComponent::TargetActor()
if (TargetLocked)
{
TargetLockOff();
} else
}
else
{
TArray<AActor*> Actors = GetAllActorsOfClass(TargetableActors);
NearestTarget = FindNearestTarget(Actors);
Expand All @@ -114,7 +123,7 @@ void UTargetSystemComponent::TargetActorWithAxisInput(float AxisValue)
{
return;
}

// Lock off target
AActor* CurrentTarget = NearestTarget;

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

// Get All Actors of Class
TArray<AActor*> Actors = GetAllActorsOfClass(TargetableActors);

// For each of these actors, check line trace and ignore Current Target and build the list of actors to look from
TArray<AActor*> ActorsToLook;

Expand Down Expand Up @@ -236,7 +245,7 @@ void UTargetSystemComponent::TargetLockOn(AActor* TargetToLockOn)
ControlRotation(true);
}

CharacterController->SetIgnoreLookInput(true);
PlayerController->SetIgnoreLookInput(true);

OnTargetLockedOn.Broadcast(TargetToLockOn);
}
Expand All @@ -257,7 +266,7 @@ void UTargetSystemComponent::TargetLockOff()
ControlRotation(false);
}

CharacterController->ResetIgnoreLookInput();
PlayerController->ResetIgnoreLookInput();

OnTargetLockedOff.Broadcast(NearestTarget);
}
Expand All @@ -271,7 +280,8 @@ void UTargetSystemComponent::CreateAndAttachTargetLockedOnWidgetComponent(AActor
if (TargetLockedOnWidgetClass)
{
TargetLockedOnWidgetComponent->SetWidgetClass(TargetLockedOnWidgetClass);
} else
}
else
{
TargetLockedOnWidgetComponent->SetWidgetClass(UTargetSystemLockOnWidget::StaticClass());
}
Expand All @@ -290,15 +300,8 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
for (TActorIterator<AActor> ActorIterator(GetWorld(), ActorClass); ActorIterator; ++ActorIterator)
{
AActor* Actor = *ActorIterator;
bool bIsImplemented = Actor->GetClass()->ImplementsInterface(UTargetSystemTargetableInterface::StaticClass());
if (bIsImplemented)
{
bool bIsTargetable = ITargetSystemTargetableInterface::Execute_IsTargetable(Actor);
if (bIsTargetable)
{
Actors.Add(Actor);
}
} else
bool IsTargetable = TargetIsTargetable(Actor);
if (IsTargetable)
{
Actors.Add(Actor);
}
Expand All @@ -307,6 +310,17 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
return Actors;
}

bool UTargetSystemComponent::TargetIsTargetable(AActor* Actor)
{
bool bIsImplemented = Actor->GetClass()->ImplementsInterface(UTargetSystemTargetableInterface::StaticClass());
if (bIsImplemented)
{
return ITargetSystemTargetableInterface::Execute_IsTargetable(Actor);
}

return true;
}

AActor* UTargetSystemComponent::FindNearestTarget(TArray<AActor*> Actors)
{
TArray<AActor*> ActorsHit;
Expand Down Expand Up @@ -380,7 +394,7 @@ bool UTargetSystemComponent::LineTrace(FHitResult& HitResult, AActor* OtherActor

FRotator UTargetSystemComponent::GetControlRotationOnTarget(AActor* OtherActor)
{
FRotator ControlRotation = CharacterController->GetControlRotation();
FRotator ControlRotation = PlayerController->GetControlRotation();

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

void UTargetSystemComponent::SetControlRotationOnTarget(AActor* TargetActor)
{
if (!CharacterController)
if (!PlayerController)
{
return;
}

CharacterController->SetControlRotation(GetControlRotationOnTarget(TargetActor));
PlayerController->SetControlRotation(GetControlRotationOnTarget(TargetActor));
}

float UTargetSystemComponent::GetDistanceFromCharacter(AActor* OtherActor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class TARGETSYSTEM_API UTargetSystemComponent : public UActorComponent
FRotator FindLookAtRotation(const FVector Start, const FVector Target);
void ResetIsSwitchingTarget();

bool TargetIsTargetable(AActor* Actor);

protected:
// Called when the game starts
virtual void BeginPlay() override;
Expand Down
2 changes: 1 addition & 1 deletion TargetSystem/TargetSystem.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0.0",
"VersionName": "1.0.1",
"EngineVersion": "4.21.0",
"FriendlyName": "TargetSystem",
"Description": "Dark Souls inspired Camera Lock On / Targeting system",
Expand Down

0 comments on commit 5a9143b

Please sign in to comment.