blaster/Source/Blaster/Weapon/ProjectileWeapon.cpp

72 lines
2.8 KiB
C++
Raw Normal View History

2022-05-04 17:17:25 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileWeapon.h"
2022-05-05 10:51:14 +00:00
#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"));
2022-05-27 21:33:52 +00:00
UWorld* World = GetWorld();
if (MuzzleFlashSocket && World)
2022-05-05 10:51:14 +00:00
{
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
// From muzzle flash socket to hit location from TraceUnderCrosshairs
FVector ToTarget = HitTarget - SocketTransform.GetLocation();
FRotator TargetRotation = ToTarget.Rotation();
2022-05-27 21:33:52 +00:00
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;
}
2022-05-28 08:30:24 +00:00
else // Server, not locally controlled - spawn non-replicated projectile, SSR
2022-05-27 21:33:52 +00:00
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
2022-05-28 08:30:24 +00:00
SpawnedProjectile->bUseServerSideRewind = true;
2022-05-27 21:33:52 +00:00
}
}
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())
2022-05-05 10:51:14 +00:00
{
2022-05-27 21:33:52 +00:00
SpawnedProjectile = World->SpawnActor<AProjectile>(ProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = false;
SpawnedProjectile->Damage = Damage;
2022-05-05 10:51:14 +00:00
}
}
}
}