Skip to content

Commit

Permalink
#216 Fixed self damage registration (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat authored Feb 23, 2024
1 parent 91ad7bd commit ed084c1
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 90 deletions.
1 change: 1 addition & 0 deletions Config/DefaultInput.ini
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ DoubleClickTime=0.200000
+ActionMappings=(ActionName="Reload",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=R)
+ActionMappings=(ActionName="Use",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=E)
+ActionMappings=(ActionName="Drop",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=G)
+ActionMappings=(ActionName="CursorSelfAim",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=V)
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W)
+AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S)
+AxisMappings=(AxisName="MoveRight",Scale=1.000000,Key=D)
Expand Down
Binary file modified Content/Characters/Blueprints/BP_Cloud9Character.uasset
Binary file not shown.
107 changes: 57 additions & 50 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,58 +124,65 @@ void ACloud9Character::UnSneak() const
}
}

void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHitValid)
void ACloud9Character::SetViewDirection(const TOptional<FHitResult>& HitResult)
{
if (IsValid(CursorToWorld))
if (HitResult)
{
let ImpactNormal = HitResult.ImpactNormal;
let ImpactRotation = ImpactNormal.Rotation();

CursorToWorld->SetWorldLocation(HitResult.Location);
CursorToWorld->SetWorldRotation(ImpactRotation);
SetCursorIsHidden(false);
}

let Settings = UCloud9DeveloperSettings::Get();

let StartLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);

if (Settings->bIsDrawHitCursorLine)
{
DrawDebugLine(
GetWorld(),
StartLocation,
HitResult.Location,
FColor::Green,
false,
0.0);
}

if (Settings->bIsDrawDeprojectedCursorLine)
{
FVector WorldLocation;
FVector WorldDirection;
FVector2D MousePosition;

GetCloud9Controller()->GetMousePosition(MousePosition.X, MousePosition.Y);
GetCloud9Controller()->DeprojectScreenPositionToWorld(
MousePosition.X,
MousePosition.Y,
WorldLocation,
WorldDirection);

DrawDebugLine(
GetWorld(),
StartLocation,
WorldLocation,
FColor::Red,
false,
0.0);
}

if (bIsHitValid)
{
let TargetLocation = HitResult.Location;
if (IsValid(CursorToWorld))
{
let ImpactNormal = HitResult->ImpactNormal;
let ImpactRotation = ImpactNormal.Rotation();

if (HitResult->GetActor() != this)
{
CursorToWorld->SetWorldLocation(HitResult->Location);
CursorToWorld->SetWorldRotation(ImpactRotation);
SetCursorIsHidden(false);
}
else
{
SetCursorIsHidden(true);
}
}

let Settings = UCloud9DeveloperSettings::Get();

let StartLocation = GetMesh()->GetBoneLocation(CameraTargetBoneName, EBoneSpaces::WorldSpace);

if (Settings->bIsDrawHitCursorLine)
{
DrawDebugLine(
GetWorld(),
StartLocation,
HitResult->Location,
FColor::Green,
false,
0.0);
}

if (Settings->bIsDrawDeprojectedCursorLine)
{
FVector WorldLocation;
FVector WorldDirection;
FVector2D MousePosition;

GetCloud9Controller()->GetMousePosition(MousePosition.X, MousePosition.Y);
GetCloud9Controller()->DeprojectScreenPositionToWorld(
MousePosition.X,
MousePosition.Y,
WorldLocation,
WorldDirection);

DrawDebugLine(
GetWorld(),
StartLocation,
WorldLocation,
FColor::Red,
false,
0.0);
}

let TargetLocation = HitResult->Location;
let LookRotation = UKismetMathLibrary::FindLookAtRotation(StartLocation, TargetLocation);
ViewVerticalRotation = LookRotation.Pitch;
GetCloud9CharacterMovement()->Rotate({0.0f, LookRotation.Yaw, 0.0f});
Expand Down
2 changes: 1 addition & 1 deletion Source/Cloud9/Character/Cloud9Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ACloud9Character : public ACharacter

void UnSneak() const;

void SetViewDirection(const FHitResult& HitResult, bool bIsHitValid);
void SetViewDirection(const TOptional<FHitResult>& HitResult);

UFUNCTION(BlueprintCallable)
float GetViewVerticalRotation() const { return ViewVerticalRotation; }
Expand Down
2 changes: 1 addition & 1 deletion Source/Cloud9/Character/Structures/HealthConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct FHealthConfig
GENERATED_BODY()

UPROPERTY(Category=Config, EditDefaultsOnly, BlueprintReadOnly,
meta=(UIMin="0", UIMax="20000.0", ClampMin="0", ClampMax="20000.0"))
meta=(UIMin="0", UIMax="999.0", ClampMin="0", ClampMax="999.0"))
float Health = 100.0f;

UPROPERTY(Category=Config, EditDefaultsOnly, BlueprintReadOnly,
Expand Down
7 changes: 7 additions & 0 deletions Source/Cloud9/Contollers/Cloud9KeyboardController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "Cloud9KeyboardController.h"

#include "Cloud9/Game/Cloud9DeveloperSettings.h"
#include "GameFramework/SpringArmComponent.h"

#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"
Expand Down Expand Up @@ -107,6 +108,12 @@ void UCloud9KeyboardController::OnReloadPressed() { WeaponAction([](let It) { It

void UCloud9KeyboardController::OnReloadReleased() { WeaponAction([](let It) { It->Reload(true); }); }

void UCloud9KeyboardController::OnCursorSelfAim()
{
static var Settings = UCloud9DeveloperSettings::Get();
Settings->SetVariableValue(UCloud9DeveloperSettings::SelfAimEnabledName, not Settings->bIsSelfAimEnabled);
}

void UCloud9KeyboardController::OnUseAction()
{
if (let Pawn = GetCloud9Pawn(); IsValid(Pawn))
Expand Down
2 changes: 2 additions & 0 deletions Source/Cloud9/Contollers/Cloud9KeyboardController.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class CLOUD9_API UCloud9KeyboardController
void OnReloadPressed();
void OnReloadReleased();

void OnCursorSelfAim();

protected:
float ForwardScale;
float RightScale;
Expand Down
12 changes: 8 additions & 4 deletions Source/Cloud9/Contollers/Cloud9MouseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "Cloud9MouseController.h"

#include "Cloud9/Tools/Cloud9ToolsLibrary.h"
#include "Cloud9/Tools/Extensions/APlayerController.h"
#include "Cloud9/Contollers/Cloud9PlayerController.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"

UCloud9MouseController::UCloud9MouseController()
{
Expand Down Expand Up @@ -117,12 +119,14 @@ void UCloud9MouseController::ProcessCharacterView() const
{
if (let Controller = GetCloud9Controller(); IsValid(Controller))
{
FHitResult TraceHitResult;
let bIsHitValid = Controller->GetHitResultUnderCursor(
static var Settings = UCloud9DeveloperSettings::Get();
let ActorsToIgnore = Settings->bIsSelfAimEnabled ? TArray<AActor*>{} : TArray<AActor*>{Pawn};
let CursorHit = Controller | EAPlayerController::GetHitUnderCursor{
TRACE_CHANNEL,
true,
TraceHitResult);
Pawn->SetViewDirection(TraceHitResult, bIsHitValid);
ActorsToIgnore
};
Pawn->SetViewDirection(CursorHit);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Cloud9/Contollers/Cloud9PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ void ACloud9PlayerController::SetupInputComponent()
InputComponent->BindAction(
"Use", IE_Pressed, KeyboardController, &UCloud9KeyboardController::OnUseAction);

InputComponent->BindAction(
"CursorSelfAim", IE_Pressed, KeyboardController, &UCloud9KeyboardController::OnCursorSelfAim);

KeyboardController->OnMoveDelegate.AddDynamic(MouseController, &UCloud9MouseController::OnCharacterMove);
}

Expand Down
61 changes: 41 additions & 20 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
#include "Cloud9/Tools/Extensions/FString.h"
#include "Cloud9/Tools/Extensions/UObject.h"

FString UCloud9DeveloperSettings::ShowMouseCursorName = "r.ShowMouseCursor";
FString UCloud9DeveloperSettings::DrawDeprojectedCursorLineName = "r.DrawDeprojectedCursorLine";
FString UCloud9DeveloperSettings::DrawHitCursorLineName = "r.DrawHitCursorLine";
FString UCloud9DeveloperSettings::DrawExplosionSphereName = "r.DrawExplosionSphere";
FString UCloud9DeveloperSettings::DrawHitScanName = "r.DrawHitScan";
FString UCloud9DeveloperSettings::PrintHitScanInfoName = "r.PrintHitScanInfo";
FString UCloud9DeveloperSettings::NetGraphName = "r.NetGraph";
FString UCloud9DeveloperSettings::AutoSelectWeaponName = "r.AutoSelectWeapon";
FString UCloud9DeveloperSettings::InfiniteAmmoName = "r.InfiniteAmmo";
FString UCloud9DeveloperSettings::CheatsName = "r.Cheats";
FString UCloud9DeveloperSettings::SelfAimEnabledName = "r.SelfAimEnabled";
FString UCloud9DeveloperSettings::CameraVerticalSpeedLagName = "r.CameraVerticalSpeedLag";
FString UCloud9DeveloperSettings::VolumeName = "r.Volume";

// ReSharper disable once CppPossiblyUninitializedMember
UCloud9DeveloperSettings::UCloud9DeveloperSettings(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
Expand All @@ -41,12 +55,13 @@ UCloud9DeveloperSettings::UCloud9DeveloperSettings(const FObjectInitializer& Obj
bIsAutoSelectWeapon = 0;
bIsInfiniteAmmo = 0;
bIsCheatsEnabled = 0;
bIsSelfAimEnabled = 0;
Volume = 0.1;
}

const UCloud9DeveloperSettings* UCloud9DeveloperSettings::Get()
UCloud9DeveloperSettings* UCloud9DeveloperSettings::Get()
{
static let Settings = StaticClass()->GetDefaultObject<UCloud9DeveloperSettings>();
static var Settings = StaticClass()->GetDefaultObject<UCloud9DeveloperSettings>();
Settings->InitializeCVars();
return Settings;
}
Expand All @@ -57,14 +72,14 @@ void UCloud9DeveloperSettings::Save()
log(Display, "%s", this | EUObject::Stringify{} | EFString::ToCStr{});
}

template <typename TValue>
auto UCloud9DeveloperSettings::RegisterConsoleVariable(TValue& ValueRef, const TCHAR* Name, const TCHAR* Help)
template <typename ValueType>
auto UCloud9DeveloperSettings::RegisterConsoleVariable(ValueType& ValueRef, const TCHAR* Name, const TCHAR* Help)
{
static_assert(
TIsSame<TValue, int>::Value ||
TIsSame<TValue, float>::Value ||
TIsSame<TValue, bool>::Value ||
TIsSame<TValue, FString>::Value,
TIsSame<ValueType, int>::Value ||
TIsSame<ValueType, float>::Value ||
TIsSame<ValueType, bool>::Value ||
TIsSame<ValueType, FString>::Value,
"TValue must be int, float, bool or FString"
);

Expand All @@ -89,73 +104,79 @@ void UCloud9DeveloperSettings::InitializeCVars()

RegisterConsoleVariable(
bIsShowMouseCursor,
TEXT("r.IsShowMouseCursor"),
*ShowMouseCursorName,
TEXT("Whether to draw line from character to GetHitResultUnderCursor point")
);

RegisterConsoleVariable(
bIsDrawDeprojectedCursorLine,
TEXT("r.IsDrawDeprojectedCursorLine"),
*DrawDeprojectedCursorLineName,
TEXT("Whether to draw line from character to deprojected mouse cursor")
);

RegisterConsoleVariable(
bIsDrawHitCursorLine,
TEXT("r.bIsDrawHitCursorLine"),
*DrawHitCursorLineName,
TEXT("Whether to show mouse cursor on screen or not in game")
);

RegisterConsoleVariable(
bIsDrawExplosionSpheres,
TEXT("r.bIsDrawExplosionSpheres"),
*DrawExplosionSphereName,
TEXT("Whether to draw debug explosions spheres")
);

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

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

RegisterConsoleVariable(
NetGraph,
TEXT("r.NetGraph"),
*NetGraphName,
TEXT("Whether to show FPS and other specific debug info")
);

RegisterConsoleVariable(
bIsAutoSelectWeapon,
TEXT("r.AutoSelectWeapon"),
*AutoSelectWeaponName,
TEXT("Select weapon after picking it up")
);

RegisterConsoleVariable(
bIsInfiniteAmmo,
TEXT("r.InfiniteAmmo"),
*InfiniteAmmoName,
TEXT("Infinite Weapon Ammo")
);

RegisterConsoleVariable(
bIsCheatsEnabled,
TEXT("r.Cheats"),
*CheatsName,
TEXT("Enable cheats")
);

RegisterConsoleVariable(
bIsSelfAimEnabled,
*SelfAimEnabledName,
TEXT("Enable self aim")
);

RegisterConsoleVariable(
CameraVerticalSpeedLag,
TEXT("r.CameraVerticalSpeedLag"),
*CameraVerticalSpeedLagName,
TEXT("Configure how smoothly does the camera change its position vertically")
);

RegisterConsoleVariable(
Volume,
TEXT("r.Volume"),
*VolumeName,
TEXT("Basic game volume")
);

Expand Down
Loading

0 comments on commit ed084c1

Please sign in to comment.