72 lines
2.8 KiB
C++
72 lines
2.8 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "ProjectileWeapon.h"
|
|
|
|
#include "Projectile.h"
|
|
#include "Engine/SkeletalMeshSocket.h"
|
|
|
|
void AProjectileWeapon::Fire(const FVector& HitTarget)
|
|
{
|
|
Super::Fire(HitTarget);
|
|
|
|
APawn* InstigatorPawn = Cast<APawn>(GetOwner());
|
|
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName(FName("MuzzleFlash"));
|
|
UWorld* World = GetWorld();
|
|
if (MuzzleFlashSocket && World)
|
|
{
|
|
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
|
|
// From muzzle flash socket to hit location from TraceUnderCrosshairs
|
|
FVector ToTarget = HitTarget - SocketTransform.GetLocation();
|
|
FRotator TargetRotation = ToTarget.Rotation();
|
|
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.Owner = GetOwner();
|
|
SpawnParams.Instigator = InstigatorPawn;
|
|
|
|
AProjectile* SpawnedProjectile = nullptr;
|
|
if (bUseServerSideRewind)
|
|
{
|
|
if (InstigatorPawn->HasAuthority()) // Server
|
|
{
|
|
if (InstigatorPawn->IsLocallyControlled()) // Server, host - use replicated projectile
|
|
{
|
|
SpawnedProjectile = World->SpawnActor<AProjectile>(ProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
|
|
SpawnedProjectile->bUseServerSideRewind = false;
|
|
SpawnedProjectile->Damage = Damage;
|
|
}
|
|
else // Server, not locally controlled - spawn non-replicated projectile, SSR
|
|
{
|
|
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
|
|
SpawnedProjectile->bUseServerSideRewind = true;
|
|
}
|
|
}
|
|
else // Client, using SSR
|
|
{
|
|
if (InstigatorPawn->IsLocallyControlled()) // Client, locally controlled - spawn non-replicated projectile, use SSR
|
|
{
|
|
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
|
|
SpawnedProjectile->bUseServerSideRewind = true;
|
|
SpawnedProjectile->TraceStart = SocketTransform.GetLocation();
|
|
SpawnedProjectile->InitialVelocity= SpawnedProjectile->GetActorForwardVector() * SpawnedProjectile->InitialSpeed;
|
|
SpawnedProjectile->Damage = Damage;
|
|
}
|
|
else // Client, not locally controlled - spawn non-replicated projectile, no SSR
|
|
{
|
|
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
|
|
SpawnedProjectile->bUseServerSideRewind = false;
|
|
}
|
|
}
|
|
}
|
|
else // Weapon not using SSR
|
|
{
|
|
if (InstigatorPawn->HasAuthority())
|
|
{
|
|
SpawnedProjectile = World->SpawnActor<AProjectile>(ProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
|
|
SpawnedProjectile->bUseServerSideRewind = false;
|
|
SpawnedProjectile->Damage = Damage;
|
|
}
|
|
}
|
|
}
|
|
}
|