From c6059578ace80f17c8c8597380e23fd9d4c0c17e Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 12 Jun 2024 00:34:24 +0100 Subject: [PATCH] Add basic PlayerCharacter --- Config/DefaultEngine.ini | 2 + Content/Input/IA_Movement.uasset | 3 ++ Content/Input/IMC_Player.uasset | 3 ++ Content/Player/BP_PlayerCharacter.uasset | 4 +- Source/vampires/PlayerCharacter.cpp | 64 ++++++++++++++++++++++++ Source/vampires/PlayerCharacter.h | 35 +++++++++++++ Source/vampires/VampireGameInstance.cpp | 5 ++ Source/vampires/VampireGameInstance.h | 17 +++++++ Source/vampires/VampireGameMode.cpp | 5 ++ Source/vampires/VampireGameMode.h | 17 +++++++ Source/vampires/vampires.Build.cs | 23 +++++++++ Source/vampires/vampires.cpp | 6 +++ Source/vampires/vampires.h | 6 +++ 13 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 Content/Input/IA_Movement.uasset create mode 100644 Content/Input/IMC_Player.uasset create mode 100644 Source/vampires/VampireGameInstance.cpp create mode 100644 Source/vampires/VampireGameInstance.h create mode 100644 Source/vampires/VampireGameMode.cpp create mode 100644 Source/vampires/VampireGameMode.h create mode 100644 Source/vampires/vampires.Build.cs create mode 100644 Source/vampires/vampires.cpp create mode 100644 Source/vampires/vampires.h diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index b96b521..cd16594 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -2,6 +2,8 @@ [/Script/EngineSettings.GameMapsSettings] GameDefaultMap=/Engine/Maps/Templates/OpenWorld +GlobalDefaultGameMode=/Game/Gamemode/BP_DefaultGamemode.BP_DefaultGamemode_C +GameInstanceClass=/Script/vampires.VampireGameInstance [/Script/WindowsTargetPlatform.WindowsTargetSettings] DefaultGraphicsRHI=DefaultGraphicsRHI_DX12 diff --git a/Content/Input/IA_Movement.uasset b/Content/Input/IA_Movement.uasset new file mode 100644 index 0000000..23b509f --- /dev/null +++ b/Content/Input/IA_Movement.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b4cf5d1e4a245f960f3efb51ef4850815731fd76ba1feb5f9509ea2f8954c45 +size 1564 diff --git a/Content/Input/IMC_Player.uasset b/Content/Input/IMC_Player.uasset new file mode 100644 index 0000000..0f69942 --- /dev/null +++ b/Content/Input/IMC_Player.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18d7779f366bb4f8e817f76c0fce18c723848a791a7214f5d4c89da2249fbc7e +size 5664 diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index 74441af..d0be04b 100644 --- a/Content/Player/BP_PlayerCharacter.uasset +++ b/Content/Player/BP_PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1da5849d936804c06c8ab769fb20e7027bfd82571a467e25a306c455bf8602da -size 24374 +oid sha256:63c52d9b60aa76b546b5aeed774b52fcff5670b5335f2d4342a84836ab46c109 +size 31377 diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 0540dd3..5e84e84 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -3,3 +3,67 @@ #include "PlayerCharacter.h" +#include "VampirePlayerController.h" +#include "EnhancedInputComponent.h" +#include "EnhancedInputSubsystems.h" +#include "InputMappingContext.h" + +APlayerCharacter::APlayerCharacter() +{ + // Create Camera Boom + CameraSpringArmComponent = CreateDefaultSubobject(TEXT("CameraSpringArmComponent")); + CameraSpringArmComponent->SetupAttachment(RootComponent); + CameraSpringArmComponent->bDoCollisionTest = true; + CameraSpringArmComponent->bUsePawnControlRotation = false; + CameraSpringArmComponent->TargetArmLength = 1000; + CameraSpringArmComponent->bEnableCameraLag = false; + CameraSpringArmComponent->SocketOffset = { 0.0f, 0.0f, 0.0f }; + CameraSpringArmComponent->SetRelativeRotation({-90.0, 0.0f, 0.0f}); + + // Create Camera + CameraComponent = CreateDefaultSubobject(TEXT("Camera Component")); + CameraComponent->SetupAttachment(CameraSpringArmComponent, USpringArmComponent::SocketName); + CameraComponent->bUsePawnControlRotation = false; + CameraComponent->SetProjectionMode(ECameraProjectionMode::Type::Orthographic); + CameraComponent->SetOrthoWidth(4000.0f); +} + +void APlayerCharacter::BeginPlay() +{ + Super::BeginPlay(); +} + +void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + + if (AVampirePlayerController* TankPlayerController = Cast(GetController())) + { + if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem(TankPlayerController->GetLocalPlayer())) + { + if (!InputMappingContext.IsNull()) + { + InputSystem->AddMappingContext(InputMappingContext.LoadSynchronous(), 0); + } + } + } + + if (UEnhancedInputComponent* Input = Cast(PlayerInputComponent)) + { + if (MovementAction) + { + Input->BindAction(MovementAction, ETriggerEvent::Triggered, this, &APlayerCharacter::MovementCallback); + } + } +} + +void APlayerCharacter::MovementCallback(const FInputActionInstance& Instance) +{ + FVector2D vec2 = Instance.GetValue().Get(); + + if (vec2.Size() != 0.0f) + { + AddMovementInput({0.0f,1.0f,0.0f}, vec2.Y); + AddMovementInput({1.0f,0.0f,0.0f}, vec2.X); + } +} diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index 109a9d7..50367ee 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -4,8 +4,13 @@ #include "CoreMinimal.h" #include "VampireCharacter.h" +#include "Camera/CameraComponent.h" +#include "GameFramework/SpringArmComponent.h" #include "PlayerCharacter.generated.h" +class UInputMappingContext; +class UInputAction; + /** * */ @@ -13,5 +18,35 @@ UCLASS() class VAMPIRES_API APlayerCharacter : public AVampireCharacter { GENERATED_BODY() + +public: + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + USpringArmComponent* CameraSpringArmComponent = nullptr; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UCameraComponent* CameraComponent = nullptr; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + TSoftObjectPtr InputMappingContext; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* MovementAction; + +public: + APlayerCharacter(); + +protected: + virtual void BeginPlay() override; + +public: + virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; + +private: + + UFUNCTION() + void MovementCallback(const FInputActionInstance& Instance); }; + + diff --git a/Source/vampires/VampireGameInstance.cpp b/Source/vampires/VampireGameInstance.cpp new file mode 100644 index 0000000..0f65da2 --- /dev/null +++ b/Source/vampires/VampireGameInstance.cpp @@ -0,0 +1,5 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "VampireGameInstance.h" + diff --git a/Source/vampires/VampireGameInstance.h b/Source/vampires/VampireGameInstance.h new file mode 100644 index 0000000..e3ccc26 --- /dev/null +++ b/Source/vampires/VampireGameInstance.h @@ -0,0 +1,17 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Engine/GameInstance.h" +#include "VampireGameInstance.generated.h" + +/** + * + */ +UCLASS() +class VAMPIRES_API UVampireGameInstance : public UGameInstance +{ + GENERATED_BODY() + +}; diff --git a/Source/vampires/VampireGameMode.cpp b/Source/vampires/VampireGameMode.cpp new file mode 100644 index 0000000..7f36029 --- /dev/null +++ b/Source/vampires/VampireGameMode.cpp @@ -0,0 +1,5 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "VampireGameMode.h" + diff --git a/Source/vampires/VampireGameMode.h b/Source/vampires/VampireGameMode.h new file mode 100644 index 0000000..b869eb9 --- /dev/null +++ b/Source/vampires/VampireGameMode.h @@ -0,0 +1,17 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/GameMode.h" +#include "VampireGameMode.generated.h" + +/** + * + */ +UCLASS() +class VAMPIRES_API AVampireGameMode : public AGameMode +{ + GENERATED_BODY() + +}; diff --git a/Source/vampires/vampires.Build.cs b/Source/vampires/vampires.Build.cs new file mode 100644 index 0000000..62aeb11 --- /dev/null +++ b/Source/vampires/vampires.Build.cs @@ -0,0 +1,23 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +using UnrealBuildTool; + +public class vampires : ModuleRules +{ + public vampires(ReadOnlyTargetRules Target) : base(Target) + { + PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; + + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" }); + + PrivateDependencyModuleNames.AddRange(new string[] { }); + + // 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/vampires/vampires.cpp b/Source/vampires/vampires.cpp new file mode 100644 index 0000000..bbfa017 --- /dev/null +++ b/Source/vampires/vampires.cpp @@ -0,0 +1,6 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#include "vampires.h" +#include "Modules/ModuleManager.h" + +IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, vampires, "vampires" ); diff --git a/Source/vampires/vampires.h b/Source/vampires/vampires.h new file mode 100644 index 0000000..90aad9e --- /dev/null +++ b/Source/vampires/vampires.h @@ -0,0 +1,6 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +