Add Base Character Movement for Top Down

This commit is contained in:
Philip W 2024-01-26 18:33:02 +00:00
parent 4b82b341a7
commit 56b84d09c8
19 changed files with 145 additions and 157 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -1,19 +1,15 @@
[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/TopDown/Maps/TopDownMap.TopDownMap
EditorStartupMap=/Game/TopDown/Maps/TopDownMap.TopDownMap
GlobalDefaultGameMode="/Script/SeagullGame.SeagullGameGameMode"
GlobalDefaultGameMode=/Script/SeagullGame.SeagullGameGameMode
[/Script/Engine.RendererSettings]
r.Mobile.EnableNoPrecomputedLightingCSMShader=1
r.DefaultFeature.AutoExposure.ExtendDefaultLuminanceRange=True
r.DefaultFeature.AutoExposure.ExtendDefaultLuminanceRange=true
r.GenerateMeshDistanceFields=True
r.DynamicGlobalIlluminationMethod=1
r.ReflectionMethod=1
r.Shadow.Virtual.Enable=1
[/Script/WindowsTargetPlatform.WindowsTargetSettings]
@ -45,7 +41,6 @@ CompressionQualityModifier=1.000000
AutoStreamingThreshold=0.000000
SoundCueCookQualityIndex=-1
[/Script/HardwareTargeting.HardwareTargetingSettings]
TargetedHardwareClass=Desktop
AppliedTargetedHardwareClass=Desktop
@ -55,6 +50,8 @@ AppliedDefaultGraphicsPerformance=Maximum
[/Script/Engine.Engine]
+ActiveGameNameRedirects=(OldGameName="TP_TopDown",NewGameName="/Script/SeagullGame")
+ActiveGameNameRedirects=(OldGameName="/Script/TP_TopDown",NewGameName="/Script/SeagullGame")
+ActiveGameNameRedirects=(OldGameName="/Script/TP_ThirdPerson",NewGameName="/Script/SeagullGame")
+ActiveGameNameRedirects=(OldGameName="TP_ThirdPerson",NewGameName="/Script/SeagullGame")
+ActiveClassRedirects=(OldClassName="TP_TopDownPlayerController",NewClassName="SeagullGamePlayerController")
+ActiveClassRedirects=(OldClassName="TP_TopDownGameMode",NewClassName="SeagullGameGameMode")
+ActiveClassRedirects=(OldClassName="TP_TopDownCharacter",NewClassName="SeagullGameCharacter")

View File

@ -68,6 +68,7 @@ bEnableLegacyInputScales=True
bEnableMotionControls=True
bFilterInputByPlatformUser=False
bShouldFlushPressedKeysOnViewportFocusLost=True
bEnableDynamicComponentInputBinding=True
bAlwaysShowTouchInterface=False
bShowConsoleOnFourFingerTap=True
bEnableGestureRecognizer=False
@ -76,6 +77,18 @@ DefaultViewportMouseCaptureMode=CapturePermanently_IncludingInitialMouseDown
DefaultViewportMouseLockMode=LockOnCapture
FOVScale=0.011110
DoubleClickTime=0.200000
+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Gamepad_FaceButton_Bottom)
+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=SpaceBar)
+AxisMappings=(AxisName="Look Up / Down Gamepad",Scale=1.000000,Key=Gamepad_RightY)
+AxisMappings=(AxisName="Look Up / Down Mouse",Scale=-1.000000,Key=MouseY)
+AxisMappings=(AxisName="Move Forward / Backward",Scale=1.000000,Key=Gamepad_LeftY)
+AxisMappings=(AxisName="Move Forward / Backward",Scale=-1.000000,Key=S)
+AxisMappings=(AxisName="Move Forward / Backward",Scale=1.000000,Key=W)
+AxisMappings=(AxisName="Move Right / Left",Scale=-1.000000,Key=A)
+AxisMappings=(AxisName="Move Right / Left",Scale=1.000000,Key=D)
+AxisMappings=(AxisName="Move Right / Left",Scale=1.000000,Key=Gamepad_LeftX)
+AxisMappings=(AxisName="Turn Right / Left Gamepad",Scale=1.000000,Key=Gamepad_RightX)
+AxisMappings=(AxisName="Turn Right / Left Mouse",Scale=1.000000,Key=MouseX)
DefaultPlayerInputClass=/Script/EnhancedInput.EnhancedPlayerInput
DefaultInputComponentClass=/Script/EnhancedInput.EnhancedInputComponent
DefaultTouchInterface=None

Binary file not shown.

Binary file not shown.

BIN
SeagullGame/Content/TopDown/Input/Actions/IAS_Jump.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/TopDown/Input/Actions/IAS_Look.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/TopDown/Input/Actions/IAS_Move.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
SeagullGame/Content/TopDown/Input/IMC_DefaultMain.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,9 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "SeagullGameCharacter.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "UObject/ConstructorHelpers.h"
#include "Camera/CameraComponent.h"
#include "Components/DecalComponent.h"
@ -47,5 +50,66 @@ ASeagullGameCharacter::ASeagullGameCharacter()
void ASeagullGameCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
Super::Tick(DeltaSeconds);
}
void ASeagullGameCharacter::BeginPlay()
{
Super::BeginPlay();
//Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
//Jumping
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//Moving
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ASeagullGameCharacter::Move);
//Looking
// EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ATP_ThirdPersonCharacter::Look);
}
}
void ASeagullGameCharacter::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get<FVector2D>();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Move: %f, %f"), MovementVector.X, MovementVector.Y));
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ASeagullGameCharacter::Look(const FInputActionValue& Value)
{
}

View File

@ -3,6 +3,7 @@
#pragma once
#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "GameFramework/Character.h"
#include "SeagullGameCharacter.generated.h"
@ -17,10 +18,36 @@ public:
// Called every frame.
virtual void Tick(float DeltaSeconds) override;
/** Returns TopDownCameraComponent subobject **/
/** Returns TopDownCameraComponent sub-object **/
FORCEINLINE class UCameraComponent* GetTopDownCameraComponent() const { return TopDownCameraComponent; }
/** Returns CameraBoom subobject **/
/** Returns CameraBoom sub-object **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
/** Move Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* MoveAction;
/** Look Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* LookAction;
virtual void BeginPlay() override;
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
/** Called for movement input */
void Move(const FInputActionValue& Value);
/** Called for looking input */
void Look(const FInputActionValue& Value);
private:
/** Top down camera */
@ -30,5 +57,5 @@ private:
/** Camera boom positioning the camera above the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
};
};

View File

@ -12,10 +12,7 @@
ASeagullGamePlayerController::ASeagullGamePlayerController()
{
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Default;
CachedDestination = FVector::ZeroVector;
FollowTime = 0.f;
}
void ASeagullGamePlayerController::BeginPlay()
@ -23,95 +20,4 @@ void ASeagullGamePlayerController::BeginPlay()
// Call the base class
Super::BeginPlay();
//Add Input Mapping Context
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
void ASeagullGamePlayerController::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent))
{
// Setup mouse input events
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Started, this, &ASeagullGamePlayerController::OnInputStarted);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Triggered, this, &ASeagullGamePlayerController::OnSetDestinationTriggered);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Completed, this, &ASeagullGamePlayerController::OnSetDestinationReleased);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Canceled, this, &ASeagullGamePlayerController::OnSetDestinationReleased);
// Setup touch input events
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Started, this, &ASeagullGamePlayerController::OnInputStarted);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Triggered, this, &ASeagullGamePlayerController::OnTouchTriggered);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Completed, this, &ASeagullGamePlayerController::OnTouchReleased);
EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Canceled, this, &ASeagullGamePlayerController::OnTouchReleased);
}
}
void ASeagullGamePlayerController::OnInputStarted()
{
StopMovement();
}
// Triggered every frame when the input is held down
void ASeagullGamePlayerController::OnSetDestinationTriggered()
{
// We flag that the input is being pressed
FollowTime += GetWorld()->GetDeltaSeconds();
// We look for the location in the world where the player has pressed the input
FHitResult Hit;
bool bHitSuccessful = false;
if (bIsTouch)
{
bHitSuccessful = GetHitResultUnderFinger(ETouchIndex::Touch1, ECollisionChannel::ECC_Visibility, true, Hit);
}
else
{
bHitSuccessful = GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, true, Hit);
}
// If we hit a surface, cache the location
if (bHitSuccessful)
{
CachedDestination = Hit.Location;
}
// Move towards mouse pointer or touch
APawn* ControlledPawn = GetPawn();
if (ControlledPawn != nullptr)
{
FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();
ControlledPawn->AddMovementInput(WorldDirection, 1.0, false);
}
}
void ASeagullGamePlayerController::OnSetDestinationReleased()
{
// If it was a short press
if (FollowTime <= ShortPressThreshold)
{
// We move there and spawn some particles
UAIBlueprintHelperLibrary::SimpleMoveToLocation(this, CachedDestination);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, FXCursor, CachedDestination, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f), true, true, ENCPoolMethod::None, true);
}
FollowTime = 0.f;
}
// Triggered every frame when the input is held down
void ASeagullGamePlayerController::OnTouchTriggered()
{
bIsTouch = true;
OnSetDestinationTriggered();
}
void ASeagullGamePlayerController::OnTouchReleased()
{
bIsTouch = false;
OnSetDestinationReleased();
}

View File

@ -19,47 +19,11 @@ class ASeagullGamePlayerController : public APlayerController
public:
ASeagullGamePlayerController();
/** Time Threshold to know if it was a short press */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
float ShortPressThreshold;
/** FX Class that we will spawn when clicking */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UNiagaraSystem* FXCursor;
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
class UInputAction* SetDestinationClickAction;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
class UInputAction* SetDestinationTouchAction;
protected:
/** True if the controlled character should navigate to the mouse cursor. */
uint32 bMoveToMouseCursor : 1;
virtual void SetupInputComponent() override;
// To add mapping context
virtual void BeginPlay();
/** Input handlers for SetDestination action. */
void OnInputStarted();
void OnSetDestinationTriggered();
void OnSetDestinationReleased();
void OnTouchTriggered();
void OnTouchReleased();
virtual void BeginPlay() override;
private:
FVector CachedDestination;
bool bIsTouch; // Is it a touch device
float FollowTime; // For how long it has been pressed
};