From 0b93ede282e9316ccb012be60f4cf9051938411f Mon Sep 17 00:00:00 2001 From: Oleg Petruny Date: Thu, 2 May 2024 16:04:02 +0200 Subject: [PATCH] Add settings and base player class --- Source/Lost_Edge/Lost_Edge.Build.cs | 28 ++--- Source/Lost_Edge/Private/APlayerBase.cpp | 111 ++++++++++++++++++ Source/Lost_Edge/Private/APlayerBase.h | 66 +++++++++++ .../Private/CustomGameUserSettings.cpp | 26 ++++ .../Private/CustomGameUserSettings.h | 44 +++++++ 5 files changed, 260 insertions(+), 15 deletions(-) create mode 100644 Source/Lost_Edge/Private/APlayerBase.cpp create mode 100644 Source/Lost_Edge/Private/APlayerBase.h create mode 100644 Source/Lost_Edge/Private/CustomGameUserSettings.cpp create mode 100644 Source/Lost_Edge/Private/CustomGameUserSettings.h diff --git a/Source/Lost_Edge/Lost_Edge.Build.cs b/Source/Lost_Edge/Lost_Edge.Build.cs index 70aaf7c..542bd5f 100644 --- a/Source/Lost_Edge/Lost_Edge.Build.cs +++ b/Source/Lost_Edge/Lost_Edge.Build.cs @@ -2,22 +2,20 @@ using UnrealBuildTool; -public class Lost_Edge : ModuleRules -{ - public Lost_Edge(ReadOnlyTargetRules Target) : base(Target) - { - PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - - PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" }); +public class Lost_Edge : ModuleRules { + public Lost_Edge(ReadOnlyTargetRules Target) : base(Target) { + PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - PrivateDependencyModuleNames.AddRange(new string[] { }); + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" }); - // Uncomment if you are using Slate UI - // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); - - // Uncomment if you are using online features - // PrivateDependencyModuleNames.Add("OnlineSubsystem"); + PrivateDependencyModuleNames.AddRange(new string[] { }); - // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true - } + // Uncomment if you are using Slate UI + // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); + + // Uncomment if you are using online features + // PrivateDependencyModuleNames.Add("OnlineSubsystem"); + + // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true + } } diff --git a/Source/Lost_Edge/Private/APlayerBase.cpp b/Source/Lost_Edge/Private/APlayerBase.cpp new file mode 100644 index 0000000..918e6f5 --- /dev/null +++ b/Source/Lost_Edge/Private/APlayerBase.cpp @@ -0,0 +1,111 @@ +// Oleg Petruny proprietary. + + +#include "APlayerBase.h" + +#include "CustomGameUserSettings.h" +#include "EnhancedInputSubsystems.h" +#include "GameFramework/CharacterMovementComponent.h" +#include "Kismet/GameplayStatics.h" +#include "Kismet/KismetMathLibrary.h" + +APlayerBase::APlayerBase() +{ + PrimaryActorTick.bCanEverTick = true; + +} + +void APlayerBase::BeginPlay() +{ + Super::BeginPlay(); + + auto world = GetWorld(); + + GetCharacterMovement()->MaxWalkSpeed = moveSpeed; + + auto cameraManager = UGameplayStatics::GetPlayerCameraManager(world, 0); + cameraManager->ViewPitchMin = minPitch; + cameraManager->ViewPitchMax = maxPitch; + + auto gameSettings = UCustomGameUserSettings::GetCustomGameUserSettings(); + + if(auto camera = FindComponentByClass()) + { + camera->PostProcessSettings.MotionBlurAmount = gameSettings->bUseMotionBlur ? 1.0f : 0.0f; + } + + if(gameSettings->bShowFps) + world->Exec(world, TEXT("show stats")); + + mouseInvert = gameSettings->bMouseInverted; + mouseSensetivity = gameSettings->GetMouseSensetivity(); + + if(inputMapping) + { + if(auto player = Cast(GetController())) + { + if(auto inputSubsystem = ULocalPlayer::GetSubsystem(player->GetLocalPlayer())) + { + inputSubsystem->ClearAllMappings(); + inputSubsystem->AddMappingContext(inputMapping, 0); + } + } + } + +} + +void APlayerBase::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + + // stabilize move speed by fps + AddActorLocalOffset(ConsumeMovementInputVector() * DeltaTime); +} + +void APlayerBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + +} + +void APlayerBase::MoveCamera(FVector2D value) +{ + if(cameraLocked) + return; + + if(mouseInvert) + value.Y *= -1; + + value *= mouseSensetivity; + + AddControllerYawInput(value.X); + AddControllerPitchInput(value.Y); +} + +void APlayerBase::MoveCharacter(FVector2D value) +{ + if(moveLocked) + return; + + AddMovementInput(GetActorRightVector(), value.X); + AddMovementInput(GetActorForwardVector(), value.Y); +} + +void APlayerBase::Jump() +{ + if(jumpLocked) + return; + + Super::Jump(); +} + +void APlayerBase::SwitchRun(bool run) +{ + if(runLocked) + return; + + if(run) + GetCharacterMovement()->MaxWalkSpeed = moveSpeed * runSpeedMultiplier; + else + GetCharacterMovement()->MaxWalkSpeed = moveSpeed; +} \ No newline at end of file diff --git a/Source/Lost_Edge/Private/APlayerBase.h b/Source/Lost_Edge/Private/APlayerBase.h new file mode 100644 index 0000000..d337945 --- /dev/null +++ b/Source/Lost_Edge/Private/APlayerBase.h @@ -0,0 +1,66 @@ +// Oleg Petruny proprietary. + +#pragma once + +#include "Camera/CameraComponent.h" +#include "CoreMinimal.h" +#include "GameFramework/Character.h" +#include "InputMappingContext.h" + +#include "APlayerBase.generated.h" + +UCLASS() +class APlayerBase : public ACharacter +{ + GENERATED_BODY() + +public: + APlayerBase(); + + virtual void Tick(float DeltaTime) override; + + virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + +protected: + virtual void BeginPlay() override; + + UFUNCTION(BlueprintCallable, Category = Character) + void MoveCamera(FVector2D value); + UFUNCTION(BlueprintCallable, Category = Character) + void MoveCharacter(FVector2D value); + virtual void Jump() override; + UFUNCTION(BlueprintCallable, Category = Character) + void SwitchRun(bool run); + + UPROPERTY(EditDefaultsOnly) + UInputMappingContext* inputMapping; + + UPROPERTY(EditDefaultsOnly) + float moveSpeed = 200; + UPROPERTY(EditDefaultsOnly) + float runSpeedMultiplier = 4; + + UPROPERTY(EditAnywhere) + bool moveLocked = false; + UPROPERTY(EditAnywhere) + bool jumpLocked = false; + UPROPERTY(EditAnywhere) + bool runLocked = false; + + UPROPERTY(EditDefaultsOnly) + float minPitch = -80; + UPROPERTY(EditDefaultsOnly) + float maxPitch = 65; + + // not visible :( + //UPROPERTY(EditDefaultsOnly) + //UCameraComponent* camera; + + bool mouseInvert = false; + float mouseSensetivity = 1.0f; + UPROPERTY(EditAnywhere) + bool cameraLocked = false; + +private: + +}; diff --git a/Source/Lost_Edge/Private/CustomGameUserSettings.cpp b/Source/Lost_Edge/Private/CustomGameUserSettings.cpp new file mode 100644 index 0000000..f0e5d74 --- /dev/null +++ b/Source/Lost_Edge/Private/CustomGameUserSettings.cpp @@ -0,0 +1,26 @@ +// Oleg Petruny proprietary. + +#include "CustomGameUserSettings.h" + +UCustomGameUserSettings::UCustomGameUserSettings(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer) +{ + bUseMotionBlur = false; + bShowFps = false; + bMouseInverted = false; + fMouseSensetivity = 1.0f; +} + +UCustomGameUserSettings* UCustomGameUserSettings::GetCustomGameUserSettings() +{ + return Cast(UGameUserSettings::GetGameUserSettings()); +} + +void UCustomGameUserSettings::SetMouseSensetivity(float value) +{ + fMouseSensetivity = FMath::Clamp(value, 0.1f, 1.0f); +} + +float UCustomGameUserSettings::GetMouseSensetivity() const +{ + return fMouseSensetivity; +} \ No newline at end of file diff --git a/Source/Lost_Edge/Private/CustomGameUserSettings.h b/Source/Lost_Edge/Private/CustomGameUserSettings.h new file mode 100644 index 0000000..a65fb5f --- /dev/null +++ b/Source/Lost_Edge/Private/CustomGameUserSettings.h @@ -0,0 +1,44 @@ +// Oleg Petruny proprietary. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/GameUserSettings.h" + +#include "CustomGameUserSettings.generated.h" + +/** + * + */ +UCLASS() +class UCustomGameUserSettings : public UGameUserSettings +{ + GENERATED_UCLASS_BODY() + +public: + UFUNCTION(BlueprintCallable, Category = Settings) + static UCustomGameUserSettings* GetCustomGameUserSettings(); + + // Sets mouse sensetivity multiplier + // @param value [0.1 - 1.0] + UFUNCTION(BlueprintCallable, Category = Settings) + void SetMouseSensetivity(float value); + + // Returns mouse sensetivity multiplier in [0.1 - 1.0] + UFUNCTION(BlueprintCallable, Category = Settings) + float GetMouseSensetivity() const; + + UPROPERTY(config) + bool bUseMotionBlur; + + UPROPERTY(config) + bool bShowFps; + + UPROPERTY(config) + bool bMouseInverted; + +protected: + UPROPERTY(config) + float fMouseSensetivity; + +};