// AGun.cpp
#include “Gun.h”
#include “Components/StaticMeshComponent.h”
#include “Kismet/GameplayStatics.h”
#include “Net/UnrealNetwork.h”
#include “Particles/ParticleSystemComponent.h”
AGun::AGun()
{
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
GunMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GunMesh"));
GunMesh->SetupAttachment(Root);
bReplicates = true;
SetReplicates(true);
SetReplicateMovement(true);
SetReplicatingMovement(true);
}
void AGun::BeginPlay()
{
Super::BeginPlay();
}
void AGun::GetLifetimeReplicatedProps(TArray &OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AGun, TraceDistance);
// DOREPLIFETIME(AGun, ImpactEmitter);
}
void AGun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool AGun::ServerPerformLineTrace_Validate()
{
return true;
}
void AGun::ServerPerformLineTrace_Implementation()
{
// if(HasAuthority())
PerformLineTrace();
}
void AGun::PerformLineTrace()
{
// if (HasAuthority())
// {
// APawn *OwnerPawn = Cast<APawn>(GetOwner());
FVector Location = GetActorLocation();
// OwnerPawn->GetController()->GetPlayerViewPoint(Location, Rotation);
FVector End = Location + (-GetActorForwardVector()) * TraceDistance;
FHitResult HitResult;
FCollisionQueryParams CollisionParams;
// CollisionParams.AddIgnoredActor(this);
if (GetWorld()->LineTraceSingleByChannel(HitResult, Location, End, ECC_Visibility, CollisionParams))
{
DrawDebugLine(GetWorld(), Location, End, FColor::Red, false, 2.f, 1, 5.f);
UE_LOG(LogTemp, Error, TEXT("%s"), *HitResult.GetActor()->GetName());
}
// }
// else
// {
// ServerPerformLineTrace();
// }
}
void AGun::Shoot()
{
if(HasAuthority())
{
PerformLineTrace();
}
else
{
ServerPerformLineTrace();
}
}