haxis/Source/UnrealProject/Abilities/HomingProjectile.cpp

48 lines
1.1 KiB
C++

// Project Lab - NHTV Igad
#include "UnrealProject.h"
#include "HomingProjectile.h"
#include "DefaultGameMode.h"
#include "NetworkPlayer.h"
void AHomingProjectile::BeginPlay()
{
Super::BeginPlay();
float maxdist = BIG_NUMBER;
if (Role == ROLE_Authority)
{
if (targetCreature == nullptr)
{
UWorld* const world = GetWorld();
if (!world) return;
ADefaultGameMode* mode = Cast<ADefaultGameMode>(world->GetAuthGameMode());
if (!mode) return;
TArray<class ANetworkPlayer*> players = mode->GetPlayers();
for (ANetworkPlayer* player : players)
{
float dist = (player->GetActorLocation() - GetActorLocation()).SizeSquared();
if (dist < maxdist)
{
maxdist = dist;
targetCreature = player;
}
}
}
}
}
void AHomingProjectile::NativeMove(float DeltaTime)
{
if (targetCreature == nullptr)
return;
FVector dir = targetCreature->GetActorLocation() - GetActorLocation();
FRotator newrot = FRotationMatrix::MakeFromX(dir).Rotator();
SetActorRotation(newrot);
Super::NativeMove(DeltaTime);
}