Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed shoot registration #98

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Config/DefaultGame.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ bIsDrawHitCursorLine=0
bIsDrawDeprojectedCursorLine=0
bIsShowMouseCursor=1
bIsDrawExplosionSpheres=0
bIsDrawHitScan=0
bIsPrintHitScanInfo=0
NetGraph=1
CameraVerticalSpeedLag=0.000000
UnUsedEnum=Everything
Expand Down
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "Cloud9/Character/Components/Cloud9Inventory.h"
#include "Cloud9/Character/Components/Cloud9SpringArmComponent.h"
#include "Cloud9/Tools/Extensions/TContainer.h"
#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"

const FName ACloud9Character::SpringArmComponentName = TEXT("CameraBoom");
const FName ACloud9Character::CameraComponentName = TEXT("TopDownCamera");
Expand Down
14 changes: 13 additions & 1 deletion Source/Cloud9/Game/Cloud9DeveloperSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,19 @@ void UCloud9DeveloperSettings::InitializeCVars()
RegisterConsoleVariable(
bIsDrawExplosionSpheres,
TEXT("r.bIsDrawExplosionSpheres"),
TEXT("Whether to debug explosions spheres")
TEXT("Whether to draw debug explosions spheres")
);

RegisterConsoleVariable(
bIsDrawHitScan,
TEXT("r.bIsDrawHitScan"),
TEXT("Whether to draw debug hit scan lines")
);

RegisterConsoleVariable(
bIsPrintHitScanInfo,
TEXT("r.bIsPrintHitScanInfo"),
TEXT("Whether to print hit scan info")
);

RegisterConsoleVariable(
Expand Down
6 changes: 6 additions & 0 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class CLOUD9_API UCloud9DeveloperSettings : public UDeveloperSettings
UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
int32 bIsDrawExplosionSpheres;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
int32 bIsDrawHitScan;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
int32 bIsPrintHitScanInfo;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
int32 NetGraph;

Expand Down
100 changes: 68 additions & 32 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "Cloud9/Tools/Extensions/FVector.h"
#include "Cloud9/Game/Cloud9PlayerController.h"
#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"
#include "Cloud9/Weapon/Tables/WeaponTableFirearm.h"

const FName ACloud9WeaponFirearm::TracerProbabilityParameterName = TEXT("Probability");
Expand Down Expand Up @@ -195,7 +196,7 @@ void ACloud9WeaponFirearm::Tick(float DeltaSeconds)

if (not WeaponInfo->bIsFullAuto)
{
WeaponState.ClearAction(EWeaponAction::PrimaryStart);
WeaponState.ClearAction(EWeaponAction::PrimaryLoop);
}
}
else if (WeaponState.IsActionActive(EWeaponAction::PrimaryEnd))
Expand Down Expand Up @@ -240,49 +241,84 @@ bool ACloud9WeaponFirearm::Fire(const FFirearmWeaponInfo* WeaponInfo, const FFir
return true;
}

let Settings = UCloud9DeveloperSettings::Get();

var CollisionParams = FCollisionQueryParams::DefaultQueryParam;

if (Settings->bIsDrawHitScan)
{
const FName TraceTag("HitScanTraceTag");
GetWorld()->DebugDrawTraceTag = TraceTag;
CollisionParams.TraceTag = TraceTag;
CollisionParams.bTraceComplex = true;
}

let StartLocation = MuzzleFlash->GetComponentLocation();
let EndLocation = CursorHit.Location;
// GetHitResultUnderCursor can return coordinates slightly upper then surface
// Prolong line in shoot direction
let EndLocation = FMath::Lerp(StartLocation, FVector{CursorHit.Location}, FirearmCommonData.LineTraceAlpha);

FHitResult LineHit;
if (not GetWorld()->LineTraceSingleByChannel(LineHit, StartLocation, EndLocation, ECC_Visibility))
let IsHit = GetWorld()->LineTraceSingleByChannel(
LineHit,
StartLocation,
EndLocation,
ECC_Visibility,
CollisionParams);

if (Settings->bIsPrintHitScanInfo)
{
log(Display, "LineTraceSingleByChannel not hit anything")
return true;
FString TargetName = IsHit ? *LineHit.Component->GetName() : TEXT("???");
FString OwnerName = IsHit and LineHit.Component->GetOwner() != nullptr
? *LineHit.Component->GetOwner()->GetName()
: TEXT("???");

log(Display,
"Target='%s' Owner='%s' Start={%s} End={%s} TraceEnd={%s} Hit={%s} ",
*TargetName,
*OwnerName,
*StartLocation.ToString(),
*EndLocation.ToString(),
*LineHit.TraceEnd.ToString(),
*LineHit.Location.ToString()
);
}

let Target = LineHit.Component;
if (IsHit)
{
let Target = LineHit.Component;

log(Verbose, "Target = %s Owner = %s", *Target->GetName(), *Target->GetOwner()->GetName());
let Direction = LineHit.Location - StartLocation | EFVector::Normalize{};

let Direction = LineHit.Location - StartLocation | EFVector::Normalize{};
if (Target->IsSimulatingPhysics() and Target->Mobility == EComponentMobility::Movable)
{
let Velocity = WeaponInfo->Damage * FirearmCommonData.ImpulseMultiplier * Direction;
Target->AddImpulse(Velocity, NAME_None, true);
}

if (Target->IsSimulatingPhysics() and Target->Mobility == EComponentMobility::Movable)
{
let Velocity = WeaponInfo->Damage * FirearmCommonData.ImpulseMultiplier * Direction;
Target->AddImpulse(Velocity, NAME_None, true);
}
if (IsValid(FirearmCommonData.Tracer))
{
let Tracer = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
GetWorld(),
FirearmCommonData.Tracer,
StartLocation);
Tracer->SetVectorParameter(TracerDirectionParameterName, Direction);
Tracer->SetFloatParameter(TracerProbabilityParameterName, WeaponInfo->TracerProbability);
Tracer->SetAutoDestroy(true);
}

if (IsValid(FirearmCommonData.Tracer))
{
let Tracer = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
GetWorld(),
FirearmCommonData.Tracer,
StartLocation);
Tracer->SetVectorParameter(TracerDirectionParameterName, Direction);
Tracer->SetFloatParameter(TracerProbabilityParameterName, WeaponInfo->TracerProbability);
Tracer->SetAutoDestroy(true);
}
if (IsValid(FirearmCommonData.Squib))
{
let Squib = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
GetWorld(),
FirearmCommonData.Squib,
LineHit.Location,
CursorHit.Normal.Rotation());
Squib->SetAutoDestroy(true);
}

if (IsValid(FirearmCommonData.Squib))
{
let Squib = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
GetWorld(),
FirearmCommonData.Squib,
LineHit.Location,
CursorHit.Normal.Rotation());
Squib->SetAutoDestroy(true);
return true;
}

return true;
}

Expand Down
8 changes: 8 additions & 0 deletions Source/Cloud9/Weapon/Tables/WeaponCommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ struct FFirearmCommonData
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Damage)
float ImpulseMultiplier = 50.0f;

/**
* Alpha coefficient to fixup hit-scan registration.
*
* GetHitUnderCursor can return value slightly upper then surface.
*/
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Settings)
float LineTraceAlpha = 5.0f;

UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Visual)
float MagazineLifetime = 10.0f;

Expand Down