From eb3f25509877e7c5a5a06eadf7482b411fe873cc Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 4 Jun 2024 00:43:52 +0100 Subject: [PATCH] Create TankPlayerCharacter --- Content/Input/IA_Movement.uasset | 3 + Content/Input/IA_Yaw.uasset | 3 + Content/Input/IMC_Player.uasset | 3 + Content/Player/BP_TankPlayerCharacter.uasset | 3 + Source/tank/TankPlayerCharacter.cpp | 74 ++++++++++++++++++++ Source/tank/TankPlayerCharacter.h | 52 ++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 Content/Input/IA_Movement.uasset create mode 100644 Content/Input/IA_Yaw.uasset create mode 100644 Content/Input/IMC_Player.uasset create mode 100644 Content/Player/BP_TankPlayerCharacter.uasset create mode 100644 Source/tank/TankPlayerCharacter.cpp create mode 100644 Source/tank/TankPlayerCharacter.h diff --git a/Content/Input/IA_Movement.uasset b/Content/Input/IA_Movement.uasset new file mode 100644 index 0000000..5e49476 --- /dev/null +++ b/Content/Input/IA_Movement.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:946d5a02a6f7dc06a97b0a8f648f40cdddc52e7658236a886860953e9cd29958 +size 1564 diff --git a/Content/Input/IA_Yaw.uasset b/Content/Input/IA_Yaw.uasset new file mode 100644 index 0000000..85fa9b5 --- /dev/null +++ b/Content/Input/IA_Yaw.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:409df4c717b732c68b65e19b7a38c48ffa1f8f77ae0bcad151253a157cdf411c +size 1539 diff --git a/Content/Input/IMC_Player.uasset b/Content/Input/IMC_Player.uasset new file mode 100644 index 0000000..5893828 --- /dev/null +++ b/Content/Input/IMC_Player.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:17af241f3bba3ab30f0f96e158036fc6910814836a7eebd0cd1f42ff0e336e9d +size 6107 diff --git a/Content/Player/BP_TankPlayerCharacter.uasset b/Content/Player/BP_TankPlayerCharacter.uasset new file mode 100644 index 0000000..a6251ea --- /dev/null +++ b/Content/Player/BP_TankPlayerCharacter.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c49bea70dc4bf0aa41007ff7c307f19d3828237ba705edcf92d07dff72f0a4d0 +size 32724 diff --git a/Source/tank/TankPlayerCharacter.cpp b/Source/tank/TankPlayerCharacter.cpp new file mode 100644 index 0000000..e78a031 --- /dev/null +++ b/Source/tank/TankPlayerCharacter.cpp @@ -0,0 +1,74 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "TankPlayerCharacter.h" + +#include "TankPlayerController.h" +#include "EnhancedInputComponent.h" +#include "EnhancedInputSubsystems.h" +#include "InputMappingContext.h" + +// Sets default values +ATankPlayerCharacter::ATankPlayerCharacter() +{ + // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + +} + +// Called when the game starts or when spawned +void ATankPlayerCharacter::BeginPlay() +{ + Super::BeginPlay(); + +} + +// Called every frame +void ATankPlayerCharacter::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +// Called to bind functionality to input +void ATankPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + + if (ATankPlayerController* 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, &ATankPlayerCharacter::MovementCallback); + } + + if (YawAction) + { + Input->BindAction(YawAction, ETriggerEvent::Triggered, this, &ATankPlayerCharacter::RotationCallback); + } + } +} + +void ATankPlayerCharacter::MovementCallback(const FInputActionInstance& Instance) +{ + float Movement = Instance.GetValue().Get(); + AddMovementInput(GetActorForwardVector(), Movement); +} + +void ATankPlayerCharacter::RotationCallback(const FInputActionInstance& Instance) +{ + float Rotation = Instance.GetValue().Get(); + AddControllerYawInput(Rotation); +} + diff --git a/Source/tank/TankPlayerCharacter.h b/Source/tank/TankPlayerCharacter.h new file mode 100644 index 0000000..63f75ad --- /dev/null +++ b/Source/tank/TankPlayerCharacter.h @@ -0,0 +1,52 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Character.h" +#include "TankPlayerCharacter.generated.h" + +class UInputMappingContext; +class UInputAction; + +UCLASS() +class TANK_API ATankPlayerCharacter : public ACharacter +{ + GENERATED_BODY() + + +public: + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + TSoftObjectPtr InputMappingContext; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* MovementAction; + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* YawAction; + +public: + // Sets default values for this character's properties + ATankPlayerCharacter(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + + // Called to bind functionality to input + virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + +private: + + UFUNCTION() + void MovementCallback(const FInputActionInstance& Instance); + + UFUNCTION() + void RotationCallback(const FInputActionInstance& Instance); + +};